Skip to content

Commit

Permalink
chore(core): add tests for query param intents
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Jul 9, 2024
1 parent 646f433 commit 5daf7c3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/sanity/src/core/studio/router/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {describe, it} from '@jest/globals'
import {expect} from '@playwright/experimental-ct-react'
import {type Tool} from 'sanity'
import {type RouterState} from 'sanity/router'

import {resolveIntentState} from './helpers'

describe('resolveIntentState', () => {
const testTool: Tool = {
name: 'test',
title: 'Test tool',
component: () => null,
canHandleIntent: () => true,
getIntentState: () => ({}),
}

it('should resolve intent state with query params', () => {
const state: RouterState = {
intent: 'edit',
params: {
id: 'p-bay-area-san-francisco-2022-08-17-2022-08-17',
type: 'playlist',
},
_searchParams: [['perspective', 'bundle.pedro-summer']],
}

const resolved = resolveIntentState([testTool], null, state)
expect(resolved).toEqual({
type: 'state',
isNotFound: false,
state: {
// searchParams are persisted in the router state
_searchParams: [['perspective', 'bundle.pedro-summer']],
tool: 'test',
test: {},
},
})
})
})
33 changes: 33 additions & 0 deletions packages/sanity/src/router/IntentLink.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {describe, expect, it} from '@jest/globals'
import {render} from '@testing-library/react'

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

describe('IntentLink', () => {
it('should resolve intent link with query params', () => {
const router = route.create('/test', [route.intents('/intent')])
const component = render(
<IntentLink
intent="edit"
params={{
id: 'document-id-123',
type: 'document-type',
}}
searchParams={[['perspective', `bundle.summer-drop`]]}
/>,
{
wrapper: ({children}) => (
<RouterProvider onNavigate={() => null} router={router} state={{}}>
{children}
</RouterProvider>
),
},
)
// 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',
)
})
})

0 comments on commit 5daf7c3

Please sign in to comment.