import { newSpecPage } from "@stencil/core/testing";
import { BpSdkBusinessClassificationSelectorListItem } from "./bp-sdk-business-classification-selector-list-item";
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-business-classification-selector-list-item></bp-sdk-business-classification-selector-list-item>',
        components: [BpSdkBusinessClassificationSelectorListItem],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    return instance;
};
describe('bp-sdk-business-classification-selector-list-item', () => {
    it('renders', async () => {
        const page = await setupPage();
        await page.waitForChanges();
        const element = page.root.querySelector('div');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-business-classification-selector-list-item')).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-business-classification-selector-list-item')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    describe('onSelection', () => {
        let evt;
        beforeEach(() => {
            evt = {
                preventDefault: jest.fn(),
            };
        });
        it('should make expected calls', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance.valueUpdate, 'emit').mockImplementation(() => ({}));
            instance.choiceId = 'bob';
            instance.locked = false;
            instance.onSelection(evt);
            expect(instance.valueUpdate.emit).toBeCalledTimes(1);
            expect(instance.valueUpdate.emit).toBeCalledWith([instance.choiceId]);
        });
        it('should not emit valueUpdate when locked is true', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance.valueUpdate, 'emit').mockImplementation(() => ({}));
            instance.choiceId = 'bob';
            instance.locked = true;
            instance.onSelection(evt);
            expect(instance.valueUpdate.emit).toBeCalledTimes(0);
        });
    });
    describe('onKeyPress', () => {
        let evt;
        beforeEach(() => {
            evt = {
                preventDefault: jest.fn(),
                key: '',
            };
        });
        it('should call onSelection when key is Enter key', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            evt.key = 'Enter';
            instance.onSelection = jest.fn();
            instance.onKeyPress(evt);
            expect(instance.onSelection).toHaveBeenCalledWith(evt);
        });
        it('should call onSelection when key is space key', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            evt.key = ' ';
            instance.onSelection = jest.fn();
            instance.onKeyPress(evt);
            expect(instance.onSelection).toHaveBeenCalledWith(evt);
        });
        it('should not call onSelection for any other key', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            evt.key = 'Shift';
            instance.onSelection = jest.fn();
            instance.onKeyPress(evt);
            expect(instance.onSelection).not.toHaveBeenCalled();
        });
    });
});
