-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(settings): make it possible to open settings modal based on quer…
…y-params (#13996) Co-authored-by: William Thorenfeldt <[email protected]>
- Loading branch information
1 parent
69d00c9
commit 439262e
Showing
4 changed files
with
115 additions
and
16 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
frontend/app-development/hooks/useOpenSettingsModalBasedQueryParam.test.ts
This file contains 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,59 @@ | ||
import { renderHookWithProviders } from '../test/mocks'; | ||
import { | ||
queryParamKey, | ||
useOpenSettingsModalBasedQueryParam, | ||
} from './useOpenSettingsModalBasedQueryParam'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { useSettingsModalContext } from '../contexts/SettingsModalContext'; | ||
import { waitFor } from '@testing-library/react'; | ||
|
||
jest.mock('../contexts/SettingsModalContext'); | ||
jest.mock('react-router-dom', () => ({ | ||
useSearchParams: jest.fn(), | ||
})); | ||
|
||
describe('useOpenSettingsModalBasedQueryParam', () => { | ||
it('should open "settingsModal" if query params has valid tab id', async () => { | ||
const searchParams = buildSearchParams('about'); | ||
setupSearchParamMock(searchParams); | ||
|
||
const openSettingsMock = jest.fn(); | ||
setupUseSettingsModalContextMock(openSettingsMock); | ||
|
||
renderHookWithProviders()(() => useOpenSettingsModalBasedQueryParam()); | ||
await waitFor(() => expect(openSettingsMock).toHaveBeenCalledWith('about')); | ||
expect(openSettingsMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not open "settingsModal" if query params has an invalid tab id', async () => { | ||
const searchParams = buildSearchParams('doestNotExistTab'); | ||
setupSearchParamMock(searchParams); | ||
|
||
const openSettingsMock = jest.fn(); | ||
setupUseSettingsModalContextMock(openSettingsMock); | ||
|
||
renderHookWithProviders()(() => useOpenSettingsModalBasedQueryParam()); | ||
await waitFor(() => expect(openSettingsMock).not.toHaveBeenCalledWith('doestNotExistTab')); | ||
expect(openSettingsMock).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
|
||
function setupSearchParamMock(searchParams: URLSearchParams): jest.Mock { | ||
return (useSearchParams as jest.Mock).mockReturnValue([searchParams, jest.fn()]); | ||
} | ||
|
||
function buildSearchParams(queryParamValue: string): URLSearchParams { | ||
const searchParams: URLSearchParams = new URLSearchParams(); | ||
searchParams.set(queryParamKey, queryParamValue); | ||
return searchParams; | ||
} | ||
|
||
function setupUseSettingsModalContextMock(openSettingsMock: typeof jest.fn): jest.Mock { | ||
return (useSettingsModalContext as jest.Mock).mockReturnValue({ | ||
settingsRef: { | ||
current: { | ||
openSettings: openSettingsMock, | ||
}, | ||
}, | ||
}); | ||
} |
24 changes: 24 additions & 0 deletions
24
frontend/app-development/hooks/useOpenSettingsModalBasedQueryParam.ts
This file contains 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,24 @@ | ||
import { useEffect } from 'react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { useSettingsModalContext } from '../contexts/SettingsModalContext'; | ||
import type { SettingsModalTabId } from '../types/SettingsModalTabId'; | ||
import { allSettingsModalTabs } from '../layout/PageHeader/SubHeader/SettingsModalButton/SettingsModal/hooks/useSettingsModalMenuTabConfigs'; | ||
|
||
export const queryParamKey: string = 'openSettingsModalWithTab'; | ||
|
||
export function useOpenSettingsModalBasedQueryParam(): void { | ||
const [searchParams] = useSearchParams(); | ||
const { settingsRef } = useSettingsModalContext(); | ||
|
||
useEffect((): void => { | ||
const tabToOpen: SettingsModalTabId = searchParams.get(queryParamKey) as SettingsModalTabId; | ||
const shouldOpenModal: boolean = isValidTab(tabToOpen); | ||
if (shouldOpenModal) { | ||
settingsRef.current.openSettings(tabToOpen); | ||
} | ||
}, [searchParams, settingsRef]); | ||
} | ||
|
||
function isValidTab(tabId: SettingsModalTabId): boolean { | ||
return allSettingsModalTabs.includes(tabId); | ||
} |
This file contains 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 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