import React from 'react';
// import { browserHistory } from 'react-router';
import { Provider } from 'react-redux';
import { configureStore }  from '@capillarytech/vulcan-react-sdk/utils';
import history from '../../../../utils/history';
import { initialReducer } from '../../../../initialReducer';
import { mountWithIntl } from '../../../../helpers/intl-enzym-test-helpers';
import { LineContainer } from '../index';
import { mockData } from './mockData';

let store;
beforeAll(() => {
  store = configureStore({}, initialReducer, history);
});

jest.mock('../Wrapper', () => ({
  __esModule: true,
  default: (props) => (
    <div className="LineWrapper-mock" {...props}>
      LineWrapper
    </div>
  ),
}));

describe('line wrapper test/>', () => {
  let renderedComponent;
  const formatMessage = jest.fn();
  const getWeCrmAccounts = jest.fn();
  const getAllTemplates = jest.fn();
  const setChannelAccount = jest.fn();
  const handleClose = jest.fn();
  const onShowTemplates = jest.fn();
  const onCreateComplete = jest.fn();
  const getFormSubscriptionData = jest.fn();
  const getTemplateDetails = jest.fn();
  const clearEditResponse = jest.fn();
  const createTemplate = jest.fn();
  const editTemplate = jest.fn();
  const renderHelper = (Line, Templates, templateData, params, isFullMode, selectedOfferDetails) => {
    renderedComponent = mountWithIntl(
      <Provider store={store}>
        <LineContainer
          intl={{ formatMessage }}
          isFullMode={isFullMode}
          Line={Line}
          onShowTemplates={onShowTemplates}
          handleClose={handleClose}
          onCreateComplete={onCreateComplete}
          getFormSubscriptionData={getFormSubscriptionData}
          Templates={Templates}
          lineData={Templates}
          templateData={templateData}
          params={params}
          selectedOfferDetails={selectedOfferDetails}
          templateActions={{
            getWeCrmAccounts,
            getAllTemplates,
            setChannelAccount,
          }}
          actions={{
            getTemplateDetails,
            createTemplate,
            clearEditResponse,
            editTemplate,
          }}
        />
      </Provider>,
    );
  };

  it('should render line component new', () => {
    editTemplate.mockImplementation((obj, callback) => {
      callback({
        templateId: {
          _id: '',
        },
      });
    });
    const { Line1, Templates1, templateData1, params1, selectedOfferDetails } = mockData;
    renderHelper(Line1, Templates1, templateData1, params1, true);
    expect(renderedComponent).toMatchSnapshot();
    renderedComponent.find('CapButton.create-msg').simulate('click');
    expect(renderedComponent).toMatchSnapshot();
    renderHelper(Line1, Templates1, templateData1, params1, false);
    expect(renderedComponent).toMatchSnapshot();
    renderHelper(Line1, Templates1, templateData1, params1, false, selectedOfferDetails);
    expect(renderedComponent).toMatchSnapshot();
    renderedComponent.find('CapButton.create-msg').simulate('click');
    expect(renderedComponent).toMatchSnapshot();
  });

  it('should render line component old', () => {
    editTemplate.mockImplementation((obj, callback) => {
      callback({
        templateId: {
          _id: '',
        },
      });
    });
    const { Line2, Templates2, templateData2, params2 } = mockData;
    renderHelper(Line2, Templates2, templateData2, params2, true);
    expect(renderedComponent).toMatchSnapshot();
    renderedComponent.find('CapButton.create-msg').simulate('click');
    expect(renderedComponent).toMatchSnapshot();
    renderHelper(Line2, Templates2, templateData2, params2, false);
    expect(renderedComponent).toMatchSnapshot();
    renderedComponent.find('CapButton.create-msg').simulate('click');
    expect(renderedComponent).toMatchSnapshot();
  });
});
