import React from 'react';
import { mountWithIntl } from "../../../../helpers/intl-enzym-test-helpers";
import { SmsTraiCreate } from '../index';
import { SAMPLE_CSV } from '../constants';
import { mockData } from './mockData';

let file = [];
const renderHelper = (fileData) => {
  file = new File(mockData.contents, fileData.name, {
    type: fileData.type,
  });
};

jest.mock('papaparse', () => ({
  parse: (file, response) => {
    response.complete({ data: mockData.contents }, mockData.meta);
  },
}));

describe('Creatives SmsTraiCreate test/>', () => {
  let renderedComponent;
  const clearCreateResponse = jest.fn();
  const createSMSTRAITemplates = jest.fn();
  const checkDuplicatesInDb = jest.fn();
  const doneCheckingDuplicates = jest.fn();
  const onCreateComplete = jest.fn();
  const onShowTemplates = jest.fn();
  const setAttribute = jest.fn();
  const {traiSms} = mockData;
  const setup = (props) => (
    <SmsTraiCreate
      actions={{
        clearCreateResponse, createSMSTRAITemplates, checkDuplicatesInDb, doneCheckingDuplicates,
      }}
      onCreateComplete={onCreateComplete}
      onShowTemplates={onShowTemplates}
      traiSms={traiSms}
      {...props} />
  );
  beforeEach(() => {
    renderedComponent = mountWithIntl(
      setup()
    );
  });

  it('renders UI', () => {
    expect(renderedComponent).toMatchSnapshot();
  });

  it('clicking on Sample CSV should download sample CSV file', () => {
    const event = { target: { setAttribute } };
    renderedComponent.find('.download-sample-link').props().onClick(event);
    expect(setAttribute).toHaveBeenCalledWith('download', SAMPLE_CSV);
  });

  it('done button should be disabled initially', () => {
    const buttonProps = renderedComponent.find('button.done-button').props();
    expect(buttonProps.disabled).toBe(true);
  });

  it('invalid file drop', async () => {
    renderHelper(mockData.invalidFile);
    const dropZone = renderedComponent.find('Dropzone').props();
    await dropZone.onDrop([file]);
    renderedComponent.update();
    expect(renderedComponent).toMatchSnapshot();
    expect(renderedComponent.find('button.done-button').props().disabled).toBe(
      true,
    );
  });

  it('valid file drop', async () => {
    const event = { target: { setAttribute } };
    createSMSTRAITemplates.mockImplementation((obj, callback) => {
      callback();
    });
    renderHelper(mockData.validFile);
    const dropZone = renderedComponent.find('Dropzone').props();
    await dropZone.onDrop([file]);
    renderedComponent.update();
    expect(renderedComponent).toMatchSnapshot();
  });

  it('done button should be disabled if blank data is passed and orginal state should be maintained', async () => {
    renderHelper(mockData.checkDuplicateDoneData);
    const dropZone = renderedComponent.find('Dropzone').props();
    await dropZone.onDrop([file]);
    renderedComponent.update();
    expect(renderedComponent).toMatchSnapshot();
    expect(renderedComponent.find('button.done-button').props().disabled).toBe(
      true,
    );
  });
});
