import { expect } from 'chai';
import { shallow } from 'enzyme';
import sinon from 'sinon';
import Immutable from 'immutable';
import { ArticlesPage } from './../../../../src/containers/pages/ArticlesPage/ArticlesPage';
import { ZERO, ONE } from './../../../../src/constants/Constants';

const meta = 'data:text/plain;charset=utf-8,';

const mockLoadEntities = () => ({ then: () => null });

describe('Articles page tests', () => {
  let sandbox; //eslint-disable-line

  beforeEach(() => {
    sandbox = sinon.sandbox.create(); //eslint-disable-line
  });

  afterEach(() => {
    sandbox.restore();
  });

  it('Table is empty by default', () => {
    const wrapper = shallow(
      <ArticlesPage
        articles={Immutable.fromJS([])}
        loadEntities={mockLoadEntities}
      />
    );

    expect(wrapper.find('td')).to.have.length(ZERO);
  });

  it('Should save selected articles', () => {
    const setAttributeSpy = sandbox.spy();
    const clickSpy = sandbox.spy();

    sandbox.stub(document, 'createElement').withArgs('a').returns({
      setAttribute: setAttributeSpy,
      dispatchEvent: clickSpy,
    });

    const data = [
      {
        url: 'http://example.com/',
        title: 'Example Domain',
        timestamp: '2016-01-01T05:06:07',
        read: null,
      },
      {
        url: 'https://github.com/',
        title: 'How people build software',
        timestamp: '2016-01-02T05:06:08',
        read: '2016-01-03T05:06:08',
      },
      {
        url: 'https://www.google.com.ua',
      },
    ];

    const wrapper = shallow(
      <ArticlesPage
        articles={Immutable.fromJS(data)}
        loadEntities={mockLoadEntities}
      />
    );

    wrapper.setState({ selectedRows: [ONE] });

    wrapper.instance().handleOnExport();

    expect(setAttributeSpy.getCall(ZERO).args).to.deep.equal([
      'href',
      `${meta}${encodeURIComponent(JSON.stringify([data[ONE]]))}`,
    ]);
    expect(clickSpy.called).to.equal(true);
  });

  it('Should save all articles if all articles are selecteled', () => {
    const setAttributeSpy = sandbox.spy();
    const clickSpy = sandbox.spy();

    sandbox.stub(document, 'createElement').withArgs('a').returns({
      setAttribute: setAttributeSpy,
      dispatchEvent: clickSpy,
    });

    const data = [
      {
        url: 'http://example.com/',
        title: 'Example Domain',
        timestamp: '2016-01-01T05:06:07',
        read: null,
      },
      {
        url: 'https://github.com/',
        title: 'How people build software',
        timestamp: '2016-01-02T05:06:08',
        read: '2016-01-02T09:06:08',
      },
      {
        url: 'https://www.google.com.ua',
      },
    ];

    const wrapper = shallow(
      <ArticlesPage
        articles={Immutable.fromJS(data)}
        loadEntities={mockLoadEntities}
      />);

    wrapper.setState({ selectedRows: 'all' });

    wrapper.instance().handleOnExport();

    expect(setAttributeSpy.getCall(ZERO).args).to.deep.equal([
      'href',
      `${meta}${encodeURIComponent(JSON.stringify(data))}`,
    ]);
    expect(clickSpy.called).to.equal(true);
  });

  it('Shouldn\'t save articles if there are no selected articles', () => {
    const setAttributeSpy = sandbox.spy();
    const clickSpy = sandbox.spy();

    sandbox.stub(document, 'createElement').withArgs('a').returns({
      setAttribute: setAttributeSpy,
      dispatchEvent: clickSpy,
    });

    const data = [
      {
        url: 'http://example.com/',
        title: 'Example Domain',
        timestamp: '2016-01-01T05:06:07',
        read: null,
      },
      {
        url: 'https://github.com/',
        title: 'How people build software',
        timestamp: '2016-01-02T05:06:08',
        read: '2016-01-03T05:06:08',
      },
      {
        url: 'https://www.google.com.ua',
      },
    ];

    const wrapper = shallow(
      <ArticlesPage
        articles={Immutable.fromJS(data)}
        loadEntities={mockLoadEntities}
      />
    );

    wrapper.setState({ selectedRows: [] });

    wrapper.instance().handleOnExport();

    expect(setAttributeSpy.called).to.equal(false);
    expect(clickSpy.called).to.equal(false);
  });
});

@@ -764,32 +764,50 @@
 ckLoadEntities%7D%0A
+        search=%22%22%0A
       /%3E%0A    );%0A
@@ -791,32 +791,32 @@
 rch=%22%22%0A      /%3E%0A
-
     );%0A%0A    expe
@@ -1523,32 +1523,57 @@
 google.com.ua',%0A
+        title: 'Google',%0A
       %7D,%0A    %5D;%0A
@@ -1696,32 +1696,50 @@
 ckLoadEntities%7D%0A
+        search=%22%22%0A
       /%3E%0A    );%0A
@@ -2714,32 +2714,57 @@
 google.com.ua',%0A
+        title: 'Google',%0A
       %7D,%0A    %5D;%0A
@@ -2887,32 +2887,50 @@
 ckLoadEntities%7D%0A
+        search=%22%22%0A
       /%3E);%0A%0A    
@@ -3900,24 +3900,49 @@
 le.com.ua',%0A
+        title: 'Google',%0A
       %7D,%0A   
@@ -4029,32 +4029,32 @@
 e.fromJS(data)%7D%0A
-
         loadEnti
@@ -4073,24 +4073,42 @@
 adEntities%7D%0A
+        search=%22%22%0A
       /%3E%0A   

Fix ArticlesPage tests