|
| 1 | + |
| 2 | +import { mount } from '@vue/test-utils' |
| 3 | +import Table from '..' |
| 4 | +import Vue from 'vue' |
| 5 | + |
| 6 | +describe('Table.pagination', () => { |
| 7 | + const columns = [{ |
| 8 | + title: 'Name', |
| 9 | + dataIndex: 'name', |
| 10 | + }] |
| 11 | + |
| 12 | + const data = [ |
| 13 | + { key: 0, name: 'Jack' }, |
| 14 | + { key: 1, name: 'Lucy' }, |
| 15 | + { key: 2, name: 'Tom' }, |
| 16 | + { key: 3, name: 'Jerry' }, |
| 17 | + ] |
| 18 | + |
| 19 | + const pagination = { class: 'my-page', pageSize: 2 } |
| 20 | + |
| 21 | + function getTableOptions (props = {}, listeners = {}) { |
| 22 | + return { |
| 23 | + propsData: { |
| 24 | + columns, |
| 25 | + dataSource: data, |
| 26 | + pagination, |
| 27 | + ...props, |
| 28 | + }, |
| 29 | + listeners: { |
| 30 | + ...listeners, |
| 31 | + }, |
| 32 | + sync: false, |
| 33 | + } |
| 34 | + // return { |
| 35 | + // render () { |
| 36 | + // return ( |
| 37 | + // <Table |
| 38 | + // {...tableProps} |
| 39 | + // /> |
| 40 | + // ) |
| 41 | + // }, |
| 42 | + // } |
| 43 | + } |
| 44 | + |
| 45 | + function renderedNames (wrapper) { |
| 46 | + return wrapper.findAll({ name: 'TableRow' }).wrappers.map(row => { |
| 47 | + return row.props().record.name |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + it('renders pagination correctly', (done) => { |
| 52 | + const wrapper = mount(Table, getTableOptions()) |
| 53 | + Vue.nextTick(() => { |
| 54 | + expect(wrapper.html()).toMatchSnapshot() |
| 55 | + done() |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should not show pager if pagination.hideOnSinglePage is true and only 1 page', (done) => { |
| 60 | + const wrapper = mount(Table, getTableOptions({ pagination: { pageSize: 3, hideOnSinglePage: true }})) |
| 61 | + Vue.nextTick(() => { |
| 62 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(1) |
| 63 | + wrapper.setProps({ pagination: { pageSize: 3, hideOnSinglePage: false }}) |
| 64 | + Vue.nextTick(() => { |
| 65 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(1) |
| 66 | + wrapper.setProps({ pagination: { pageSize: 4, hideOnSinglePage: true }}) |
| 67 | + Vue.nextTick(() => { |
| 68 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(0) |
| 69 | + wrapper.setProps({ pagination: { pageSize: 4, hideOnSinglePage: false }}) |
| 70 | + Vue.nextTick(() => { |
| 71 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(1) |
| 72 | + wrapper.setProps({ pagination: { pageSize: 5, hideOnSinglePage: true }}) |
| 73 | + Vue.nextTick(() => { |
| 74 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(0) |
| 75 | + wrapper.setProps({ pagination: { pageSize: 5, hideOnSinglePage: false }}) |
| 76 | + Vue.nextTick(() => { |
| 77 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(1) |
| 78 | + done() |
| 79 | + }) |
| 80 | + }) |
| 81 | + }) |
| 82 | + }) |
| 83 | + }) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + it('paginate data', (done) => { |
| 88 | + const wrapper = mount(Table, getTableOptions()) |
| 89 | + Vue.nextTick(() => { |
| 90 | + expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy']) |
| 91 | + const pager = wrapper.findAll({ name: 'Pager' }) |
| 92 | + pager.at(pager.length - 1).trigger('click') |
| 93 | + Vue.nextTick(() => { |
| 94 | + expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']) |
| 95 | + done() |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + it('repaginates when pageSize change', () => { |
| 101 | + const wrapper = mount(Table, getTableOptions()) |
| 102 | + wrapper.setProps({ pagination: { pageSize: 1 }}) |
| 103 | + Vue.nextTick(() => { |
| 104 | + expect(renderedNames(wrapper)).toEqual(['Jack']) |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + it('fires change event', (done) => { |
| 109 | + const handleChange = jest.fn() |
| 110 | + const handlePaginationChange = jest.fn() |
| 111 | + const noop = () => {} |
| 112 | + const wrapper = mount(Table, getTableOptions({ |
| 113 | + pagination: { ...pagination, onChange: handlePaginationChange, onShowSizeChange: noop }, |
| 114 | + |
| 115 | + }, { change: handleChange })) |
| 116 | + Vue.nextTick(() => { |
| 117 | + const pager = wrapper.findAll({ name: 'Pager' }) |
| 118 | + pager.at(pager.length - 1).trigger('click') |
| 119 | + |
| 120 | + expect(handleChange).toBeCalledWith( |
| 121 | + { |
| 122 | + class: 'my-page', |
| 123 | + current: 2, |
| 124 | + pageSize: 2, |
| 125 | + }, |
| 126 | + {}, |
| 127 | + {} |
| 128 | + ) |
| 129 | + |
| 130 | + expect(handlePaginationChange).toBeCalledWith(2, 2) |
| 131 | + done() |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + // https://github.com/ant-design/ant-design/issues/4532 |
| 136 | + // https://codepen.io/afc163/pen/dVeNoP?editors=001 |
| 137 | + it('should have pager when change pagination from false to undefined', (done) => { |
| 138 | + const wrapper = mount(Table, getTableOptions({ pagination: false })) |
| 139 | + Vue.nextTick(() => { |
| 140 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(0) |
| 141 | + wrapper.setProps({ pagination: undefined }) |
| 142 | + Vue.nextTick(() => { |
| 143 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(1) |
| 144 | + expect(wrapper.findAll('.ant-pagination-item-active')).toHaveLength(1) |
| 145 | + done() |
| 146 | + }) |
| 147 | + }) |
| 148 | + }) |
| 149 | + |
| 150 | + // https://github.com/ant-design/ant-design/issues/4532 |
| 151 | + // https://codepen.io/afc163/pen/pWVRJV?editors=001 |
| 152 | + it('should display pagination as prop pagination change between true and false', (done) => { |
| 153 | + const wrapper = mount(Table, getTableOptions()) |
| 154 | + Vue.nextTick(() => { |
| 155 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(1) |
| 156 | + expect(wrapper.findAll('.ant-pagination-item')).toHaveLength(2) |
| 157 | + wrapper.setProps({ pagination: false }) |
| 158 | + Vue.nextTick(() => { |
| 159 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(0) |
| 160 | + wrapper.setProps({ pagination }) |
| 161 | + // wrapper.update() |
| 162 | + Vue.nextTick(() => { |
| 163 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(1) |
| 164 | + expect(wrapper.findAll('.ant-pagination-item')).toHaveLength(2) |
| 165 | + wrapper.find('.ant-pagination-item-2').trigger('click') |
| 166 | + Vue.nextTick(() => { |
| 167 | + expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']) |
| 168 | + wrapper.setProps({ pagination: false }) |
| 169 | + Vue.nextTick(() => { |
| 170 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(0) |
| 171 | + wrapper.setProps({ pagination: true }) |
| 172 | + Vue.nextTick(() => { |
| 173 | + expect(wrapper.findAll('.ant-pagination')).toHaveLength(1) |
| 174 | + expect(wrapper.findAll('.ant-pagination-item')).toHaveLength(1) // pageSize will be 10 |
| 175 | + expect(renderedNames(wrapper)).toHaveLength(4) |
| 176 | + done() |
| 177 | + }) |
| 178 | + }) |
| 179 | + }) |
| 180 | + }) |
| 181 | + }) |
| 182 | + }) |
| 183 | + }) |
| 184 | + |
| 185 | + // https://github.com/ant-design/ant-design/issues/5259 |
| 186 | + it('change to correct page when data source changes', (done) => { |
| 187 | + const wrapper = mount(Table, getTableOptions({ pagination: { pageSize: 1 }})) |
| 188 | + Vue.nextTick(() => { |
| 189 | + wrapper.find('.ant-pagination-item-3').trigger('click') |
| 190 | + wrapper.setProps({ dataSource: [data[0]] }) |
| 191 | + Vue.nextTick(() => { |
| 192 | + expect(wrapper.find('.ant-pagination-item-1').classes()).toContain('ant-pagination-item-active') |
| 193 | + done() |
| 194 | + }) |
| 195 | + }) |
| 196 | + }) |
| 197 | + |
| 198 | + it('specify the position of pagination', (done) => { |
| 199 | + const wrapper = mount(Table, getTableOptions({ pagination: { position: 'top' }})) |
| 200 | + Vue.nextTick(() => { |
| 201 | + expect(wrapper.findAll('.ant-spin-container > *')).toHaveLength(2) |
| 202 | + expect(wrapper.findAll('.ant-spin-container > *').at(0).findAll('.ant-pagination')).toHaveLength(1) |
| 203 | + wrapper.setProps({ pagination: { position: 'bottom' }}) |
| 204 | + Vue.nextTick(() => { |
| 205 | + expect(wrapper.findAll('.ant-spin-container > *')).toHaveLength(2) |
| 206 | + expect(wrapper.findAll('.ant-spin-container > *').at(1).findAll('.ant-pagination')).toHaveLength(1) |
| 207 | + wrapper.setProps({ pagination: { position: 'both' }}) |
| 208 | + Vue.nextTick(() => { |
| 209 | + expect(wrapper.findAll('.ant-spin-container > *')).toHaveLength(3) |
| 210 | + expect(wrapper.findAll('.ant-spin-container > *').at(0).findAll('.ant-pagination')).toHaveLength(1) |
| 211 | + expect(wrapper.findAll('.ant-spin-container > *').at(2).findAll('.ant-pagination')).toHaveLength(1) |
| 212 | + done() |
| 213 | + }) |
| 214 | + }) |
| 215 | + }) |
| 216 | + }) |
| 217 | +}) |
0 commit comments