import { h } from "@stencil/core";
import { newSpecPage } from "@stencil/core/testing";
import { BpSdkQueueContainer } from "./bp-sdk-queue-container";
const setupPage = async () => {
    return await newSpecPage({
        components: [BpSdkQueueContainer],
        template: () => h("bp-sdk-queue-container", { queue: [] }),
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    return instance;
};
describe('bp-sdk-queue-container', () => {
    beforeEach(() => {
        jest.restoreAllMocks();
        jest.useFakeTimers({ legacyFakeTimers: true });
    });
    it('exists', async () => {
        const page = await setupPage();
        await page.waitForChanges();
        expect(page.rootInstance).toBeTruthy();
    });
    it('renders nothing inside of it', async () => {
        const page = await setupPage();
        expect(page.root).toEqualHtml('<bp-sdk-queue-container></bp-sdk-queue-container>');
    });
    describe('connectedCallback', () => {
        it('makes expected calls', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.resetState = jest.fn();
            instance.handleSubscriptions = jest.fn();
            instance.connectedCallback();
            expect(instance.resetState).toHaveBeenCalled();
            expect(instance.handleSubscriptions).toHaveBeenCalled();
        });
    });
    describe('disconnectedCallback', () => {
        it('calls stopHeartbeat when heartbeatInterval is defined', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.stopHeartbeat = jest.fn();
            instance.heartbeatInterval = (() => { });
            instance.disconnectedCallback();
            expect(instance.stopHeartbeat).toHaveBeenCalled();
        });
        it('does not call stopHeartbeat when heartbeatInterval is undefined', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.stopHeartbeat = jest.fn();
            instance.heartbeatInterval = undefined;
            instance.disconnectedCallback();
            expect(instance.stopHeartbeat).not.toHaveBeenCalled();
        });
    });
    describe('componentWillLoad', () => {
        it('calls onQueueChange', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.onQueueChange = jest.fn();
            instance.componentWillLoad();
            expect(instance.onQueueChange).toHaveBeenCalled();
        });
    });
    describe('handleSubscriptions', () => {
        it('calls unsubscribe when it is defined', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const storeUnsubMock = jest.fn();
            instance.storeUnsubscribe = storeUnsubMock;
            instance.handleSubscriptions();
            expect(storeUnsubMock).toHaveBeenCalled();
        });
        it('assigns a new unsubscribe', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const storeUnsubMock = jest.fn();
            instance.storeUnsubscribe = storeUnsubMock;
            instance.handleSubscriptions();
            expect(instance.storeUnsubscribe).not.toEqual(storeUnsubMock);
        });
    });
    describe('mapStateToProps', () => {
        it('returns an object with queue', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const state = {
                queues: {
                    answers: {
                        queue: [],
                    },
                },
            };
            expect(instance.mapStateToProps(state)).toEqual({ queue: state.queues.answers.queue });
        });
    });
    describe('onQueueChange', () => {
        it('does nothing when queue is undefined', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.heartbeatInterval = undefined;
            instance.onQueueChange(undefined);
            expect(instance.heartbeatInterval).toBeUndefined();
        });
        it('does nothing when queue has length and oldQueue has length', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.heartbeatInterval = undefined;
            instance.onQueueChange([{}], [{}]);
            expect(instance.heartbeatInterval).toBeUndefined();
        });
        it('assigns a new interval when queue has length and oldQueue does not', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.heartbeatInterval = undefined;
            instance.onQueueChange([{}]);
            expect(instance.heartbeatInterval).toBeDefined();
            expect(setTimeout).toHaveBeenCalled();
        });
        it('calls stopHeartbeat when queue gets emptied', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.heartbeatInterval = (() => { });
            instance.stopHeartbeat = jest.fn();
            instance.onQueueChange([], [{}]);
            expect(instance.stopHeartbeat).toHaveBeenCalled();
        });
    });
    describe('stopHeartbeat', () => {
        it('clears the interval', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.heartbeatInterval = (() => { });
            instance.stopHeartbeat();
            expect(instance.heartbeatInterval).toBeUndefined();
        });
    });
});
