11import React from 'react' ;
22import { render , fireEvent } from '@testing-library/react' ;
3+ import userEvent from '@testing-library/user-event' ;
34import Select from 'rc-select' ;
45import Pagination from '../src' ;
56
6- const onChange = jest . fn ( ) ;
7-
87const options = [
98 { value : '10' , label : '10 条每页' } ,
109 { value : '25' , label : '25 条每页' } ,
@@ -15,14 +14,14 @@ const options = [
1514
1615describe ( 'Pagination with showSizeChanger' , ( ) => {
1716 it ( 'should onChange called when pageSize change' , ( ) => {
17+ const onChange = jest . fn ( ) ;
1818 const { container, getByRole } = render (
1919 < Pagination
2020 defaultCurrent = { 1 }
2121 total = { 500 }
2222 selectComponentClass = { Select }
2323 showSizeChanger = { {
2424 options,
25- showSearch : false ,
2625 onChange,
2726 } }
2827 /> ,
@@ -33,27 +32,32 @@ describe('Pagination with showSizeChanger', () => {
3332 expect ( container . querySelectorAll ( '.rc-select-item' ) [ 2 ] ) . toHaveTextContent (
3433 '50 条每页' ,
3534 ) ;
36- const pageSize1 = container . querySelectorAll ( '.rc-select-item' ) [ 0 ] ;
37- expect ( pageSize1 ) . toBeInTheDocument ( ) ;
35+ const pageSize1 = container . querySelectorAll ( '.rc-select-item' ) [ 1 ] ;
3836 fireEvent . click ( pageSize1 ) ;
37+ expect ( onChange ) . toHaveBeenCalledWith ( '25' , {
38+ label : '25 条每页' ,
39+ value : '25' ,
40+ } ) ;
3941 } ) ;
4042
41- it ( 'should onChange called when pageSize change with search' , ( ) => {
43+ it ( 'should onChange called when pageSize change with search' , async ( ) => {
44+ const onChange = jest . fn ( ) ;
4245 const { container } = render (
4346 < Pagination
4447 defaultCurrent = { 1 }
4548 total = { 500 }
4649 selectComponentClass = { Select }
4750 showSizeChanger = { {
4851 options,
49- showSearch : false ,
52+ showSearch : true ,
5053 onChange,
5154 } }
5255 /> ,
5356 ) ;
54- fireEvent . change ( container . querySelector ( 'input' ) , {
55- target : { value : '25' } ,
56- } ) ;
57+ expect ( container . querySelector ( 'input' ) . hasAttribute ( 'readOnly' ) ) . toBe (
58+ false ,
59+ ) ;
60+ await userEvent . type ( container . querySelector ( 'input' ) , '25' ) ;
5761 expect (
5862 container . querySelectorAll ( '.rc-select-item-option-content' ) ,
5963 ) . toHaveLength ( 1 ) ;
@@ -63,5 +67,9 @@ describe('Pagination with showSizeChanger', () => {
6367 const pageSize1 = container . querySelector ( '.rc-select-item-option-content' ) ;
6468 expect ( pageSize1 ) . toBeInTheDocument ( ) ;
6569 fireEvent . click ( pageSize1 ) ;
70+ expect ( onChange ) . toHaveBeenCalledWith ( '25' , {
71+ label : '25 条每页' ,
72+ value : '25' ,
73+ } ) ;
6674 } ) ;
6775} ) ;
0 commit comments