import { newSpecPage } from "@stencil/core/testing";
import { BpSdkBusinessClassificationSelectorListItemContainer } from "./bp-sdk-business-classification-selector-list-item-container";
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-business-classification-selector-list-item-container></bp-sdk-business-classification-selector-list-item-container>',
        components: [BpSdkBusinessClassificationSelectorListItemContainer],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    return instance;
};
describe('bp-sdk-business-classification-selector-list-item-container', () => {
    it('renders', async () => {
        const page = await setupPage();
        await page.waitForChanges();
        const element = page.root.querySelector('bp-sdk-business-classification-selector-list-item');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-business-classification-selector-list-item-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-business-classification-selector-list-item-container')).toBeTruthy();
        whenDefinedMock.mockRestore();
    });
    describe('disconnectedCallback', () => {
        it('makes expected calls', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            instance.storeUnsubscribe = jest.fn();
            instance.disconnectedCallback();
            expect(instance.storeUnsubscribe).toBeCalledTimes(1);
        });
    });
    describe('mapStateToProps', () => {
        const mockState = {
            application: {
                form: {
                    locked: true,
                    owner_id: '4',
                },
            },
            auth: { userId: '4' },
            config: {
                options: { enableDetailedRelevanceChips: true, enableUserApplicationGuard: true },
            },
        };
        it('returns showDetailedRelevanceChip', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const result = instance.mapStateToProps(mockState);
            expect(result.showDetailedRelevanceChip).toBe(true);
        });
        it('returns enableUserApplicationGuard', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const result = instance.mapStateToProps(mockState);
            expect(result.enableUserApplicationGuard).toBe(true);
        });
        it('returns locked', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const result = instance.mapStateToProps(mockState);
            expect(result.locked).toBe(true);
        });
        it('returns isOwner', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const result = instance.mapStateToProps(mockState);
            expect(result.isOwner).toBe(true);
        });
        it('returns hasPermissionToEdit', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const result = instance.mapStateToProps(mockState);
            expect(result.hasPermissionToEdit).toBe(false);
        });
    });
    describe('updateIsLocked', () => {
        let instance;
        beforeEach(async () => {
            const page = await setupPage();
            instance = setupInstance(page);
        });
        it.each([
            { locked: true, isOwner: false, hasPermission: false, enableUserApplicationGuard: true },
            { locked: true, isOwner: true, hasPermission: false, enableUserApplicationGuard: true },
            { locked: true, isOwner: false, hasPermission: true, enableUserApplicationGuard: true },
            { locked: true, isOwner: true, hasPermission: true, enableUserApplicationGuard: false },
            { locked: true, isOwner: true, hasPermission: true, enableUserApplicationGuard: true },
        ])('should be locked whenever locked is true', ({ locked, isOwner, hasPermission, enableUserApplicationGuard }) => {
            instance.updateIsLocked(locked, isOwner, hasPermission, enableUserApplicationGuard);
            expect(instance.isLocked).toBe(true);
        });
        it.each([
            { locked: false, isOwner: false, hasPermission: true, enableUserApplicationGuard: true },
            { locked: false, isOwner: false, hasPermission: false, enableUserApplicationGuard: true },
        ])('should be locked when enableUserApplicationGuard is true and isOwner is false', ({ locked, isOwner, hasPermission, enableUserApplicationGuard }) => {
            instance.updateIsLocked(locked, isOwner, hasPermission, enableUserApplicationGuard);
            expect(instance.isLocked).toBe(true);
        });
        it.each([
            { locked: true, isOwner: false, hasPermission: false, enableUserApplicationGuard: true },
            { locked: true, isOwner: true, hasPermission: false, enableUserApplicationGuard: true },
            { locked: false, isOwner: false, hasPermission: false, enableUserApplicationGuard: true },
            { locked: false, isOwner: true, hasPermission: false, enableUserApplicationGuard: true },
        ])('should be locked when enableUserApplicationGuard is true and hasPermission is false', ({ locked, isOwner, hasPermission, enableUserApplicationGuard }) => {
            instance.updateIsLocked(locked, isOwner, hasPermission, enableUserApplicationGuard);
            expect(instance.isLocked).toBe(true);
        });
        it('should not be locked when not locked and isOwner and hasPermission are true', () => {
            const locked = false;
            const isOwner = true;
            const hasPermission = true;
            const enableUserApplicationGuard = true;
            instance.updateIsLocked(locked, isOwner, hasPermission, enableUserApplicationGuard);
            expect(instance.isLocked).toBe(false);
        });
    });
    describe('onLocked', () => {
        it('should call through to updateIsLocked', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'updateIsLocked');
            instance.isOwner = true;
            instance.hasPermissionToEdit = false;
            instance.enableUserApplicationGuard = true;
            instance.onLocked(false);
            expect(instance.updateIsLocked).toHaveBeenCalledWith(false, instance.isOwner, instance.hasPermissionToEdit, instance.enableUserApplicationGuard);
        });
    });
    describe('onEnableUserApplicationGuard', () => {
        it('should call through to updateIsLocked', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'updateIsLocked');
            instance.locked = false;
            instance.isOwner = true;
            instance.hasPermissionToEdit = false;
            instance.onEnableUserApplicationGuard(true);
            expect(instance.updateIsLocked).toHaveBeenCalledWith(instance.locked, instance.isOwner, instance.hasPermissionToEdit, true);
        });
    });
    describe('onHasPermissionToEdit', () => {
        it('should call through to updateIsLocked', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'updateIsLocked');
            instance.locked = false;
            instance.isOwner = true;
            instance.enableUserApplicationGuard = true;
            instance.onHasPermissionToEdit(true);
            expect(instance.updateIsLocked).toHaveBeenCalledWith(instance.locked, instance.isOwner, true, instance.enableUserApplicationGuard);
        });
    });
    describe('onIsOwner', () => {
        it('should call through to updateIsLocked', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance, 'updateIsLocked');
            instance.locked = false;
            instance.hasPermissionToEdit = false;
            instance.enableUserApplicationGuard = true;
            instance.onIsOwner(true);
            expect(instance.updateIsLocked).toHaveBeenCalledWith(instance.locked, true, instance.hasPermissionToEdit, instance.enableUserApplicationGuard);
        });
    });
    describe('handleSubscriptions', () => {
        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('resubscribes to 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);
        });
    });
});
