/* eslint-env jest */
import React from 'react';
import { mount, render } from 'enzyme/build';
import AnalyticsWrapper from './AnalyticsWrapper';
import ServiceLocator from '../../../ServiceLocator';
import Button from '@material-ui/core/Button';
import Select from '@material-ui/core/Select';

it('the wrapper component does not modify the original output', () => {
    const clickFunc = jest.fn();
    const tree = render(
        <AnalyticsWrapper targetId="widget_print_view">
            <Button onClick={clickFunc} variant="outlined" size="large">
                Test
            </Button>
        </AnalyticsWrapper>
    );
    expect(tree).toMatchSnapshot();
});

it('calls triggerEvent onclick of child element', () => {
    const analyticsSrvc = new (class {
        static serviceName = 'AnalyticsService';
        triggerEvent = jest.fn();
    })();
    const clickFunc = jest.fn();

    ServiceLocator.provide(analyticsSrvc);
    const wrapper = mount(
        <AnalyticsWrapper targetId="widget_print_view">
            <Button
                role="button"
                onClick={clickFunc}
                className="child_element"
                variant="outlined"
                size="large"
            >
                Test
            </Button>
        </AnalyticsWrapper>
    );
    const button = wrapper.find(Button).get(0);
    button.props.onClick();
    expect(analyticsSrvc.triggerEvent).toHaveBeenCalled();
});

it('calls triggerEvent onchange of child element', () => {
    const analyticsSrvc = new (class {
        static serviceName = 'AnalyticsService';
        triggerEvent = jest.fn();
    })();
    const changeFunc = jest.fn();

    ServiceLocator.provide(analyticsSrvc);
    const wrapper = mount(
        <AnalyticsWrapper targetId="widget_print_view">
            <Select
                value="selected_value"
                onChange={changeFunc}
                className="child_element"
                variant="outlined"
                size="large"
            />
        </AnalyticsWrapper>
    );
    const select = wrapper.find(Select).get(0);
    select.props.onChange();
    expect(analyticsSrvc.triggerEvent).toHaveBeenCalled();
});
