import AnalyticsService, { actionsToTrack } from '../AnalyticsService';
import ServiceLocator from '../../ServiceLocator';
import { EventTypes } from 'App/Constant';

describe('Services/AnalyticsService', () => {
    class StoreService {
        static serviceName = 'StoreService';

        observeStore() {}

        get state() {
            return {
                configurator: {
                    configuration: {
                        item: {
                            identifier: 'test_identifier',
                        },
                    },
                },
            };
        }

        dispatch = jest.fn();
    }

    class ConfiguratorService {
        static serviceName = 'ConfiguratorService';

        state = {
            loadIdentifier: 'softshell_creator_2d',
        };
    }

    // add the mock services
    ServiceLocator.provide(new StoreService());
    ServiceLocator.provide(new ConfiguratorService());

    const mockDataLayer = {
        push: jest.fn(),
    };

    test('Event load_request pushes the correct ga data to the dataLayer', () => {
        const analyticsSrvc = new AnalyticsService();
        analyticsSrvc.setDataLayer(mockDataLayer);
        analyticsSrvc.triggerEvent(EventTypes.load_request, 'softshell_designer_2d');
        expect(mockDataLayer.push).toHaveBeenNthCalledWith(1, {
            event: 'load_item',
            item_identifier: 'softshell_creator_2d',
        });
        expect(mockDataLayer.push).toHaveBeenNthCalledWith(2, {
            event: 'header_load_configuration_request',
            configuration_code: 'softshell_designer_2d',
        });
    });

    test('Event widget_open_configurator dispatches the correct action and test result', () => {
        const type = EventTypes.widget_open_configurator;
        const mockAction = { type };
        const mockState = {
            configurator: {
                calculation: {
                    positions: [
                        {
                            title: 'CHALLENGER Clear',
                            selecteableByUser: false,
                        },
                        {
                            title: 'Grundkosten Schaftdruck 360°',
                            selecteableByUser: true,
                            selected: true,
                        },
                    ],
                },
                configuration: {
                    customdata: {
                        selectedAmounts: { 'S-012192103920': 500 },
                    },
                    designdata: {
                        a_1: {
                            canvasData: null,
                            designProductionMethodIdentifier: 'clipdruck_tampon',
                            colorAmount: 1,
                        },
                        d: {
                            canvasData: { objects: [{ text: 'abc' }] },
                            colorAmount: 1,
                            designProductionMethodIdentifier: 'schaftdruck_360',
                        },
                    },
                },
            },
        };
        const result = actionsToTrack[type](mockAction, mockState);
        const expected = {
            event: 'calculation_widget_open_configurator',
            selected_amount: {
                'S-012192103920': 500,
            },
            selected_options: [],
            selected_design_areas: ['d'],
            selected_design_production_methods: ['schaftdruck_360'],
        };
        expect(result).toEqual(expected);
    });

    test('Event add_to_cart dispatches the correct action and test result', () => {
        const type = EventTypes.add_to_cart;
        const mockAction = { type };
        const mockState = {
            configurationMode: {
                selectedMode: 'designer',
            },
            configurator: {
                configuration: {
                    customdata: {
                        selectedAmounts: { 'S-012192103920': 500 },
                    },
                    designdata: {
                        a_1: {
                            canvasData: null,
                            designProductionMethodIdentifier: 'clipdruck_tampon',
                            colorAmount: 1,
                        },
                        d: {
                            canvasData: { objects: [] },
                            colorAmount: 1,
                            designProductionMethodIdentifier: 'schaftdruck_360',
                        },
                    },
                },
            },
        };
        const result = actionsToTrack[type](mockAction, mockState);
        const expected = {
            event: 'accept_design_open_dialog',
            selected_amounts: {
                'S-012192103920': 500,
            },
        };
        expect(result).toEqual(expected);
    });
});
