import '../../../../../__mocks__/matchMedia';
import mountWithState from '../../../../../Utils/Test/mountWithState';
import ConfigurationModeSwitch from '../ConfigurationModeSwitch';
import ServiceLocator from '../../../../ServiceLocator';

jest.mock('../../../../Reducers/ConfigurationMode/Actions');
import { selectConfigurationMode } from '../../../../Reducers/ConfigurationMode/Actions';

describe('Screens/Configurator/Containers/ConfigurationModeSwitch integration tests', () => {
    beforeEach(() => {
        selectConfigurationMode.mockReset();
    });
    it('calls ConfiguratorService.validateConfiguration when switching to designer, switches if valid', async () => {
        const validationPromise = Promise.resolve();
        const configuratorSrvc = new (class {
            static serviceName = 'ConfiguratorService';
            validateConfiguration = jest.fn(() => validationPromise);
        })();
        const analyticsSrvc = new (class {
            static serviceName = 'AnalyticsService';
            triggerEvent = jest.fn();
        })();
        const storeSrvc = new (class {
            static serviceName = 'StoreService';
            observeStore() {}

            get state() {
                return {
                    configurationMode: {
                        availableModes: {
                            creator: true,
                            designer: true,
                        },
                        selectedMode: 'creator',
                    },
                    device: {
                        screen: {},
                    },
                    configurator: {
                        validation: {
                            success: true,
                        },
                    },
                };
            }
        })();
        ServiceLocator.provide(storeSrvc);
        ServiceLocator.provide(configuratorSrvc);
        ServiceLocator.provide(analyticsSrvc);
        const { wrapper } = mountWithState(ConfigurationModeSwitch, storeSrvc.state, {});
        wrapper.find('button').simulate('click');
        await validationPromise;
        expect(configuratorSrvc.validateConfiguration).toHaveBeenCalled();
        expect(selectConfigurationMode).toHaveBeenLastCalledWith('designer');
    });

    it('calls ConfiguratorService.validateConfiguration when switching to designer, does not switch if invalid', async () => {
        const validationPromise = Promise.resolve();
        const configuratorSrvc = new (class {
            static serviceName = 'ConfiguratorService';
            validateConfiguration = jest.fn(() => validationPromise);
        })();
        const analyticsSrvc = new (class {
            static serviceName = 'AnalyticsService';
            triggerEvent = jest.fn();
        })();
        const storeSrvc = new (class {
            static serviceName = 'StoreService';
            observeStore() {}

            get state() {
                return {
                    configurationMode: {
                        availableModes: {
                            creator: true,
                            designer: true,
                        },
                        selectedMode: 'creator',
                    },
                    device: {
                        screen: {},
                    },
                    configurator: {
                        validation: {
                            success: false,
                        },
                    },
                };
            }
        })();
        ServiceLocator.provide(storeSrvc);
        ServiceLocator.provide(configuratorSrvc);
        ServiceLocator.provide(analyticsSrvc);
        const { wrapper } = mountWithState(ConfigurationModeSwitch, storeSrvc.state, {});
        wrapper.find('button').simulate('click');
        await validationPromise;
        expect(configuratorSrvc.validateConfiguration).toHaveBeenCalled();
        expect(selectConfigurationMode).not.toHaveBeenLastCalledWith('designer');
    });

    it('does not call ConfiguratorService.validateConfiguration when switching to creator', async () => {
        const validationPromise = Promise.resolve();
        const configuratorSrvc = new (class {
            static serviceName = 'ConfiguratorService';
            validateConfiguration = jest.fn(() => validationPromise);
        })();
        const analyticsSrvc = new (class {
            static serviceName = 'AnalyticsService';
            triggerEvent = jest.fn();
        })();
        const storeSrvc = new (class {
            static serviceName = 'StoreService';
            observeStore() {}

            get state() {
                return {
                    configurationMode: {
                        availableModes: {
                            creator: true,
                            designer: true,
                        },
                        selectedMode: 'designer',
                    },
                    device: {
                        screen: {},
                    },
                    configurator: {
                        validation: {
                            success: false,
                        },
                    },
                };
            }
        })();
        ServiceLocator.provide(storeSrvc);
        ServiceLocator.provide(configuratorSrvc);
        ServiceLocator.provide(analyticsSrvc);
        const { wrapper } = mountWithState(ConfigurationModeSwitch, storeSrvc.state, {});
        wrapper.find('button').simulate('click');
        expect(configuratorSrvc.validateConfiguration).not.toHaveBeenCalled();
        expect(selectConfigurationMode).toHaveBeenLastCalledWith('creator');
    });
});
