2019-11-15 14:35:25 +08:00
/* eslint-disable import/first */
jest . mock ( '../../_util/scrollTo' ) ;
2022-06-02 17:26:41 +08:00
import React from 'react' ;
2016-12-14 15:38:26 +08:00
import Table from '..' ;
2022-08-08 13:31:55 +08:00
import { fireEvent , render , act } from '../../../tests/utils' ;
2019-11-15 14:35:25 +08:00
import scrollTo from '../../_util/scrollTo' ;
2022-05-10 15:43:29 +08:00
import { resetWarned } from '../../_util/warning' ;
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' } ,
] ;
2021-02-03 11:42:11 +08:00
const longData = [ ] ;
for ( let i = 0 ; i < 100 ; i += 1 ) {
longData . push ( { key : i , name : ` ${ i } ` } ) ;
}
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
}
2022-06-07 10:14:32 +08:00
function renderedNames ( contain ) {
// --- reserve comment for code review ---
// return wrapper.find('BodyRow').map(row => row.props().record.name);
const namesList = [ ] ;
contain
. querySelector ( '.ant-table-tbody' )
. querySelectorAll ( 'tr' )
. forEach ( tr => {
namesList . push ( tr . querySelector ( 'td' ) . textContent ) ;
} ) ;
return namesList ;
2016-12-14 14:48:09 +08:00
}
it ( 'renders pagination correctly' , ( ) => {
2022-06-07 10:14:32 +08:00
const { asFragment } = render ( createTable ( ) ) ;
expect ( asFragment ( ) . firstChild ) . 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 ( ( ) => {
2022-06-07 10:14:32 +08:00
render ( createTable ( { pagination : { pageSize : undefined } } ) ) ;
2020-04-28 20:29:34 +08:00
} ) . not . toThrow ( ) ;
} ) ;
2017-12-29 20:14:53 +08:00
it ( 'should not show pager if pagination.hideOnSinglePage is true and only 1 page' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container , rerender } = render (
createTable ( { pagination : { pageSize : 3 , hideOnSinglePage : true } } ) ,
) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
rerender ( createTable ( { pagination : { pageSize : 3 , hideOnSinglePage : false } } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
rerender ( createTable ( { pagination : { pageSize : 4 , hideOnSinglePage : true } } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
rerender ( createTable ( { pagination : { pageSize : 4 , hideOnSinglePage : false } } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
rerender ( createTable ( { pagination : { pageSize : 5 , hideOnSinglePage : true } } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
rerender ( createTable ( { pagination : { pageSize : 5 , hideOnSinglePage : false } } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
2017-12-29 20:14:53 +08:00
} ) ;
2019-02-02 13:24:01 +08:00
it ( 'should use pageSize when defaultPageSize and pageSize are both specified' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container } = render ( createTable ( { pagination : { pageSize : 3 , defaultPageSize : 4 } } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination-item' ) ) . toHaveLength ( 2 ) ;
2019-02-02 13:24:01 +08:00
} ) ;
2016-12-14 14:48:09 +08:00
it ( 'paginate data' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container } = render ( createTable ( ) ) ;
2016-12-14 14:48:09 +08:00
2022-06-07 10:14:32 +08:00
expect ( renderedNames ( container ) ) . toEqual ( [ 'Jack' , 'Lucy' ] ) ;
fireEvent . click ( container . querySelector ( '.ant-pagination-next' ) ) ;
expect ( renderedNames ( container ) ) . toEqual ( [ 'Tom' , 'Jerry' ] ) ;
2016-12-14 14:48:09 +08:00
} ) ;
it ( 'repaginates when pageSize change' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container , rerender } = render ( createTable ( ) ) ;
rerender ( createTable ( { pagination : { pageSize : 1 } } ) ) ;
expect ( renderedNames ( container ) ) . toEqual ( [ 'Jack' ] ) ;
2016-12-14 14:48:09 +08:00
} ) ;
2021-12-31 10:44:16 +08:00
// https://github.com/ant-design/ant-design/issues/33487
it ( 'should not crash when trigger onChange in render' , ( ) => {
function App ( ) {
const [ page , setPage ] = React . useState ( { current : 1 , pageSize : 10 } ) ;
const onChange = ( current , pageSize ) => {
setPage ( { current , pageSize } ) ;
} ;
return (
< Table
dataSource = { [ ] }
pagination = { {
... page ,
onChange ,
} }
/ >
) ;
}
2022-06-07 10:14:32 +08:00
const { asFragment } = render ( < App / > ) ;
expect ( asFragment ( ) . firstChild ) . toMatchSnapshot ( ) ;
2021-12-31 10:44:16 +08:00
} ) ;
2019-09-28 18:14:28 +08:00
it ( 'should accept pagination size' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container } = render (
2019-09-28 18:14:28 +08:00
createTable ( {
pagination : { size : 'small' } ,
} ) ,
) ;
2022-06-07 10:14:32 +08:00
expect ( container . querySelectorAll ( '.ant-pagination.ant-pagination-mini' ) ) . toHaveLength ( 1 ) ;
2019-09-28 18:14:28 +08:00
} ) ;
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 ) ;
2022-06-07 10:14:32 +08:00
const { container } = render (
2019-11-02 17:18:44 +08:00
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
2022-06-07 10:14:32 +08:00
fireEvent . click ( container . querySelector ( '.ant-pagination-next' ) ) ;
2019-11-15 14:35:25 +08:00
expect ( scrollTo ) . toHaveBeenCalledTimes ( 1 ) ;
2019-11-02 17:18:44 +08:00
2022-06-07 10:14:32 +08:00
fireEvent . mouseDown ( container . querySelector ( '.ant-select-selector' ) ) ;
fireEvent . click ( container . querySelectorAll ( '.ant-select-item' ) [ 1 ] ) ;
2020-07-10 15:17:15 +08:00
expect ( scrollTo ) . toHaveBeenCalledTimes ( 2 ) ;
2019-09-08 09:25:05 +08:00
} ) ;
2020-09-03 13:36:23 +08:00
it ( 'should scroll inside .ant-table-body' , ( ) => {
scrollTo . mockImplementationOnce ( ( top , { getContainer } ) => {
expect ( top ) . toBe ( 0 ) ;
expect ( getContainer ( ) . className ) . toBe ( 'ant-table-body' ) ;
} ) ;
2022-06-07 10:14:32 +08:00
const { container } = render (
2020-09-03 13:36:23 +08:00
createTable ( { scroll : { y : 20 } , pagination : { showSizeChanger : true , pageSize : 2 } } ) ,
) ;
2022-06-07 10:14:32 +08:00
fireEvent . click ( container . querySelector ( '.ant-pagination-next' ) ) ;
2020-09-03 13:36:23 +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 = ( ) => { } ;
2022-06-07 10:14:32 +08:00
const { container } = render (
2018-12-07 16:17:45 +08:00
createTable ( {
pagination : { ... pagination , onChange : handlePaginationChange , onShowSizeChange : noop } ,
onChange : handleChange ,
} ) ,
) ;
2022-06-07 10:14:32 +08:00
fireEvent . click ( container . querySelector ( '.ant-pagination-next' ) ) ;
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' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container , rerender } = render ( createTable ( { pagination : false } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
rerender ( createTable ( { pagination : undefined } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
expect ( container . querySelectorAll ( '.ant-pagination-item-active' ) ) . toHaveLength ( 1 ) ;
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/pWVRJV?editors=001
2017-02-07 21:09:51 +08:00
it ( 'should display pagination as prop pagination change between true and false' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container , rerender } = render ( createTable ( ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
expect ( container . querySelectorAll ( '.ant-pagination-item' ) ) . toHaveLength ( 2 ) ;
rerender ( createTable ( { pagination : false } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
rerender ( createTable ( { pagination } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
expect ( container . querySelectorAll ( '.ant-pagination-item' ) ) . toHaveLength ( 2 ) ;
fireEvent . click ( container . querySelector ( '.ant-pagination-item-2' ) ) ;
expect ( renderedNames ( container ) ) . toEqual ( [ 'Tom' , 'Jerry' ] ) ;
rerender ( createTable ( { pagination : false } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
rerender ( createTable ( { pagination : undefined } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
expect ( container . querySelectorAll ( '.ant-pagination-item' ) ) . toHaveLength ( 2 ) ;
expect ( renderedNames ( container ) ) . 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' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container , rerender } = render ( createTable ( { pagination : { pageSize : 1 } } ) ) ;
fireEvent . click ( container . querySelector ( '.ant-pagination-item-3' ) ) ;
rerender ( createTable ( { dataSource : [ data [ 0 ] ] } ) ) ;
expect (
container
. querySelector ( '.ant-pagination-item-1' )
. className . includes ( '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
2021-02-03 11:42:11 +08:00
it ( 'should called onChange when pageSize change' , ( ) => {
2020-06-28 22:41:59 +08:00
const onChange = jest . fn ( ) ;
const onShowSizeChange = jest . fn ( ) ;
2022-06-07 10:14:32 +08:00
const { container } = render (
2020-06-28 22:41:59 +08:00
createTable ( {
pagination : {
current : 1 ,
pageSize : 10 ,
total : 200 ,
onChange ,
onShowSizeChange ,
} ,
} ) ,
) ;
2022-06-07 10:14:32 +08:00
fireEvent . mouseDown ( container . querySelector ( '.ant-select-selector' ) ) ;
expect ( container . querySelectorAll ( '.ant-select-item-option' ) . length ) . toBe ( 4 ) ;
fireEvent . click ( container . querySelectorAll ( '.ant-select-item-option' ) [ 1 ] ) ;
2020-06-28 22:41:59 +08:00
expect ( onChange ) . toHaveBeenCalledWith ( 1 , 20 ) ;
} ) ;
2019-09-28 16:20:19 +08:00
it ( 'should not change page when pagination current is specified' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container } = render ( createTable ( { pagination : { current : 2 , pageSize : 1 } } ) ) ;
expect (
container
. querySelector ( '.ant-pagination-item-2' )
. className . includes ( 'ant-pagination-item-active' ) ,
) . toBe ( true ) ;
fireEvent . click ( container . querySelector ( '.ant-pagination-item-3' ) ) ;
expect (
container
. querySelector ( '.ant-pagination-item-2' )
. className . includes ( 'ant-pagination-item-active' ) ,
) . toBe ( true ) ;
2019-09-28 16:20:19 +08:00
} ) ;
2021-02-03 11:42:11 +08:00
// https://github.com/ant-design/ant-design/issues/29175
it ( 'should change page to max page count when pageSize change without pagination.total' , ( ) => {
const onChange = jest . fn ( ) ;
const onShowSizeChange = jest . fn ( ) ;
2022-06-07 10:14:32 +08:00
const { container } = render (
2021-02-03 11:42:11 +08:00
createTable ( {
pagination : {
current : 10 ,
pageSize : 10 ,
onChange ,
onShowSizeChange ,
} ,
dataSource : longData ,
} ) ,
) ;
2022-06-07 10:14:32 +08:00
fireEvent . mouseDown ( container . querySelector ( '.ant-select-selector' ) ) ;
expect ( container . querySelectorAll ( '.ant-select-item-option' ) . length ) . toBe ( 4 ) ;
fireEvent . click ( container . querySelectorAll ( '.ant-select-item-option' ) [ 1 ] ) ;
const newPageSize = parseInt (
container . querySelectorAll ( '.ant-select-item-option' ) [ 1 ] . textContent ,
10 ,
) ;
2021-02-03 11:42:11 +08:00
expect ( onChange ) . toHaveBeenCalledWith ( longData . length / newPageSize , 20 ) ;
} ) ;
it ( 'should change page to max page count when pageSize change with pagination.total' , ( ) => {
const onChange = jest . fn ( ) ;
const onShowSizeChange = jest . fn ( ) ;
const total = 20000 ;
2022-06-07 10:14:32 +08:00
const { container } = render (
2021-02-03 11:42:11 +08:00
createTable ( {
pagination : {
current : total / 10 ,
pageSize : 10 ,
onChange ,
total ,
onShowSizeChange ,
} ,
dataSource : longData ,
} ) ,
) ;
2022-06-07 10:14:32 +08:00
fireEvent . mouseDown ( container . querySelector ( '.ant-select-selector' ) ) ;
expect ( container . querySelectorAll ( '.ant-select-item-option' ) . length ) . toBe ( 4 ) ;
fireEvent . click ( container . querySelectorAll ( '.ant-select-item-option' ) [ 1 ] ) ;
const newPageSize = parseInt (
container . querySelectorAll ( '.ant-select-item-option' ) [ 1 ] . textContent ,
10 ,
) ;
2021-02-03 11:42:11 +08:00
expect ( onChange ) . toHaveBeenCalledWith ( total / newPageSize , 20 ) ;
} ) ;
// https://github.com/ant-design/ant-design/issues/29175
it ( 'should not change page to max page if current is not greater max page when pageSize change' , ( ) => {
const onChange = jest . fn ( ) ;
const onShowSizeChange = jest . fn ( ) ;
2022-06-07 10:14:32 +08:00
const { container } = render (
2021-02-03 11:42:11 +08:00
createTable ( {
pagination : {
current : 4 ,
pageSize : 10 ,
onChange ,
onShowSizeChange ,
} ,
dataSource : longData ,
} ) ,
) ;
2022-06-07 10:14:32 +08:00
fireEvent . mouseDown ( container . querySelector ( '.ant-select-selector' ) ) ;
expect ( container . querySelectorAll ( '.ant-select-item-option' ) . length ) . toBe ( 4 ) ;
fireEvent . click ( container . querySelectorAll ( '.ant-select-item-option' ) [ 1 ] ) ;
2021-02-03 11:42:11 +08:00
expect ( onChange ) . toHaveBeenCalledWith ( 4 , 20 ) ;
} ) ;
it ( 'should reset current to max page when data length is cut' , ( ) => {
const onChange = jest . fn ( ) ;
2022-06-07 10:14:32 +08:00
const { container , rerender } = render (
2021-02-03 11:42:11 +08:00
createTable ( {
pagination : {
current : 10 ,
pageSize : 10 ,
onChange ,
} ,
dataSource : longData ,
} ) ,
) ;
2022-06-07 10:14:32 +08:00
expect ( container . querySelector ( '.ant-pagination-item-active' ) . textContent ) . toBe ( '10' ) ;
rerender (
createTable ( {
pagination : {
current : 10 ,
pageSize : 10 ,
onChange ,
} ,
dataSource : longData . filter ( item => item . key < 60 ) ,
} ) ,
) ;
expect ( container . querySelector ( '.ant-pagination-item-active' ) . textContent ) . toBe ( '6' ) ;
2021-02-03 11:42:11 +08:00
} ) ;
2018-03-02 12:00:28 +08:00
it ( 'specify the position of pagination' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container , rerender } = render ( createTable ( { pagination : { position : [ 'topLeft' ] } } ) ) ;
expect ( container . querySelector ( '.ant-spin-container' ) . children ) . toHaveLength ( 2 ) ;
expect (
container
. querySelector ( '.ant-spin-container' )
. children [ 0 ] . className . includes ( 'ant-pagination' ) ,
) . toBe ( true ) ;
rerender ( createTable ( { pagination : { position : [ 'bottomRight' ] } } ) ) ;
expect ( container . querySelector ( '.ant-spin-container' ) . children ) . toHaveLength ( 2 ) ;
expect (
container
. querySelector ( '.ant-spin-container' )
. children [ 1 ] . className . includes ( 'ant-pagination' ) ,
) . toBe ( true ) ;
rerender ( createTable ( { pagination : { position : [ 'topLeft' , 'bottomRight' ] } } ) ) ;
expect ( container . querySelector ( '.ant-spin-container' ) . children ) . toHaveLength ( 3 ) ;
expect (
container
. querySelector ( '.ant-spin-container' )
. children [ 0 ] . className . includes ( 'ant-pagination' ) ,
) . toBe ( true ) ;
expect (
container
. querySelector ( '.ant-spin-container' )
. children [ 2 ] . className . includes ( 'ant-pagination' ) ,
) . toBe ( true ) ;
rerender ( createTable ( { pagination : { position : [ 'none' , 'none' ] } } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 0 ) ;
rerender ( createTable ( { pagination : { position : [ 'invalid' ] } } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 1 ) ;
rerender ( createTable ( { pagination : { position : [ 'invalid' , 'invalid' ] } } ) ) ;
expect ( container . querySelectorAll ( '.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
/ * *
2021-02-23 10:45:11 +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 .
2019-02-14 11:40:46 +08:00
* /
it ( 'Accepts pagination as true' , ( ) => {
2022-06-07 10:14:32 +08:00
const { asFragment } = render ( createTable ( { pagination : true } ) ) ;
expect ( asFragment ( ) . firstChild ) . 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
2022-06-07 10:14:32 +08:00
const { container } = render (
2019-02-18 18:05:27 +08:00
createTable ( {
onChange ,
pagination : {
total : 200 ,
2020-03-30 15:45:14 +08:00
onChange : onPaginationChange ,
2019-02-18 18:05:27 +08:00
} ,
} ) ,
) ;
2022-06-07 10:14:32 +08:00
expect ( container . querySelectorAll ( '.ant-table-tbody tr.ant-table-row' ) ) . toHaveLength (
data . length ,
) ;
2019-02-18 18:05:27 +08:00
2022-06-07 10:14:32 +08:00
fireEvent . click ( container . querySelector ( '.ant-pagination .ant-pagination-item-2' ) ) ;
2019-02-18 18:05:27 +08:00
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
2022-06-07 10:14:32 +08:00
expect ( container . querySelectorAll ( '.ant-table-tbody tr.ant-table-row' ) ) . toHaveLength (
data . length ,
) ;
2019-02-18 18:05:27 +08:00
} ) ;
2019-08-27 17:11:00 +08:00
2020-10-28 12:56:05 +08:00
it ( 'onShowSizeChange should trigger once' , ( ) => {
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 ( ) ;
2022-06-07 10:14:32 +08:00
const { container } = render (
2019-08-27 17:11:00 +08:00
createTable ( {
pagination : {
total : 200 ,
showSizeChanger : true ,
onShowSizeChange ,
} ,
2019-09-28 15:22:27 +08:00
onChange ,
2019-08-27 17:11:00 +08:00
} ) ,
) ;
2022-06-07 10:14:32 +08:00
fireEvent . mouseDown ( container . querySelector ( '.ant-select-selector' ) ) ;
// resolve Warning: An update to Align ran an effect, but was not wrapped in act(...)
act ( ( ) => {
jest . runAllTimers ( ) ;
} ) ;
expect ( container . querySelectorAll ( '.ant-select-item-option' ) . length ) . toBe ( 4 ) ;
fireEvent . click ( container . querySelectorAll ( '.ant-select-item-option' ) [ 3 ] ) ;
2020-10-28 12:56:05 +08:00
expect ( onShowSizeChange ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( onShowSizeChange ) . toHaveBeenLastCalledWith ( 1 , 100 ) ;
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' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container } = render ( createTable ( { pagination : { current : 2 , pageSize : 1 } } ) ) ;
expect ( container . querySelector ( '.ant-pagination-item-active' ) . textContent ) . toBe ( '2' ) ;
2019-09-28 15:22:27 +08:00
} ) ;
it ( 'should support defaultCurrent in pagination' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container } = render ( createTable ( { pagination : { defaultCurrent : 2 , pageSize : 1 } } ) ) ;
expect ( container . querySelector ( '.ant-pagination-item-active' ) . textContent ) . toBe ( '2' ) ;
2019-09-28 15:22:27 +08:00
} ) ;
it ( 'should support defaultPageSize in pagination' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container } = render ( createTable ( { pagination : { defaultPageSize : 1 } } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination-item' ) ) . toHaveLength ( 4 ) ;
2019-09-28 15:22:27 +08:00
} ) ;
2019-11-28 14:46:49 +08:00
// https://github.com/ant-design/ant-design/issues/19957
it ( 'ajax should work with pagination' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container , rerender } = render ( createTable ( { pagination : { total : 100 } } ) ) ;
fireEvent . click ( container . querySelector ( '.ant-pagination-item-2' ) ) ;
rerender ( createTable ( { pagination : { current : 2 , total : 100 } } ) ) ;
2019-11-28 14:46:49 +08:00
expect (
2022-06-07 10:14:32 +08:00
container
. querySelector ( '.ant-pagination-item-2' )
. className . includes ( 'ant-pagination-item-active' ) ,
2019-11-28 14:46:49 +08:00
) . toBeTruthy ( ) ;
} ) ;
2019-12-30 15:35:53 +08:00
it ( 'pagination should ignore invalidate total' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container } = render ( createTable ( { pagination : { total : null } } ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination-item-1' ) . length ) . toBeTruthy ( ) ;
2019-12-30 15:35:53 +08:00
} ) ;
2020-03-27 15:48:42 +08:00
it ( 'renders pagination topLeft and bottomRight' , ( ) => {
2022-06-07 10:14:32 +08:00
const { asFragment } = render ( createTable ( { pagination : [ 'topLeft' , 'bottomRight' ] } ) ) ;
expect ( asFragment ( ) . firstChild ) . toMatchSnapshot ( ) ;
2020-03-27 15:48:42 +08:00
} ) ;
2020-04-10 14:13:14 +08:00
2020-07-10 15:17:15 +08:00
it ( 'should call onChange when change pagination size' , ( ) => {
const onChange = jest . fn ( ) ;
2022-06-07 10:14:32 +08:00
const { container } = render (
2020-07-10 15:17:15 +08:00
createTable ( {
pagination : {
total : 200 ,
showSizeChanger : true ,
} ,
onChange ,
} ) ,
) ;
2022-06-07 10:14:32 +08:00
fireEvent . mouseDown ( container . querySelector ( '.ant-select-selector' ) ) ;
fireEvent . click ( container . querySelectorAll ( '.ant-select-item-option' ) [ 2 ] ) ;
2020-08-11 16:18:34 +08:00
expect ( onChange ) . toBeCalledTimes ( 1 ) ;
2020-07-10 15:17:15 +08:00
} ) ;
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 ,
} ) ;
}
2022-06-07 10:14:32 +08:00
const { container } = render (
2020-04-10 14:13:14 +08:00
createTable ( {
dataSource : dynamicData ,
pagination : { total : 100 , pageSize : 10 , current : 2 } ,
} ) ,
) ;
2022-06-07 10:14:32 +08:00
expect ( container . querySelectorAll ( 'tbody tr' ) ) . toHaveLength ( 5 ) ;
2020-04-10 14:13:14 +08:00
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.' ,
) ;
} ) ;
2020-08-11 16:18:34 +08:00
2020-08-28 15:48:19 +08:00
it ( 'should render pagination after last item on last page being removed' , ( ) => {
const total = data . length ;
const paginationProp = {
pageSize : 1 ,
total ,
current : total ,
position : [ 'topLeft' , 'bottomLeft' ] ,
} ;
2022-06-07 10:14:32 +08:00
const { container , rerender } = render (
2020-08-28 15:48:19 +08:00
createTable ( {
pagination : paginationProp ,
} ) ,
2020-08-11 16:18:34 +08:00
) ;
2022-06-07 10:14:32 +08:00
rerender (
createTable ( {
dataSource : data . slice ( total - 1 ) ,
pagination : { ... paginationProp , total : total - 1 } ,
} ) ,
) ;
2020-08-11 16:18:34 +08:00
2022-06-07 10:14:32 +08:00
expect ( container . querySelectorAll ( '.ant-pagination' ) ) . toHaveLength ( 2 ) ;
2020-08-11 16:18:34 +08:00
} ) ;
2020-10-27 23:02:52 +08:00
it ( 'showTotal should hide when removed' , ( ) => {
const Demo = ( ) => {
const [ p , setP ] = React . useState ( {
showTotal : t => ` > ${ t } < ` ,
total : 200 ,
current : 1 ,
pageSize : 10 ,
} ) ;
return (
< Table
data = { [ ] }
columns = { [ ] }
pagination = { p }
onChange = { pg => {
setP ( {
... pg ,
total : 23 ,
} ) ;
} }
/ >
) ;
} ;
2022-06-07 10:14:32 +08:00
const { container } = render ( < Demo / > ) ;
expect ( container . querySelector ( '.ant-pagination-total-text' ) . textContent ) . toEqual ( '>200<' ) ;
2020-10-27 23:02:52 +08:00
// Should hide
2022-06-07 10:14:32 +08:00
fireEvent . click ( container . querySelector ( '.ant-pagination-item-2' ) ) ;
expect ( container . querySelectorAll ( '.ant-pagination-total-text' ) ) . toHaveLength ( 0 ) ;
2020-10-27 23:02:52 +08:00
} ) ;
2021-09-11 21:44:03 +08:00
it ( 'should preserve table pagination className' , ( ) => {
2022-06-07 10:14:32 +08:00
const { container } = render (
2021-09-11 21:44:03 +08:00
< Table
data = { [ ] }
columns = { [ ] }
pagination = { {
className : 'pagination' ,
total : 200 ,
current : 1 ,
pageSize : 10 ,
} }
/ > ,
) ;
2022-06-07 10:14:32 +08:00
expect ( container . querySelector ( '.ant-pagination' ) . className ) . toEqual (
2021-09-11 21:44:03 +08:00
'ant-pagination ant-table-pagination ant-table-pagination-right pagination' ,
) ;
} ) ;
2016-12-14 14:48:09 +08:00
} ) ;