-
Notifications
You must be signed in to change notification settings - Fork 383
feat(table): initialize the scroll bar when switching pages #3684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+148
−2
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9d36815
feat(table): initialize the scroll bar when switching pages
RSS1102 f3e1f15
feat(table): initialize the scroll bar when switching pages
RSS1102 a45124f
fix(pagination): ensure resetScrollbar is always called
RSS1102 3c77d24
refactor(pagination): replace resetScrollbar function with direct scr…
RSS1102 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { afterEach } from 'vitest'; | ||
| import { mount } from '@vue/test-utils'; | ||
| import { | ||
| Table, BaseTable, PrimaryTable, EnhancedTable, | ||
| } from '@/src/table/index.ts'; | ||
|
|
||
| // 与项目中其它分页测试风格保持一致,针对受控分页(on page-change)和切页时滚动回顶部进行断言 | ||
| const TABLES = [Table, BaseTable, PrimaryTable, EnhancedTable]; | ||
|
|
||
| function createData(total) { | ||
| return new Array(total).fill(null).map((_, i) => { | ||
| let applicant = `Name${i + 1}`; | ||
| if (i === 2) applicant = '王芳'; | ||
| if (i === 3) applicant = '贾明'; | ||
| return { | ||
| id: i + 1, | ||
| index: i + 1, | ||
| applicant, | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| TABLES.forEach((TTable) => { | ||
| describe(`${TTable.name} pagination onPageChange & scroll reset`, () => { | ||
| afterEach(() => { | ||
| document.querySelector('.t-popup')?.remove(); | ||
| document.querySelector('.t-table')?.remove(); | ||
| }); | ||
|
|
||
| it('props.pagination: onPageChange should be triggered when switching pages', async () => { | ||
| const onPageChange = vi.fn(); | ||
| const pagination = { | ||
| current: 1, | ||
| pageSize: 2, | ||
| total: 10, | ||
| }; | ||
|
|
||
| const wrapper = mount({ | ||
| data() { | ||
| return { data: createData(10), pagination }; | ||
| }, | ||
| render() { | ||
| return ( | ||
| <TTable | ||
| rowKey="index" | ||
| data={this.data} | ||
| columns={[ | ||
| { title: 'index', colKey: 'index' }, | ||
| { title: 'applicant', colKey: 'applicant' }, | ||
| ]} | ||
| pagination={this.pagination} | ||
| on={{ 'page-change': onPageChange }} | ||
| ></TTable> | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| expect(wrapper.find('.t-table__pagination').exists()).toBeTruthy(); | ||
| const nextButton = wrapper.find('.t-pagination__btn-next'); | ||
| expect(nextButton.exists()).toBeTruthy(); | ||
|
|
||
| await nextButton.trigger('click'); | ||
|
|
||
| expect(onPageChange).toHaveBeenCalledTimes(1); | ||
| expect(onPageChange).toHaveBeenCalledWith( | ||
| expect.objectContaining({ current: 2, pageSize: 2, previous: 1 }), | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ index: 3, applicant: '王芳' }), | ||
| expect.objectContaining({ index: 4, applicant: '贾明' }), | ||
| ]), | ||
| ); | ||
| }); | ||
|
|
||
| it('props.pagination: scroll position should reset when switching pages', async () => { | ||
| const onPageChange = vi.fn(); | ||
| const pagination = { | ||
| current: 1, | ||
| pageSize: 2, | ||
| total: 50, | ||
| }; | ||
|
|
||
| const wrapper = mount({ | ||
| data() { | ||
| return { data: createData(50), pagination }; | ||
| }, | ||
| render() { | ||
| return ( | ||
| <TTable | ||
| rowKey="index" | ||
| data={this.data} | ||
| columns={[ | ||
| { title: 'index', colKey: 'index' }, | ||
| { title: 'applicant', colKey: 'applicant' }, | ||
| ]} | ||
| pagination={this.pagination} | ||
| on={{ 'page-change': onPageChange }} | ||
| maxHeight={200} | ||
| ></TTable> | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| expect(wrapper.find('.t-table__pagination').exists()).toBeTruthy(); | ||
| const tableContent = wrapper.find('.t-table__content'); | ||
| expect(tableContent.exists()).toBeTruthy(); | ||
|
|
||
| const scrollElement = tableContent.element; | ||
|
|
||
| // JSDOM 下 scrollHeight/clientHeight 默认为 0,需要 mock | ||
| Object.defineProperty(scrollElement, 'scrollHeight', { value: 100, configurable: true }); | ||
| Object.defineProperty(scrollElement, 'clientHeight', { value: 50, configurable: true }); | ||
|
|
||
| expect(scrollElement.scrollHeight).toBeGreaterThan(scrollElement.clientHeight); | ||
| expect(scrollElement.scrollTop).toBe(0); | ||
|
|
||
| // 模拟滚动到底部 | ||
| scrollElement.scrollTop = 100; | ||
| expect(scrollElement.scrollTop).toBe(100); | ||
|
|
||
| const nextButton = wrapper.find('.t-pagination__btn-next'); | ||
| expect(nextButton.exists()).toBeTruthy(); | ||
| await nextButton.trigger('click'); | ||
|
|
||
| expect(onPageChange).toHaveBeenCalledTimes(1); | ||
| // 切页后滚动回到顶部 | ||
| expect(scrollElement.scrollTop).toBe(0); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.