{"size": 4136, "ext": "js", "max_stars_repo_path": "test/components/Base/Table.test.js", "max_stars_repo_name": "whDongRui/dashboard", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/components/Base/Table.test.js", "max_issues_repo_name": "whDongRui/dashboard", "max_issues_repo_issues_event_min_datetime": "2018-05-05T09:14:04.000Z", "max_issues_repo_issues_event_max_datetime": "2018-05-05T09:14:04.000Z", "max_forks_repo_path": "test/components/Base/Table.test.js", "max_forks_repo_name": "whDongRui/dashboard", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 24.0465116279, "max_line_length": 107, "alphanum_fraction": 0.5843810445}
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { render, mount } from 'enzyme';
import toJson from 'enzyme-to-json';

import Table from 'components/Base/Table';

function getTable(props) {
  return (
    <MemoryRouter>
      <Table {...props} />
    </MemoryRouter>
  );
}

const setup = (type, props = {}) => (type === 'render' ? render(getTable(props)) : mount(getTable(props)));

const getTableInstance = wrapper => wrapper
  .find(Table)
  .childAt(0)
  .childAt(0)
  .instance();

describe('Base/Table', () => {
  const columns = [
    {
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
      sorter: true,
      width: '50%'
    },
    {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
      width: '50%'
    }
  ];
  const dataSource = [
    {
      name: 'zhangsan',
      age: 20,
      key: '1'
    },
    {
      name: 'lisi',
      age: 22,
      key: '2'
    }
  ];
  const rowSelection = {
    selectedRowKeys: [],
    onChange: jest.fn(),
    onSelect: jest.fn(),
    onSelectAll: jest.fn()
  };
  const pagination = {
    tableType: 'Clusters',
    onChange: jest.fn(),
    total: 2,
    current: 2
  };
  it('basic render', () => {
    const wrapper = setup('render', { columns, dataSource });

    expect(toJson(wrapper)).toMatchSnapshot();
  });

  it('render emptyText', () => {
    const wrapper = setup('render', { columns, dataSource: [] });

    expect(toJson(wrapper)).toMatchSnapshot();
  });

  it('call onChange', () => {
    const wrapper = setup('mount', {
      columns,
      dataSource,
      rowSelection,
      pagination
    });
    const tableInstance = getTableInstance(wrapper);
    const checkbox = wrapper
      .find('tbody tr')
      .first()
      .find('td')
      .at(0)
      .find('input[type="checkbox"]');
    checkbox.simulate('change', { target: { checked: true } });

    const mockChange = rowSelection.onChange;
    expect(tableInstance.state.selectionDirty).toBeTruthy();
    expect(mockChange).toHaveBeenCalled();
    expect(mockChange.mock.calls[0][0]).toEqual(['1']);
  });

  it('call onSelect', () => {
    const wrapper = setup('mount', {
      columns,
      dataSource,
      rowSelection,
      pagination
    });
    const tableInstance = getTableInstance(wrapper);
    const checkbox = wrapper
      .find('tbody tr')
      .first()
      .find('td')
      .at(0)
      .find('input[type="checkbox"]');
    checkbox.simulate('change', { target: { checked: true } });

    const mockSelect = rowSelection.onSelect;
    expect(tableInstance.state.selectionDirty).toBeTruthy();
    expect(mockSelect).toHaveBeenCalled();
    expect(mockSelect.mock.calls[0][1]).toEqual(dataSource[0]);
  });

  it('call onSelectAll', () => {
    const wrapper = setup('mount', {
      columns,
      dataSource,
      rowSelection,
      pagination
    });
    const tableInstance = getTableInstance(wrapper);
    const checkbox = wrapper
      .find('thead th')
      .at(0)
      .find('input[type="checkbox"]');
    checkbox.simulate('change', { target: { checked: true } });

    const mockSelectAll = rowSelection.onSelectAll;
    expect(tableInstance.state.selectionDirty).toBeTruthy();
    expect(mockSelectAll).toHaveBeenCalled();
    expect(mockSelectAll.mock.calls[0][1]).toEqual(dataSource);
  });

  it('call remove onSelectAll', () => {
    const wrapper = setup('mount', {
      columns,
      dataSource,
      rowSelection,
      pagination
    });
    const checkbox = wrapper
      .find('thead th')
      .at(0)
      .find('input[type="checkbox"]');
    const mockSelectAll = rowSelection.onSelectAll;
    checkbox.simulate('change', { target: { checked: true } });
    expect(mockSelectAll).toHaveBeenCalled();
    checkbox.simulate('change', { target: { checked: false } });
    expect(mockSelectAll).toHaveBeenCalled();
  });

  it('call handleSort', () => {
    const wrapper = setup('mount', {
      columns,
      dataSource,
      rowSelection,
      pagination
    });
    const icon = wrapper
      .find('thead th')
      .at(1)
      .find('span.icon');
    icon.simulate('click');
  });
});
