Skip to content

Commit

Permalink
tests(router): reworking sticky params to support mocked testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanl17 committed Aug 30, 2024
1 parent 432cf10 commit e89d3d0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
22 changes: 13 additions & 9 deletions packages/sanity/src/router/IntentLink.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {describe, expect, it} from '@jest/globals'
import {describe, expect, it, jest} from '@jest/globals'
import {render} from '@testing-library/react'
import {noop} from 'lodash'

import {IntentLink} from './IntentLink'
import {route} from './route'
import {RouterProvider} from './RouterProvider'

jest.mock('./stickyParams', () => ({
STICKY_PARAMS: ['aTestStickyParam'],
}))

describe('IntentLink', () => {
it('should resolve intent link with query params', () => {
const router = route.create('/test', [route.intents('/intent')])
Expand All @@ -16,7 +20,7 @@ describe('IntentLink', () => {
id: 'document-id-123',
type: 'document-type',
}}
searchParams={[['perspective', `bundle.summer-drop`]]}
searchParams={[['aTestStickyParam', `aStickyParam.value`]]}
/>,
{
wrapper: ({children}) => (
Expand All @@ -28,7 +32,7 @@ describe('IntentLink', () => {
)
// Component should render the query param in the href
expect(component.container.querySelector('a')?.href).toContain(
'/test/intent/edit/id=document-id-123;type=document-type/?perspective=bundle.summer-drop',
'/test/intent/edit/id=document-id-123;type=document-type/?aTestStickyParam=aStickyParam.value',
)
})

Expand All @@ -48,7 +52,7 @@ describe('IntentLink', () => {
onNavigate={noop}
router={router}
state={{
_searchParams: [['perspective', 'bundle.summer-drop']],
_searchParams: [['aTestStickyParam', 'aStickyParam.value']],
}}
>
{children}
Expand All @@ -58,7 +62,7 @@ describe('IntentLink', () => {
)
// Component should render the query param in the href
expect(component.container.querySelector('a')?.href).toContain(
'/test/intent/edit/id=document-id-123;type=document-type/?perspective=bundle.summer-drop',
'/test/intent/edit/id=document-id-123;type=document-type/?aTestStickyParam=aStickyParam.value',
)
})

Expand All @@ -71,15 +75,15 @@ describe('IntentLink', () => {
id: 'document-id-123',
type: 'document-type',
}}
searchParams={[['perspective', `bundle.autumn-drop`]]}
searchParams={[['aTestStickyParam', `aStickyParam.value.to-be-defined`]]}
/>,
{
wrapper: ({children}) => (
<RouterProvider
onNavigate={noop}
router={router}
state={{
_searchParams: [['perspective', 'bundle.summer-drop']],
_searchParams: [['aTestStickyParam', 'aStickyParam.value.to-be-overridden']],
}}
>
{children}
Expand All @@ -89,10 +93,10 @@ describe('IntentLink', () => {
)
// Component should render the query param in the href
expect(component.container.querySelector('a')?.href).toContain(
'/test/intent/edit/id=document-id-123;type=document-type/?perspective=bundle.autumn-drop',
'/test/intent/edit/id=document-id-123;type=document-type/?aTestStickyParam=aStickyParam.value.to-be-defined',
)
expect(component.container.querySelector('a')?.href).not.toContain(
'perspective=bundle.summer-drop',
'aTestStickyParam=aStickyParam.value.to-be-overridden',
)
})
})
10 changes: 5 additions & 5 deletions packages/sanity/src/router/RouterProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {fromPairs, partition, toPairs} from 'lodash'
import {type ReactElement, type ReactNode, useCallback, useMemo} from 'react'
import {RouterContext} from 'sanity/_singletons'

import {STICKY_PARAMS} from './stickyParams'
import {
type IntentParameters,
type NavigateOptions,
Expand Down Expand Up @@ -89,7 +90,7 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {
params,
payload,
_searchParams: toPairs({
...fromPairs((state._searchParams ?? []).filter(([key]) => STICKY.includes(key))),
...fromPairs((state._searchParams ?? []).filter(([key]) => STICKY_PARAMS.includes(key))),
...fromPairs(_searchParams ?? []),
}),
})
Expand All @@ -101,7 +102,7 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {
(nextState: RouterState): string => {
const currentStateParams = state._searchParams || []
const nextStateParams = nextState._searchParams || []
const nextParams = STICKY.reduce((acc, param) => {
const nextParams = STICKY_PARAMS.reduce((acc, param) => {
return replaceStickyParam(
acc,
param,
Expand All @@ -119,7 +120,7 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {

const handleNavigateStickyParam = useCallback(
(param: string, value: string | undefined, options: NavigateOptions = {}) => {
if (!STICKY.includes(param)) {
if (!STICKY_PARAMS.includes(param)) {
throw new Error('Parameter is not sticky')
}
onNavigate({
Expand Down Expand Up @@ -152,7 +153,7 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {
return [state, null]
}
const {_searchParams, ...rest} = state
const [sticky, restParams] = partition(_searchParams, ([key]) => STICKY.includes(key))
const [sticky, restParams] = partition(_searchParams, ([key]) => STICKY_PARAMS.includes(key))
if (sticky.length === 0) {
return [state, null]
}
Expand Down Expand Up @@ -184,7 +185,6 @@ export function RouterProvider(props: RouterProviderProps): ReactElement {

return <RouterContext.Provider value={router}>{props.children}</RouterContext.Provider>
}
const STICKY: string[] = []

function replaceStickyParam(
current: SearchParam[],
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/router/stickyParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const STICKY_PARAMS: string[] = []

0 comments on commit e89d3d0

Please sign in to comment.