import React from 'react';
import Immutable from 'immutable';
import { shallow, mount } from 'enzyme';
import Rainbow from './rainbow.component';
import { rootTagTest } from '../../utils/helpers/tags/tags-specs/tags-specs';

/* global jest */

describe('Rainbow', () => {
  global.Highcharts = {
    chart() {
      return {
        setTitle() {},
        series: [
          { setData() {} }
        ]
      };
    }
  };

  let wrapper, data, chart;

  function render(props = {}) {
    wrapper = mount(
      <Rainbow
        title='My Title' data={ data }
        { ...props }
      />
    );
  }

  beforeEach(() => {
    data = Immutable.fromJS([
      {
        y: 30,
        name: 'First Bit',
        label: 'label for first bit',
        tooltip: 'more info about this bit'
      }, {
        y: 70,
        name: 'Second Bit',
        label: 'label for second bit',
        tooltip: 'more info about this bit'
      }
    ]);
    render();

    const instance = wrapper.instance();
    chart = instance._chart;
  });

  describe('shouldComponentUpdate', () => {
    beforeEach(() => {
      chart.setTitle = jest.fn();
      chart.series[0].setData = jest.fn();
    });

    describe('when title changes', () => {
      test('calls setTitle', () => {
        wrapper.setProps({ title: 'different title', data: wrapper.props().data });
        expect(chart.setTitle).toHaveBeenCalledWith({ text: 'different title' });
      });
    });

    describe('when data changes', () => {
      it('calls setData', () => {
        const newData = Immutable.fromJS({ foo: 'bar' });
        wrapper.setProps({ title: wrapper.props().title, data: newData });
        expect(chart.series[0].setData).toHaveBeenCalledWith(newData.toJS());
      });
    });

    describe('when nothing has changed', () => {
      it('calls neither set methods', () => {
        const theProps = wrapper.props();
        wrapper.setProps({ title: theProps.title, data: theProps.data });
        expect(chart.setTitle).not.toHaveBeenCalled();
        expect(chart.series[0].setData).not.toHaveBeenCalled();
      });
    });
  });

  describe('mainClasses', () => {
    describe('when a custom class is passed', () => {
      it('returns base and custom class', () => {
        render({ className: 'customClass' });
        expect(wrapper.instance().mainClasses).toEqual('customClass');
      });
    });
  });

  describe('tags on component', () => {
    const wrapperWithTags = shallow(
      <Rainbow
        data-element='bar'
        data-role='baz'
        data={ Immutable.fromJS({}) }
      />
    );

    it('include correct component, element and role data tags', () => {
      rootTagTest(wrapperWithTags, 'rainbow', 'bar', 'baz');
    });
  });
});

@@ -203,27 +203,16 @@
 gs-specs
-/tags-specs
 ';%0A%0A/* g

remove double import