import { newSpecPage } from "@stencil/core/testing";
import { BpSdkParentQuestionPoolSelector } from "./bp-sdk-parent-question-pool-selector";
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-parent-question-pool-selector></bp-sdk-parent-question-pool-selector>',
        components: [BpSdkParentQuestionPoolSelector],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    return instance;
};
describe('bp-sdk-parent-question-pool-selector', () => {
    const choices = [
        { id: '1', value: 'Driver 1', created_at: '', updated_at: '', alias: undefined, order: 0, question_id: '1' },
        { id: '2', value: 'Driver 2', created_at: '', updated_at: '', alias: undefined, order: 0, question_id: '1' },
    ];
    beforeEach(() => {
        global['FocusEvent'] = Event;
    });
    afterEach(() => {
        global['FocusEvent'] = undefined;
    });
    it('renders', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.choices = choices;
        await page.waitForChanges();
        expect(page.body.querySelector('bp-sdk-select-input')).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-parent-question-pool-selector')).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-parent-question-pool-selector')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    it('emits selectedAnswerPoolId on submit if choice exists', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.choices = choices;
        instance.selectedChoiceId = choices[0].id;
        const spy = jest.spyOn(instance.selectedAnswerPoolId, 'emit');
        await page.waitForChanges();
        const button = page.root.querySelector('.bp-sdk-parent-pool-selector__submit-button');
        button.click();
        expect(spy).toHaveBeenCalledWith(choices[0].id);
    });
    it('resets selected choice on submit', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.choices = choices;
        instance.selectedChoiceId = choices[0].id;
        await page.waitForChanges();
        const button = page.root.querySelector('.bp-sdk-parent-pool-selector__submit-button');
        button.click();
        expect(instance.selectedChoiceId).toBe(undefined);
    });
    it('selectValueUpdate stops value update event from bubbling to form', async () => {
        const ev = { stopPropagation: jest.fn(), detail: ['foobar'] };
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.selectValueUpdate(ev);
        expect(ev.stopPropagation).toHaveBeenCalled();
    });
});
