-
Notifications
You must be signed in to change notification settings - Fork 1.6k
chore(ui): Add page size dropdown option to MUI table pagination #24784
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
38a970d
chore(ui): Add page size dropdown option to MUI table pagination
shah-harshit e5c5597
add tests and address copilot comments
shah-harshit 94712b1
nit
shah-harshit 6d373c0
address comments
shah-harshit b626f01
Merge branch 'main' into add-page-size-dropdown
shah-harshit dac4e36
remove negative condition
shah-harshit 8d954d1
Merge branch 'main' into add-page-size-dropdown
shah-harshit 8769de4
Merge branch 'main' into add-page-size-dropdown
shah-harshit 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
311 changes: 311 additions & 0 deletions
311
...ui/src/main/resources/ui/src/components/common/atoms/compositions/useListingData.test.tsx
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,311 @@ | ||
| /* | ||
| * Copyright 2025 Collate. | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { act, renderHook } from '@testing-library/react'; | ||
| import { MemoryRouter } from 'react-router-dom'; | ||
| import { SearchIndex } from '../../../../enums/search.enum'; | ||
| import { useListingData } from './useListingData'; | ||
|
|
||
| // Mock dependencies | ||
| jest.mock('../data/useUrlState'); | ||
| jest.mock('../data/useDataFetching'); | ||
| jest.mock('../data/useSelectionState'); | ||
| jest.mock('../pagination/usePaginationState'); | ||
| jest.mock('./useActionHandlers'); | ||
|
|
||
| import { useDataFetching } from '../data/useDataFetching'; | ||
| import { useSelectionState } from '../data/useSelectionState'; | ||
| import { useUrlState } from '../data/useUrlState'; | ||
| import { usePaginationState } from '../pagination/usePaginationState'; | ||
| import { useActionHandlers } from './useActionHandlers'; | ||
|
|
||
| const mockSetPageSize = jest.fn(); | ||
| const mockSetSearchQuery = jest.fn(); | ||
| const mockSetFilters = jest.fn(); | ||
| const mockSetCurrentPage = jest.fn(); | ||
|
|
||
| const mockUseUrlState = useUrlState as jest.MockedFunction<typeof useUrlState>; | ||
| const mockUseDataFetching = useDataFetching as jest.MockedFunction< | ||
| typeof useDataFetching | ||
| >; | ||
| const mockUseSelectionState = useSelectionState as jest.MockedFunction< | ||
| typeof useSelectionState | ||
| >; | ||
| const mockUsePaginationState = usePaginationState as jest.MockedFunction< | ||
| typeof usePaginationState | ||
| >; | ||
| const mockUseActionHandlers = useActionHandlers as jest.MockedFunction< | ||
| typeof useActionHandlers | ||
| >; | ||
|
|
||
| const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
| <MemoryRouter>{children}</MemoryRouter> | ||
| ); | ||
|
|
||
| describe('useListingData', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| mockUseUrlState.mockReturnValue({ | ||
| urlState: { | ||
| searchQuery: '', | ||
| filters: {}, | ||
| currentPage: 1, | ||
| pageSize: 10, | ||
| }, | ||
| parsedFilters: [], | ||
| setSearchQuery: mockSetSearchQuery, | ||
| setFilters: mockSetFilters, | ||
| setCurrentPage: mockSetCurrentPage, | ||
| setPageSize: mockSetPageSize, | ||
| resetFilters: jest.fn(), | ||
| resetAll: jest.fn(), | ||
| }); | ||
|
|
||
| mockUseDataFetching.mockReturnValue({ | ||
| entities: [], | ||
| loading: false, | ||
| error: null, | ||
| totalEntities: 0, | ||
| refetch: jest.fn(), | ||
| searchEntities: jest.fn(), | ||
| aggregations: {}, | ||
| }); | ||
|
|
||
| mockUseSelectionState.mockReturnValue({ | ||
| selectedEntities: [], | ||
| isAllSelected: false, | ||
| isIndeterminate: false, | ||
| handleSelectAll: jest.fn(), | ||
| handleSelect: jest.fn(), | ||
| clearSelection: jest.fn(), | ||
| isSelected: jest.fn(), | ||
| }); | ||
|
|
||
| mockUsePaginationState.mockReturnValue({ | ||
| currentPage: 1, | ||
| totalPages: 1, | ||
| pageSize: 10, | ||
| totalEntities: 0, | ||
| setCurrentPage: jest.fn(), | ||
| }); | ||
|
|
||
| mockUseActionHandlers.mockReturnValue({ | ||
| onEntityClick: jest.fn(), | ||
| onAddClick: jest.fn(), | ||
| onDeleteClick: jest.fn(), | ||
| onEditClick: jest.fn(), | ||
| }); | ||
| }); | ||
|
|
||
| describe('handlePageSizeChange', () => { | ||
| it('should call setPageSize with the provided size', () => { | ||
| const { result } = renderHook( | ||
| () => | ||
| useListingData({ | ||
| searchIndex: SearchIndex.TABLE, | ||
| filterKeys: [], | ||
| columns: [], | ||
| pageSize: 10, | ||
| }), | ||
| { wrapper } | ||
| ); | ||
|
|
||
| act(() => { | ||
| result.current.handlePageSizeChange?.(25); | ||
| }); | ||
|
|
||
| expect(mockSetPageSize).toHaveBeenCalledWith(25); | ||
| expect(mockSetPageSize).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should handle multiple page size changes', () => { | ||
| const { result } = renderHook( | ||
| () => | ||
| useListingData({ | ||
| searchIndex: SearchIndex.TABLE, | ||
| filterKeys: [], | ||
| columns: [], | ||
| pageSize: 10, | ||
| }), | ||
| { wrapper } | ||
| ); | ||
|
|
||
| act(() => { | ||
| result.current.handlePageSizeChange?.(25); | ||
| }); | ||
|
|
||
| act(() => { | ||
| result.current.handlePageSizeChange?.(50); | ||
| }); | ||
|
|
||
| act(() => { | ||
| result.current.handlePageSizeChange?.(10); | ||
| }); | ||
|
|
||
| expect(mockSetPageSize).toHaveBeenCalledTimes(3); | ||
| expect(mockSetPageSize).toHaveBeenNthCalledWith(1, 25); | ||
| expect(mockSetPageSize).toHaveBeenNthCalledWith(2, 50); | ||
| expect(mockSetPageSize).toHaveBeenNthCalledWith(3, 10); | ||
| }); | ||
|
|
||
| it('should delegate to setPageSize which resets page to 1', () => { | ||
| mockSetPageSize.mockImplementation(() => { | ||
| // Simulate the behavior of setPageSize in useUrlState | ||
| // which sets page to '1' when page size changes | ||
| }); | ||
|
|
||
| const { result } = renderHook( | ||
| () => | ||
| useListingData({ | ||
| searchIndex: SearchIndex.TABLE, | ||
| filterKeys: [], | ||
| columns: [], | ||
| pageSize: 10, | ||
| }), | ||
| { wrapper } | ||
| ); | ||
|
|
||
| // Simulate being on page 3 | ||
| mockUseUrlState.mockReturnValue({ | ||
| urlState: { | ||
| searchQuery: '', | ||
| filters: {}, | ||
| currentPage: 3, | ||
| pageSize: 10, | ||
| }, | ||
| parsedFilters: [], | ||
| setSearchQuery: mockSetSearchQuery, | ||
| setFilters: mockSetFilters, | ||
| setCurrentPage: mockSetCurrentPage, | ||
| setPageSize: mockSetPageSize, | ||
| resetFilters: jest.fn(), | ||
| resetAll: jest.fn(), | ||
| }); | ||
|
|
||
| act(() => { | ||
| result.current.handlePageSizeChange?.(25); | ||
| }); | ||
|
|
||
| expect(mockSetPageSize).toHaveBeenCalledWith(25); | ||
| // The setPageSize function should reset page to 1 | ||
| // This is tested in useUrlState.test.tsx | ||
| }); | ||
|
|
||
| it('should be defined when useListingData is called', () => { | ||
| const { result } = renderHook( | ||
| () => | ||
| useListingData({ | ||
| searchIndex: SearchIndex.TABLE, | ||
| filterKeys: [], | ||
| columns: [], | ||
| pageSize: 10, | ||
| }), | ||
| { wrapper } | ||
| ); | ||
|
|
||
| expect(result.current.handlePageSizeChange).toBeDefined(); | ||
| expect(typeof result.current.handlePageSizeChange).toBe('function'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('effectivePageSize fallback', () => { | ||
| it('should use urlState.pageSize when available', () => { | ||
| mockUseUrlState.mockReturnValueOnce({ | ||
| urlState: { | ||
| searchQuery: '', | ||
| filters: {}, | ||
| currentPage: 1, | ||
| pageSize: 25, | ||
| }, | ||
| parsedFilters: [], | ||
| setSearchQuery: mockSetSearchQuery, | ||
| setFilters: mockSetFilters, | ||
| setCurrentPage: mockSetCurrentPage, | ||
| setPageSize: mockSetPageSize, | ||
| resetFilters: jest.fn(), | ||
| resetAll: jest.fn(), | ||
| }); | ||
|
|
||
| mockUsePaginationState.mockReturnValueOnce({ | ||
| currentPage: 1, | ||
| totalPages: 1, | ||
| pageSize: 25, | ||
| totalEntities: 0, | ||
| setCurrentPage: jest.fn(), | ||
| }); | ||
|
|
||
| const { result } = renderHook( | ||
| () => | ||
| useListingData({ | ||
| searchIndex: SearchIndex.TABLE, | ||
| filterKeys: [], | ||
| columns: [], | ||
| pageSize: 10, | ||
| }), | ||
| { wrapper } | ||
| ); | ||
|
|
||
| expect(result.current.pageSize).toBe(25); | ||
| expect(mockUseDataFetching).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| pageSize: 25, | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('should fallback to props.pageSize when urlState.pageSize is 0', () => { | ||
| mockUseUrlState.mockReturnValueOnce({ | ||
| urlState: { | ||
| searchQuery: '', | ||
| filters: {}, | ||
| currentPage: 1, | ||
| pageSize: 0, | ||
| }, | ||
| parsedFilters: [], | ||
| setSearchQuery: mockSetSearchQuery, | ||
| setFilters: mockSetFilters, | ||
| setCurrentPage: mockSetCurrentPage, | ||
| setPageSize: mockSetPageSize, | ||
| resetFilters: jest.fn(), | ||
| resetAll: jest.fn(), | ||
| }); | ||
|
|
||
| mockUsePaginationState.mockReturnValueOnce({ | ||
| currentPage: 1, | ||
| totalPages: 1, | ||
| pageSize: 10, | ||
| totalEntities: 0, | ||
| setCurrentPage: jest.fn(), | ||
| }); | ||
|
|
||
| const { result } = renderHook( | ||
| () => | ||
| useListingData({ | ||
| searchIndex: SearchIndex.TABLE, | ||
| filterKeys: [], | ||
| columns: [], | ||
| pageSize: 10, | ||
| }), | ||
| { wrapper } | ||
| ); | ||
|
|
||
| expect(result.current.pageSize).toBe(10); | ||
| expect(mockUseDataFetching).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| pageSize: 10, | ||
| }) | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.