|
1 | 1 | import React from 'react'; |
2 | 2 | import { render, fireEvent } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
3 | 4 | import Select from 'rc-select'; |
4 | 5 | import Pagination from '../src'; |
5 | 6 |
|
@@ -82,4 +83,75 @@ describe('Pagination with sizer', () => { |
82 | 83 | container.querySelector('.rc-select-selection-item'), |
83 | 84 | ).toHaveTextContent('20 条/页'); |
84 | 85 | }); |
| 86 | + |
| 87 | + describe('Pagination with showSizeChanger', () => { |
| 88 | + const options = [ |
| 89 | + { value: '10', label: '10 条每页' }, |
| 90 | + { value: '25', label: '25 条每页' }, |
| 91 | + { value: '50', label: '50 条每页' }, |
| 92 | + { value: '75', label: '75 条每页' }, |
| 93 | + { value: '100', label: '100 条每页' }, |
| 94 | + ]; |
| 95 | + it('should onChange called when pageSize change', () => { |
| 96 | + const onChange = jest.fn(); |
| 97 | + const { container, getByRole } = render( |
| 98 | + <Pagination |
| 99 | + defaultCurrent={1} |
| 100 | + total={500} |
| 101 | + selectComponentClass={Select} |
| 102 | + showSizeChanger={{ |
| 103 | + options, |
| 104 | + onChange, |
| 105 | + }} |
| 106 | + />, |
| 107 | + ); |
| 108 | + const select = getByRole('combobox'); |
| 109 | + expect(select).toBeTruthy(); |
| 110 | + fireEvent.mouseDown(select); |
| 111 | + expect( |
| 112 | + container.querySelectorAll('.rc-select-item')[2], |
| 113 | + ).toHaveTextContent('50 条每页'); |
| 114 | + const pageSize1 = container.querySelectorAll('.rc-select-item')[1]; |
| 115 | + fireEvent.click(pageSize1); |
| 116 | + expect(onChange).toHaveBeenCalledWith('25', { |
| 117 | + label: '25 条每页', |
| 118 | + value: '25', |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should onChange called when pageSize change with search', async () => { |
| 123 | + const onChange = jest.fn(); |
| 124 | + const { container } = render( |
| 125 | + <Pagination |
| 126 | + defaultCurrent={1} |
| 127 | + total={500} |
| 128 | + selectComponentClass={Select} |
| 129 | + showSizeChanger={{ |
| 130 | + options, |
| 131 | + showSearch: true, |
| 132 | + onChange, |
| 133 | + }} |
| 134 | + />, |
| 135 | + ); |
| 136 | + expect(container.querySelector('input').hasAttribute('readOnly')).toBe( |
| 137 | + false, |
| 138 | + ); |
| 139 | + await userEvent.type(container.querySelector('input'), '25'); |
| 140 | + expect( |
| 141 | + container.querySelectorAll('.rc-select-item-option-content'), |
| 142 | + ).toHaveLength(1); |
| 143 | + expect( |
| 144 | + container.querySelector('.rc-select-item-option-content').textContent, |
| 145 | + ).toBe('25 条每页'); |
| 146 | + const pageSize1 = container.querySelector( |
| 147 | + '.rc-select-item-option-content', |
| 148 | + ); |
| 149 | + expect(pageSize1).toBeInTheDocument(); |
| 150 | + fireEvent.click(pageSize1); |
| 151 | + expect(onChange).toHaveBeenCalledWith('25', { |
| 152 | + label: '25 条每页', |
| 153 | + value: '25', |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
85 | 157 | }); |
0 commit comments