|
| 1 | +import { afterEach } from 'vitest'; |
| 2 | +import { mount } from '@vue/test-utils'; |
| 3 | +import { |
| 4 | + Table, BaseTable, PrimaryTable, EnhancedTable, |
| 5 | +} from '@/src/table/index.ts'; |
| 6 | + |
| 7 | +// 与项目中其它分页测试风格保持一致,针对受控分页(on page-change)和切页时滚动回顶部进行断言 |
| 8 | +const TABLES = [Table, BaseTable, PrimaryTable, EnhancedTable]; |
| 9 | + |
| 10 | +function createData(total) { |
| 11 | + return new Array(total).fill(null).map((_, i) => { |
| 12 | + let applicant = `Name${i + 1}`; |
| 13 | + if (i === 2) applicant = '王芳'; |
| 14 | + if (i === 3) applicant = '贾明'; |
| 15 | + return { |
| 16 | + id: i + 1, |
| 17 | + index: i + 1, |
| 18 | + applicant, |
| 19 | + }; |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +TABLES.forEach((TTable) => { |
| 24 | + describe(`${TTable.name} pagination onPageChange & scroll reset`, () => { |
| 25 | + afterEach(() => { |
| 26 | + document.querySelector('.t-popup')?.remove(); |
| 27 | + document.querySelector('.t-table')?.remove(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('props.pagination: onPageChange should be triggered when switching pages', async () => { |
| 31 | + const onPageChange = vi.fn(); |
| 32 | + const pagination = { |
| 33 | + current: 1, |
| 34 | + pageSize: 2, |
| 35 | + total: 10, |
| 36 | + }; |
| 37 | + |
| 38 | + const wrapper = mount({ |
| 39 | + data() { |
| 40 | + return { data: createData(10), pagination }; |
| 41 | + }, |
| 42 | + render() { |
| 43 | + return ( |
| 44 | + <TTable |
| 45 | + rowKey="index" |
| 46 | + data={this.data} |
| 47 | + columns={[ |
| 48 | + { title: 'index', colKey: 'index' }, |
| 49 | + { title: 'applicant', colKey: 'applicant' }, |
| 50 | + ]} |
| 51 | + pagination={this.pagination} |
| 52 | + on={{ 'page-change': onPageChange }} |
| 53 | + ></TTable> |
| 54 | + ); |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + expect(wrapper.find('.t-table__pagination').exists()).toBeTruthy(); |
| 59 | + const nextButton = wrapper.find('.t-pagination__btn-next'); |
| 60 | + expect(nextButton.exists()).toBeTruthy(); |
| 61 | + |
| 62 | + await nextButton.trigger('click'); |
| 63 | + |
| 64 | + expect(onPageChange).toHaveBeenCalledTimes(1); |
| 65 | + expect(onPageChange).toHaveBeenCalledWith( |
| 66 | + expect.objectContaining({ current: 2, pageSize: 2, previous: 1 }), |
| 67 | + expect.arrayContaining([ |
| 68 | + expect.objectContaining({ index: 3, applicant: '王芳' }), |
| 69 | + expect.objectContaining({ index: 4, applicant: '贾明' }), |
| 70 | + ]), |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('props.pagination: scroll position should reset when switching pages', async () => { |
| 75 | + const onPageChange = vi.fn(); |
| 76 | + const pagination = { |
| 77 | + current: 1, |
| 78 | + pageSize: 2, |
| 79 | + total: 50, |
| 80 | + }; |
| 81 | + |
| 82 | + const wrapper = mount({ |
| 83 | + data() { |
| 84 | + return { data: createData(50), pagination }; |
| 85 | + }, |
| 86 | + render() { |
| 87 | + return ( |
| 88 | + <TTable |
| 89 | + rowKey="index" |
| 90 | + data={this.data} |
| 91 | + columns={[ |
| 92 | + { title: 'index', colKey: 'index' }, |
| 93 | + { title: 'applicant', colKey: 'applicant' }, |
| 94 | + ]} |
| 95 | + pagination={this.pagination} |
| 96 | + on={{ 'page-change': onPageChange }} |
| 97 | + maxHeight={200} |
| 98 | + ></TTable> |
| 99 | + ); |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(wrapper.find('.t-table__pagination').exists()).toBeTruthy(); |
| 104 | + const tableContent = wrapper.find('.t-table__content'); |
| 105 | + expect(tableContent.exists()).toBeTruthy(); |
| 106 | + |
| 107 | + const scrollElement = tableContent.element; |
| 108 | + |
| 109 | + // JSDOM 下 scrollHeight/clientHeight 默认为 0,需要 mock |
| 110 | + Object.defineProperty(scrollElement, 'scrollHeight', { value: 100, configurable: true }); |
| 111 | + Object.defineProperty(scrollElement, 'clientHeight', { value: 50, configurable: true }); |
| 112 | + |
| 113 | + expect(scrollElement.scrollHeight).toBeGreaterThan(scrollElement.clientHeight); |
| 114 | + expect(scrollElement.scrollTop).toBe(0); |
| 115 | + |
| 116 | + // 模拟滚动到底部 |
| 117 | + scrollElement.scrollTop = 100; |
| 118 | + expect(scrollElement.scrollTop).toBe(100); |
| 119 | + |
| 120 | + const nextButton = wrapper.find('.t-pagination__btn-next'); |
| 121 | + expect(nextButton.exists()).toBeTruthy(); |
| 122 | + await nextButton.trigger('click'); |
| 123 | + |
| 124 | + expect(onPageChange).toHaveBeenCalledTimes(1); |
| 125 | + // 切页后滚动回到顶部 |
| 126 | + expect(scrollElement.scrollTop).toBe(0); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments