import { ApplicationFormStates } from "@boldpenguin/sdk-types";
import { newSpecPage } from "@stencil/core/testing";
import { BpSdkFilterApplicationFormStateContainer } from "./bp-sdk-filter-application-form-state-container";
const fakeStore = {
    application: {
        form: {
            state: ApplicationFormStates.Completed,
        },
    },
};
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-filter-application-form-state-container></bp-sdk-filter-application-form-state-container>',
        components: [BpSdkFilterApplicationFormStateContainer],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    instance.targetState = ApplicationFormStates.Completed;
    return instance;
};
describe('bp-sdk-filter-application-form-state-container', () => {
    it('renders', async () => {
        const page = await setupPage();
        // TODO i think something is going on here setting the initial state
        page.rootInstance.state = undefined;
        await page.waitForChanges();
        const element = page.root.querySelector('div');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-filter-application-form-state-container')).toBeFalsy();
    });
    it('renders external component if defined', async () => {
        const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
        const page = await setupPage();
        await page.waitForChanges();
        expect(page.root.querySelector('bp-sdk-external-filter-application-form-state-container')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    describe('handleSubscriptions', () => {
        it('calls unsubscribe if subscribed', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const unsubscribe = jest.fn();
            instance.storeUnsubscribe = unsubscribe;
            instance.handleSubscriptions();
            expect(unsubscribe).toHaveBeenCalled();
        });
        it('overwrites an existing subscribe function', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const unsubscribe = jest.fn();
            instance.storeUnsubscribe = unsubscribe;
            instance.handleSubscriptions();
            expect(instance.storeUnsubscribe).not.toEqual(unsubscribe);
            expect(instance.storeUnsubscribe).toBeDefined();
        });
        it('subscribes if not subscribed', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.storeUnsubscribe = null;
            instance.handleSubscriptions();
            expect(instance.storeUnsubscribe).not.toBeNull();
        });
        it('subscribes updates mapStateToProps if already has storeUnsubscribe', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const unsubscribe = jest.fn();
            instance.storeUnsubscribe = unsubscribe;
            instance.handleSubscriptions();
            expect(instance.storeUnsubscribe).not.toEqual(unsubscribe);
        });
    });
    describe('mapStateToProps', () => {
        it('returns expected', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const result = instance.mapStateToProps(fakeStore);
            expect(result.state).toEqual(fakeStore.application.form.state);
        });
    });
});
