forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Table testing (ant-design#4151)
- Loading branch information
Showing
12 changed files
with
871 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import React from 'react'; | ||
import { render, mount } from 'enzyme'; | ||
import { renderToJson } from 'enzyme-to-json'; | ||
import Table from '../../components/table'; | ||
|
||
describe('Table.filter', () => { | ||
const filterFn = (value, record) => record.name === 'Lucy'; | ||
const column = { | ||
title: 'Name', | ||
dataIndex: 'name', | ||
filters: [ | ||
{ text: 'Boy', value: 'boy' }, | ||
{ text: 'Girl', value: 'girl' }, | ||
{ text: 'Title', value: 'title', children: [ | ||
{ text: 'Designer', value: 'designer' }, | ||
{ text: 'Coder', value: 'coder' }, | ||
] }, | ||
], | ||
onFilter: filterFn, | ||
}; | ||
|
||
const data = [ | ||
{ key: 0, name: 'Jack' }, | ||
{ key: 1, name: 'Lucy' }, | ||
{ key: 2, name: 'Tom' }, | ||
{ key: 3, name: 'Jerry' }, | ||
]; | ||
|
||
function createTable(props) { | ||
return ( | ||
<Table | ||
columns={[ column ]} | ||
dataSource={data} | ||
pagination={false} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
it('renders filter correctly', () => { | ||
const wrapper = render(createTable()); | ||
|
||
expect(renderToJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders menu correctly', () => { | ||
const wrapper = mount(createTable()); | ||
const dropdownWrapper = render(wrapper.find('Trigger').node.getComponent()); | ||
expect(renderToJson(dropdownWrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders radio filter correctly', () => { | ||
const wrapper = mount(createTable({ | ||
columns: [{ | ||
...column, | ||
filterMultiple: false | ||
}], | ||
})); | ||
const dropdownWrapper = render(wrapper.find('Trigger').node.getComponent()); | ||
expect(renderToJson(dropdownWrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders custom content correctly', () => { | ||
const filter = ( | ||
<div className="custom-filter-dropdown"> | ||
custom filter | ||
</div> | ||
); | ||
const wrapper = mount(createTable({ | ||
columns: [{ | ||
...column, | ||
filterDropdown: filter | ||
}], | ||
})); | ||
|
||
const dropdownWrapper = render(wrapper.find('Trigger').node.getComponent()); | ||
expect(renderToJson(dropdownWrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
xit('can be controlled by filterDropdownVisible', () => { | ||
const wrapper = mount(createTable({ | ||
columns: [{ | ||
...column, | ||
filterDropdownVisible: true | ||
}], | ||
})); | ||
const dropdown = wrapper.find('Dropdown').first(); | ||
|
||
expect(dropdown.props().visible).toBe(true); | ||
wrapper.setProps({ columns: [{ | ||
...column, | ||
filterDropdownVisible: false | ||
}]}); | ||
expect(dropdown.props().visible).toBe(false); | ||
}); | ||
|
||
it('fires change event when visible change', () => { | ||
const handleChange = jest.fn(); | ||
const wrapper = mount(createTable({ | ||
columns: [{ | ||
...column, | ||
onFilterDropdownVisibleChange: handleChange | ||
}], | ||
})); | ||
|
||
wrapper.find('Dropdown').first().simulate('click'); | ||
|
||
expect(handleChange).toBeCalledWith(true); | ||
}); | ||
|
||
it('can be controlled by filteredValue', () => { | ||
const wrapper = mount(createTable({ | ||
columns: [{ | ||
...column, | ||
filteredValue: ['girl'], | ||
}], | ||
})); | ||
|
||
expect(wrapper.find('tbody tr').length).toBe(1); | ||
wrapper.setProps({ columns: [{ | ||
...column, | ||
filteredValue: [], | ||
}]}); | ||
expect(wrapper.find('tbody tr').length).toBe(4); | ||
}); | ||
|
||
it('fires change event', () => { | ||
const handleChange = jest.fn(); | ||
const wrapper = mount(createTable({ onChange: handleChange })); | ||
const dropdownWrapper = mount(wrapper.find('Trigger').node.getComponent()); | ||
|
||
dropdownWrapper.find('MenuItem').first().simulate('click'); | ||
dropdownWrapper.find('.confirm').simulate('click'); | ||
|
||
expect(handleChange).toBeCalledWith({}, { name: ['boy'] }, {}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from 'react'; | ||
import { render, mount } from 'enzyme'; | ||
import { renderToJson } from 'enzyme-to-json'; | ||
import Table from '../../components/table'; | ||
|
||
describe('Table.pagination', () => { | ||
const columns = [{ | ||
title: 'Name', | ||
dataIndex: 'name', | ||
}]; | ||
|
||
const data = [ | ||
{ key: 0, name: 'Jack' }, | ||
{ key: 1, name: 'Lucy' }, | ||
{ key: 2, name: 'Tom' }, | ||
{ key: 3, name: 'Jerry' }, | ||
]; | ||
|
||
const pagination = { pageSize: 2 }; | ||
|
||
function createTable(props) { | ||
return ( | ||
<Table | ||
columns={columns} | ||
dataSource={data} | ||
pagination={pagination} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
function renderedNames (wrapper) { | ||
return wrapper.find('TableRow').map(row => row.props().record.name); | ||
} | ||
|
||
it('renders pagination correctly', () => { | ||
const wrapper = render(createTable()); | ||
expect(renderToJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('paginate data', () => { | ||
const wrapper = mount(createTable()); | ||
|
||
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy']); | ||
wrapper.find('Pager').last().simulate('click'); | ||
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']); | ||
}); | ||
|
||
|
||
it('repaginates when pageSize change', () => { | ||
const wrapper = mount(createTable()); | ||
|
||
wrapper.setProps({ pagination: { pageSize: 1 } }); | ||
expect(renderedNames(wrapper)).toEqual(['Jack']); | ||
}); | ||
|
||
it('fires change event', () => { | ||
const handleChange = jest.fn(); | ||
const noop = () => {}; | ||
const wrapper = mount(createTable({ | ||
pagination: { ...pagination, onChange: noop, onShowSizeChange: noop }, | ||
onChange: handleChange | ||
})); | ||
|
||
wrapper.find('Pager').last().simulate('click'); | ||
|
||
expect(handleChange).toBeCalledWith( | ||
{ | ||
current: 2, | ||
onChange: noop, | ||
onShowSizeChange: noop, | ||
pageSize: 2 | ||
}, | ||
{}, | ||
{} | ||
); | ||
}); | ||
}); |
Oops, something went wrong.