-
Notifications
You must be signed in to change notification settings - Fork 750
OCPBUGS-113611: Keep Projects list sort after kebab delete #17088
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
Open
kchawlani19
wants to merge
4
commits into
openshift:main
Choose a base branch
from
kchawlani19:OCPBUGS-113611-preserve-projects-list-sort
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5de2305
OCPBUGS-113611: Keep Projects list sort after kebab delete
kchawlani19 c46d48d
OCPBUGS-113611: Preserve ConsoleDataView sort by column id
kchawlani19 89c2f5c
OCPBUGS-113611: Stop exporting unused getColumnSortKey
kchawlani19 302041c
OCPBUGS-113611: Fix frontend lint on ConsoleDataView sort
kchawlani19 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
152 changes: 152 additions & 0 deletions
152
...d/packages/console-app/src/components/data-view/__tests__/useConsoleDataViewSort.spec.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,152 @@ | ||
| import type { ReactNode } from 'react'; | ||
| import { SortByDirection } from '@patternfly/react-table'; | ||
| import { act, renderHook } from '@testing-library/react'; | ||
| import { MemoryRouter, useSearchParams } from 'react-router'; | ||
| import type { ConsoleDataViewColumn } from '../types'; | ||
| import { findSortColumnIndex, useConsoleDataViewSort } from '../useConsoleDataViewSort'; | ||
|
|
||
| const columns: ConsoleDataViewColumn<unknown>[] = [ | ||
| { id: 'name', title: 'Name', sort: 'metadata.name', cell: 'Name' }, | ||
| { | ||
| id: 'requester', | ||
| title: 'Requester', | ||
| sort: "metadata.annotations['openshift.io/requester']", | ||
| cell: 'Requester', | ||
| }, | ||
| { id: 'created', title: 'Created', sort: 'metadata.creationTimestamp', cell: 'Created' }, | ||
| ]; | ||
|
|
||
| describe('findSortColumnIndex', () => { | ||
| it('matches a column by id', () => { | ||
| expect(findSortColumnIndex(columns, 'requester')).toBe(1); | ||
| }); | ||
|
|
||
| it('matches a column by title for existing URLs', () => { | ||
| expect(findSortColumnIndex(columns, 'Requester')).toBe(1); | ||
| }); | ||
|
|
||
| it('returns -1 when the key is missing', () => { | ||
| expect(findSortColumnIndex(columns, null)).toBe(-1); | ||
| expect(findSortColumnIndex(columns, 'unknown')).toBe(-1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('useConsoleDataViewSort', () => { | ||
| const wrapper = | ||
| (initialEntry: string) => | ||
| ({ children }: { children: ReactNode }) => ( | ||
| <MemoryRouter initialEntries={[initialEntry]}>{children}</MemoryRouter> | ||
| ); | ||
|
|
||
| const wrapperWithSearchControl = (initialEntry: string) => { | ||
| const searchParamsApi: { clearSortBy: () => void } = { clearSortBy: () => undefined }; | ||
|
|
||
| const SearchParamsBridge = () => { | ||
| const [searchParams, setSearchParams] = useSearchParams(); | ||
| searchParamsApi.clearSortBy = () => { | ||
| const next = new URLSearchParams(searchParams); | ||
| next.delete('sortBy'); | ||
| setSearchParams(next, { replace: true }); | ||
| }; | ||
| return null; | ||
| }; | ||
|
|
||
| return { | ||
| wrapper: ({ children }: { children: ReactNode }) => ( | ||
| <MemoryRouter initialEntries={[initialEntry]}> | ||
| <SearchParamsBridge /> | ||
| {children} | ||
| </MemoryRouter> | ||
| ), | ||
| clearSortBy: () => searchParamsApi.clearSortBy(), | ||
| }; | ||
| }; | ||
|
|
||
| it('restores sort from the sortBy query param after columns are rebuilt', () => { | ||
| const { result, rerender } = renderHook( | ||
| ({ cols }) => useConsoleDataViewSort({ columns: cols }), | ||
| { | ||
| wrapper: wrapper('/k8s/cluster/projects?sortBy=Requester&orderBy=asc'), | ||
| initialProps: { cols: columns }, | ||
| }, | ||
| ); | ||
|
|
||
| expect(result.current.sortBy).toEqual({ index: 1, direction: SortByDirection.asc }); | ||
|
|
||
| rerender({ cols: [...columns] }); | ||
|
|
||
| expect(result.current.sortBy).toEqual({ index: 1, direction: SortByDirection.asc }); | ||
| }); | ||
|
|
||
| it('keeps the current sort column when columns rebuild without a sortBy param', () => { | ||
| const { wrapper: testWrapper, clearSortBy } = wrapperWithSearchControl( | ||
| '/k8s/cluster/projects?sortBy=requester&orderBy=desc', | ||
| ); | ||
| const { result, rerender } = renderHook( | ||
| ({ cols }) => useConsoleDataViewSort({ columns: cols }), | ||
| { | ||
| wrapper: testWrapper, | ||
| initialProps: { cols: columns }, | ||
| }, | ||
| ); | ||
|
|
||
| expect(result.current.sortBy.index).toBe(1); | ||
| expect(result.current.sortBy.direction).toBe(SortByDirection.desc); | ||
|
|
||
| act(() => { | ||
| clearSortBy(); | ||
| }); | ||
|
|
||
| rerender({ cols: columns.map((c) => ({ ...c })) }); | ||
|
|
||
| expect(result.current.sortBy.index).toBe(1); | ||
| expect(result.current.sortBy.direction).toBe(SortByDirection.desc); | ||
| }); | ||
|
|
||
| it('follows the selected column id when columns are reordered without a sortBy param', () => { | ||
| const { wrapper: testWrapper, clearSortBy } = wrapperWithSearchControl( | ||
| '/k8s/cluster/projects?sortBy=requester&orderBy=desc', | ||
| ); | ||
| const { result, rerender } = renderHook( | ||
| ({ cols }) => useConsoleDataViewSort({ columns: cols }), | ||
| { | ||
| wrapper: testWrapper, | ||
| initialProps: { cols: columns }, | ||
| }, | ||
| ); | ||
|
|
||
| expect(result.current.sortBy.index).toBe(1); | ||
|
|
||
| act(() => { | ||
| clearSortBy(); | ||
| }); | ||
|
|
||
| rerender({ cols: [columns[0], columns[2], columns[1]].map((c) => ({ ...c })) }); | ||
|
|
||
| expect(result.current.sortBy.index).toBe(2); | ||
| expect(result.current.sortBy.direction).toBe(SortByDirection.desc); | ||
| }); | ||
|
|
||
| it('falls back to the default sort when the selected column is removed and sortBy is absent', () => { | ||
| const { wrapper: testWrapper, clearSortBy } = wrapperWithSearchControl( | ||
| '/k8s/cluster/projects?sortBy=requester&orderBy=desc', | ||
| ); | ||
| const { result, rerender } = renderHook( | ||
| ({ cols }) => useConsoleDataViewSort({ columns: cols }), | ||
| { | ||
| wrapper: testWrapper, | ||
| initialProps: { cols: columns }, | ||
| }, | ||
| ); | ||
|
|
||
| expect(result.current.sortBy.index).toBe(1); | ||
|
|
||
| act(() => { | ||
| clearSortBy(); | ||
| }); | ||
|
|
||
| rerender({ cols: [columns[0], columns[2]].map((c) => ({ ...c })) }); | ||
|
|
||
| expect(result.current.sortBy).toEqual({ index: 0, direction: SortByDirection.asc }); | ||
| }); | ||
| }); | ||
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
29 changes: 29 additions & 0 deletions
29
frontend/public/components/modals/__tests__/delete-namespace-modal.spec.ts
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,29 @@ | ||
| import { | ||
| getClusterResourceListPath, | ||
| isOnClusterResourceListPage, | ||
| } from '../delete-namespace-modal-utils'; | ||
|
|
||
| describe('isOnClusterResourceListPage', () => { | ||
| it('returns true for the Projects list path', () => { | ||
| expect(isOnClusterResourceListPage('/k8s/cluster/projects', 'projects')).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true when the list path has a trailing slash', () => { | ||
| expect(isOnClusterResourceListPage('/k8s/cluster/projects/', 'projects')).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false for a project details path', () => { | ||
| expect(isOnClusterResourceListPage('/k8s/cluster/projects/my-app', 'projects')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for a namespaced page of the deleted project', () => { | ||
| expect(isOnClusterResourceListPage('/k8s/ns/my-app/pods', 'projects')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getClusterResourceListPath', () => { | ||
| it('builds the cluster-scoped list path from the model plural', () => { | ||
| expect(getClusterResourceListPath('projects')).toBe('/k8s/cluster/projects'); | ||
| expect(getClusterResourceListPath('namespaces')).toBe('/k8s/cluster/namespaces'); | ||
| }); | ||
| }); |
8 changes: 8 additions & 0 deletions
8
frontend/public/components/modals/delete-namespace-modal-utils.ts
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,8 @@ | ||
| /** Cluster-scoped list page for Project / Namespace, e.g. `/k8s/cluster/projects`. */ | ||
| export const getClusterResourceListPath = (plural: string): string => `/k8s/cluster/${plural}`; | ||
|
|
||
| /** True when the user is already on that list page (query params must be preserved). */ | ||
| export const isOnClusterResourceListPage = (pathname: string, plural: string): boolean => { | ||
| const listPath = getClusterResourceListPath(plural); | ||
| return pathname === listPath || pathname === `${listPath}/`; | ||
| }; |
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.