2019-11-15 14:35:25 +08:00
/* eslint-disable import/first */
jest . mock ( '../../_util/scrollTo' ) ;
2016-12-14 14:48:09 +08:00
import React from 'react' ;
2019-11-15 14:35:25 +08:00
import { mount } from 'enzyme' ;
2016-12-14 15:38:26 +08:00
import Table from '..' ;
2019-11-15 14:35:25 +08:00
import scrollTo from '../../_util/scrollTo' ;
2020-05-14 15:57:04 +08:00
import { resetWarned } from '../../_util/devWarning' ;
2016-12-14 14:48:09 +08:00
describe ( 'Table.pagination' , ( ) => {
2018-12-07 16:17:45 +08:00
const columns = [
{
title : 'Name' ,
dataIndex : 'name' ,
} ,
] ;
2016-12-14 14:48:09 +08:00
const data = [
{ key : 0 , name : 'Jack' } ,
{ key : 1 , name : 'Lucy' } ,
{ key : 2 , name : 'Tom' } ,
{ key : 3 , name : 'Jerry' } ,
] ;
2017-06-12 11:35:29 +08:00
const pagination = { className : 'my-page' , pageSize : 2 } ;
2016-12-14 14:48:09 +08:00
function createTable ( props ) {
2018-12-07 16:17:45 +08:00
return < Table columns = { columns } dataSource = { data } pagination = { pagination } { ... props } / > ;
2016-12-14 14:48:09 +08:00
}
function renderedNames ( wrapper ) {
2020-04-10 14:13:14 +08:00
return wrapper . find ( 'BodyRow' ) . map ( row => row . props ( ) . record . name ) ;
2016-12-14 14:48:09 +08:00
}
it ( 'renders pagination correctly' , ( ) => {
2019-11-15 14:35:25 +08:00
const wrapper = mount ( createTable ( ) ) ;
expect ( wrapper . render ( ) ) . toMatchSnapshot ( ) ;
2016-12-14 14:48:09 +08:00
} ) ;
2020-04-28 20:29:34 +08:00
it ( 'not crash when pageSize is undefined' , ( ) => {
expect ( ( ) => {
mount ( createTable ( { pagination : { pageSIze : undefined } } ) ) ;
} ) . not . toThrow ( ) ;
} ) ;
2017-12-29 20:14:53 +08:00
it ( 'should not show pager if pagination.hideOnSinglePage is true and only 1 page' , ( ) => {
const wrapper = mount ( createTable ( { pagination : { pageSize : 3 , hideOnSinglePage : true } } ) ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
wrapper . setProps ( { pagination : { pageSize : 3 , hideOnSinglePage : false } } ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
wrapper . setProps ( { pagination : { pageSize : 4 , hideOnSinglePage : true } } ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
wrapper . setProps ( { pagination : { pageSize : 4 , hideOnSinglePage : false } } ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
wrapper . setProps ( { pagination : { pageSize : 5 , hideOnSinglePage : true } } ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
wrapper . setProps ( { pagination : { pageSize : 5 , hideOnSinglePage : false } } ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
} ) ;
2019-02-02 13:24:01 +08:00
it ( 'should use pageSize when defaultPageSize and pageSize are both specified' , ( ) => {
const wrapper = mount ( createTable ( { pagination : { pageSize : 3 , defaultPageSize : 4 } } ) ) ;
expect ( wrapper . find ( '.ant-pagination-item' ) ) . toHaveLength ( 2 ) ;
} ) ;
2016-12-14 14:48:09 +08:00
it ( 'paginate data' , ( ) => {
const wrapper = mount ( createTable ( ) ) ;
expect ( renderedNames ( wrapper ) ) . toEqual ( [ 'Jack' , 'Lucy' ] ) ;
2020-03-27 15:48:42 +08:00
wrapper . find ( 'Pager' ) . last ( ) . simulate ( 'click' ) ;
2016-12-14 14:48:09 +08:00
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' ] ) ;
} ) ;
2019-09-28 18:14:28 +08:00
it ( 'should accept pagination size' , ( ) => {
const wrapper = mount (
createTable ( {
pagination : { size : 'small' } ,
} ) ,
) ;
expect ( wrapper . find ( '.ant-pagination.mini' ) ) . toHaveLength ( 1 ) ;
} ) ;
2019-09-08 09:25:05 +08:00
it ( 'should scroll to first row when page change' , ( ) => {
2019-11-15 14:35:25 +08:00
scrollTo . mockReturnValue ( null ) ;
2019-11-02 17:18:44 +08:00
const wrapper = mount (
createTable ( { scroll : { y : 20 } , pagination : { showSizeChanger : true , pageSize : 2 } } ) ,
) ;
2019-11-15 14:35:25 +08:00
expect ( scrollTo ) . toHaveBeenCalledTimes ( 0 ) ;
2019-09-08 09:25:05 +08:00
2020-03-27 15:48:42 +08:00
wrapper . find ( 'Pager' ) . last ( ) . simulate ( 'click' ) ;
2019-11-15 14:35:25 +08:00
expect ( scrollTo ) . toHaveBeenCalledTimes ( 1 ) ;
2019-11-02 17:18:44 +08:00
2019-11-06 10:34:53 +08:00
wrapper . find ( '.ant-select-selector' ) . simulate ( 'mousedown' ) ;
2020-03-27 15:48:42 +08:00
wrapper . find ( '.ant-select-item' ) . last ( ) . simulate ( 'click' ) ;
2020-06-28 22:41:59 +08:00
expect ( scrollTo ) . toHaveBeenCalledTimes ( 3 ) ;
2019-09-08 09:25:05 +08:00
} ) ;
2016-12-14 14:48:09 +08:00
it ( 'fires change event' , ( ) => {
const handleChange = jest . fn ( ) ;
2017-02-19 13:49:22 +08:00
const handlePaginationChange = jest . fn ( ) ;
2016-12-14 14:48:09 +08:00
const noop = ( ) => { } ;
2018-12-07 16:17:45 +08:00
const wrapper = mount (
createTable ( {
pagination : { ... pagination , onChange : handlePaginationChange , onShowSizeChange : noop } ,
onChange : handleChange ,
} ) ,
) ;
2016-12-14 14:48:09 +08:00
2020-03-27 15:48:42 +08:00
wrapper . find ( 'Pager' ) . last ( ) . simulate ( 'click' ) ;
2016-12-14 14:48:09 +08:00
2019-04-03 15:54:26 +08:00
expect ( handleChange ) . toHaveBeenCalledWith (
2016-12-14 14:48:09 +08:00
{
2017-06-12 11:35:29 +08:00
className : 'my-page' ,
2016-12-14 14:48:09 +08:00
current : 2 ,
pageSize : 2 ,
} ,
{ } ,
2018-09-30 11:17:39 +08:00
{ } ,
{
currentDataSource : [
{ key : 0 , name : 'Jack' } ,
{ key : 1 , name : 'Lucy' } ,
{ key : 2 , name : 'Tom' } ,
{ key : 3 , name : 'Jerry' } ,
] ,
2020-06-28 22:41:59 +08:00
action : 'paginate' ,
2018-09-30 11:17:39 +08:00
} ,
2016-12-14 14:48:09 +08:00
) ;
2017-02-19 13:49:22 +08:00
2019-04-03 15:54:26 +08:00
expect ( handlePaginationChange ) . toHaveBeenCalledWith ( 2 , 2 ) ;
2016-12-14 14:48:09 +08:00
} ) ;
2017-01-09 20:49:00 +08:00
2017-02-07 21:09:51 +08:00
// https://github.com/ant-design/ant-design/issues/4532
2017-10-10 11:35:34 +08:00
// https://codepen.io/afc163/pen/dVeNoP?editors=001
2017-02-07 21:21:51 +08:00
it ( 'should have pager when change pagination from false to undefined' , ( ) => {
2017-02-07 21:09:51 +08:00
const wrapper = mount ( createTable ( { pagination : false } ) ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
wrapper . setProps ( { pagination : undefined } ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
expect ( wrapper . find ( '.ant-pagination-item-active' ) ) . toHaveLength ( 1 ) ;
} ) ;
// https://github.com/ant-design/ant-design/issues/4532
2017-10-10 11:35:34 +08:00
// https://codepen.io/afc163/pen/pWVRJV?editors=001
2017-02-07 21:09:51 +08:00
it ( 'should display pagination as prop pagination change between true and false' , ( ) => {
2017-01-09 20:49:00 +08:00
const wrapper = mount ( createTable ( ) ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
expect ( wrapper . find ( '.ant-pagination-item' ) ) . toHaveLength ( 2 ) ;
wrapper . setProps ( { pagination : false } ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
wrapper . setProps ( { pagination } ) ;
2017-09-20 16:26:18 +08:00
wrapper . update ( ) ;
2017-01-09 20:49:00 +08:00
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
expect ( wrapper . find ( '.ant-pagination-item' ) ) . toHaveLength ( 2 ) ;
wrapper . find ( '.ant-pagination-item-2' ) . simulate ( 'click' ) ;
expect ( renderedNames ( wrapper ) ) . toEqual ( [ 'Tom' , 'Jerry' ] ) ;
wrapper . setProps ( { pagination : false } ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
2019-11-15 14:35:25 +08:00
wrapper . setProps ( { pagination : undefined } ) ;
2017-01-09 20:49:00 +08:00
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
2019-11-15 14:35:25 +08:00
expect ( wrapper . find ( '.ant-pagination-item' ) ) . toHaveLength ( 2 ) ;
expect ( renderedNames ( wrapper ) ) . toEqual ( [ 'Tom' , 'Jerry' ] ) ;
2017-01-09 20:49:00 +08:00
} ) ;
2017-03-16 23:01:13 +08:00
// https://github.com/ant-design/ant-design/issues/5259
it ( 'change to correct page when data source changes' , ( ) => {
const wrapper = mount ( createTable ( { pagination : { pageSize : 1 } } ) ) ;
wrapper . find ( '.ant-pagination-item-3' ) . simulate ( 'click' ) ;
wrapper . setProps ( { dataSource : [ data [ 0 ] ] } ) ;
2018-12-07 16:17:45 +08:00
expect ( wrapper . find ( '.ant-pagination-item-1' ) . hasClass ( 'ant-pagination-item-active' ) ) . toBe (
true ,
) ;
2017-03-16 23:01:13 +08:00
} ) ;
2018-03-02 12:00:28 +08:00
2020-06-28 22:41:59 +08:00
// https://github.com/ant-design/ant-design/issues/24913
it ( 'should onChange called when pageSize change' , ( ) => {
const onChange = jest . fn ( ) ;
const onShowSizeChange = jest . fn ( ) ;
const wrapper = mount (
createTable ( {
pagination : {
current : 1 ,
pageSize : 10 ,
total : 200 ,
onChange ,
onShowSizeChange ,
} ,
} ) ,
) ;
wrapper . find ( '.ant-select-selector' ) . simulate ( 'mousedown' ) ;
expect ( wrapper . find ( '.ant-select-item-option' ) . length ) . toBe ( 4 ) ;
wrapper . find ( '.ant-select-item-option' ) . at ( 1 ) . simulate ( 'click' ) ;
expect ( onChange ) . toHaveBeenCalledWith ( 1 , 20 ) ;
} ) ;
2019-09-28 16:20:19 +08:00
it ( 'should not change page when pagination current is specified' , ( ) => {
const wrapper = mount ( createTable ( { pagination : { current : 2 , pageSize : 1 } } ) ) ;
expect ( wrapper . find ( '.ant-pagination-item-2' ) . hasClass ( 'ant-pagination-item-active' ) ) . toBe (
true ,
) ;
wrapper . find ( '.ant-pagination-item-3' ) . simulate ( 'click' ) ;
expect ( wrapper . find ( '.ant-pagination-item-2' ) . hasClass ( 'ant-pagination-item-active' ) ) . toBe (
true ,
) ;
} ) ;
2018-03-02 12:00:28 +08:00
it ( 'specify the position of pagination' , ( ) => {
2020-03-27 15:48:42 +08:00
const wrapper = mount ( createTable ( { pagination : { position : [ 'topLeft' ] } } ) ) ;
2018-03-02 12:00:28 +08:00
expect ( wrapper . find ( '.ant-spin-container' ) . children ( ) ) . toHaveLength ( 2 ) ;
2020-03-27 15:48:42 +08:00
expect ( wrapper . find ( '.ant-spin-container' ) . childAt ( 0 ) . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
wrapper . setProps ( { pagination : { position : [ 'bottomRight' ] } } ) ;
2018-03-02 12:00:28 +08:00
expect ( wrapper . find ( '.ant-spin-container' ) . children ( ) ) . toHaveLength ( 2 ) ;
2020-03-27 15:48:42 +08:00
expect ( wrapper . find ( '.ant-spin-container' ) . childAt ( 1 ) . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
wrapper . setProps ( { pagination : { position : [ 'topLeft' , 'bottomRight' ] } } ) ;
2018-03-02 12:00:28 +08:00
expect ( wrapper . find ( '.ant-spin-container' ) . children ( ) ) . toHaveLength ( 3 ) ;
2020-03-27 15:48:42 +08:00
expect ( wrapper . find ( '.ant-spin-container' ) . childAt ( 0 ) . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
expect ( wrapper . find ( '.ant-spin-container' ) . childAt ( 2 ) . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
2020-03-30 15:45:14 +08:00
wrapper . setProps ( { pagination : { position : [ 'invalid' ] } } ) ;
expect ( wrapper . find ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
2018-03-02 12:00:28 +08:00
} ) ;
2019-01-28 22:43:03 +08:00
2019-02-14 11:40:46 +08:00
/ * *
* ` pagination ` is not designed to accept ` true ` value ,
* but in practice , many people assign ` true ` to ` pagination ` ,
* since they misunderstand that ` pagination ` can accept a boolean value .
* /
it ( 'Accepts pagination as true' , ( ) => {
2019-11-15 14:35:25 +08:00
const wrapper = mount ( createTable ( { pagination : true } ) ) ;
expect ( wrapper . render ( ) ) . toMatchSnapshot ( ) ;
2019-02-14 11:40:46 +08:00
} ) ;
2019-02-18 18:05:27 +08:00
it ( 'ajax render should keep display by the dataSource' , ( ) => {
const onChange = jest . fn ( ) ;
2020-03-30 15:45:14 +08:00
const onPaginationChange = jest . fn ( ) ;
2019-02-18 18:05:27 +08:00
const wrapper = mount (
createTable ( {
onChange ,
pagination : {
total : 200 ,
2020-03-30 15:45:14 +08:00
onChange : onPaginationChange ,
2019-02-18 18:05:27 +08:00
} ,
} ) ,
) ;
expect ( wrapper . find ( '.ant-table-tbody tr.ant-table-row' ) ) . toHaveLength ( data . length ) ;
wrapper . find ( '.ant-pagination .ant-pagination-item-2' ) . simulate ( 'click' ) ;
expect ( onChange . mock . calls [ 0 ] [ 0 ] . current ) . toBe ( 2 ) ;
2020-04-10 14:13:14 +08:00
expect ( onChange ) . toHaveBeenCalledWith (
{ current : 2 , pageSize : 10 , total : 200 } ,
{ } ,
{ } ,
{
currentDataSource : [
{ key : 0 , name : 'Jack' } ,
{ key : 1 , name : 'Lucy' } ,
{ key : 2 , name : 'Tom' } ,
{ key : 3 , name : 'Jerry' } ,
] ,
2020-06-28 22:41:59 +08:00
action : 'paginate' ,
2020-04-10 14:13:14 +08:00
} ,
) ;
2020-03-30 15:45:14 +08:00
expect ( onPaginationChange ) . toHaveBeenCalledWith ( 2 , 10 ) ;
2019-02-18 18:05:27 +08:00
expect ( wrapper . find ( '.ant-table-tbody tr.ant-table-row' ) ) . toHaveLength ( data . length ) ;
} ) ;
2019-08-27 17:11:00 +08:00
it ( 'select by checkbox to trigger stopPropagation' , ( ) => {
2019-08-27 17:35:29 +08:00
jest . useFakeTimers ( ) ;
2019-08-27 17:11:00 +08:00
const onShowSizeChange = jest . fn ( ) ;
2019-09-28 15:22:27 +08:00
const onChange = jest . fn ( ) ;
2019-08-27 17:11:00 +08:00
const wrapper = mount (
createTable ( {
pagination : {
total : 200 ,
showSizeChanger : true ,
onShowSizeChange ,
} ,
2019-09-28 15:22:27 +08:00
onChange ,
2019-08-27 17:11:00 +08:00
} ) ,
) ;
2019-09-12 20:15:17 +08:00
wrapper . find ( '.ant-select-selector' ) . simulate ( 'mousedown' ) ;
2019-08-27 17:35:29 +08:00
jest . runAllTimers ( ) ;
2020-03-27 15:48:42 +08:00
const dropdownWrapper = mount ( wrapper . find ( 'Trigger' ) . instance ( ) . getComponent ( ) ) ;
2019-09-12 20:15:17 +08:00
expect ( wrapper . find ( '.ant-select-item-option' ) . length ) . toBe ( 4 ) ;
2020-03-27 15:48:42 +08:00
dropdownWrapper . find ( '.ant-select-item-option' ) . at ( 3 ) . simulate ( 'click' ) ;
2019-08-27 17:11:00 +08:00
expect ( onShowSizeChange ) . toHaveBeenCalled ( ) ;
2019-09-28 15:22:27 +08:00
expect ( onChange ) . toHaveBeenCalled ( ) ;
2019-08-27 17:35:29 +08:00
jest . useRealTimers ( ) ;
2019-08-27 17:11:00 +08:00
} ) ;
2019-09-28 15:22:27 +08:00
it ( 'should support current in pagination' , ( ) => {
const wrapper = mount ( createTable ( { pagination : { current : 2 , pageSize : 1 } } ) ) ;
expect ( wrapper . find ( '.ant-pagination-item-active' ) . text ( ) ) . toBe ( '2' ) ;
} ) ;
it ( 'should support defaultCurrent in pagination' , ( ) => {
const wrapper = mount ( createTable ( { pagination : { defaultCurrent : 2 , pageSize : 1 } } ) ) ;
expect ( wrapper . find ( '.ant-pagination-item-active' ) . text ( ) ) . toBe ( '2' ) ;
} ) ;
it ( 'should support defaultPageSize in pagination' , ( ) => {
const wrapper = mount ( createTable ( { pagination : { defaultPageSize : 1 } } ) ) ;
expect ( wrapper . find ( '.ant-pagination-item' ) ) . toHaveLength ( 4 ) ;
} ) ;
2019-11-28 14:46:49 +08:00
// https://github.com/ant-design/ant-design/issues/19957
it ( 'ajax should work with pagination' , ( ) => {
const wrapper = mount ( createTable ( { pagination : { total : 100 } } ) ) ;
wrapper . find ( '.ant-pagination-item-2' ) . simulate ( 'click' ) ;
wrapper . setProps ( { pagination : { current : 2 , total : 100 } } ) ;
expect (
wrapper . find ( '.ant-pagination-item-2' ) . hasClass ( 'ant-pagination-item-active' ) ,
) . toBeTruthy ( ) ;
} ) ;
2019-12-30 15:35:53 +08:00
it ( 'pagination should ignore invalidate total' , ( ) => {
const wrapper = mount ( createTable ( { pagination : { total : null } } ) ) ;
expect ( wrapper . find ( '.ant-pagination-item-1' ) . length ) . toBeTruthy ( ) ;
} ) ;
2020-03-27 15:48:42 +08:00
it ( 'renders pagination topLeft and bottomRight' , ( ) => {
const wrapper = mount ( createTable ( { pagination : [ 'topLeft' , 'bottomRight' ] } ) ) ;
expect ( wrapper . render ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2020-04-10 14:13:14 +08:00
it ( 'dynamic warning' , ( ) => {
resetWarned ( ) ;
const errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
const dynamicData = [ ] ;
for ( let i = 0 ; i < 15 ; i += 1 ) {
dynamicData . push ( {
key : i ,
name : i ,
} ) ;
}
const wrapper = mount (
createTable ( {
dataSource : dynamicData ,
pagination : { total : 100 , pageSize : 10 , current : 2 } ,
} ) ,
) ;
expect ( wrapper . find ( 'tbody tr' ) ) . toHaveLength ( 5 ) ;
expect ( errorSpy ) . toHaveBeenCalledWith (
'Warning: [antd: Table] `dataSource` length is less than `pagination.total` but large than `pagination.pageSize`. Please make sure your config correct data with async mode.' ,
) ;
} ) ;
2016-12-14 14:48:09 +08:00
} ) ;