Skip to content
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

feat: Export OnUrlUpdateFunction type for Vitest v2 #712

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions packages/docs/content/docs/testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ by wrapping your rendered component in a `NuqsTestingAdapter{:ts}`.
Here is an example for Vitest and Testing Library to test a button rendering
a counter:

```tsx title="counter-button.test.tsx"
<Tabs items={['Vitest v1', 'Vitest v2']}>

```tsx title="counter-button.test.tsx" tab="Vitest v1"
// [!code word:NuqsTestingAdapter]
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
Expand All @@ -34,10 +36,45 @@ it('should increment the count when clicked', async () => {
// 3. Assert changes in the state and in the (mocked) URL
expect(button).toHaveTextContent('count is 43')
expect(onUrlUpdate).toHaveBeenCalledOnce()
expect(onUrlUpdate.mock.calls[0][0].queryString).toBe('?count=43')
expect(onUrlUpdate.mock.calls[0][0].searchParams.get('count')).toBe('43')
expect(onUrlUpdate.mock.calls[0][0].options.history).toBe('push')
const event = onUrlUpdate.mock.calls[0][0]!
expect(event.queryString).toBe('?count=43')
expect(event.searchParams.get('count')).toBe('43')
expect(event.options.history).toBe('push')
})
```

```tsx title="counter-button.test.tsx" tab="Vitest v2"
// [!code word:NuqsTestingAdapter]
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { NuqsTestingAdapter, type OnUrlUpdateFunction } from 'nuqs/adapters/testing'
import { describe, expect, it, vi } from 'vitest'
import { CounterButton } from './counter-button'

it('should increment the count when clicked', async () => {
const user = userEvent.setup()
const onUrlUpdate = vi.fn<OnUrlUpdateFunction>()
render(<CounterButton />, {
// 1. Setup the test by passing initial search params / querystring:
wrapper: ({ children }) => (
<NuqsTestingAdapter searchParams="?count=42" onUrlUpdate={onUrlUpdate}>
{children}
</NuqsTestingAdapter>
)
})
// 2. Act
const button = screen.getByRole('button')
await user.click(button)
// 3. Assert changes in the state and in the (mocked) URL
expect(button).toHaveTextContent('count is 43')
expect(onUrlUpdate).toHaveBeenCalledOnce()
const event = onUrlUpdate.mock.calls[0][0]!
expect(event.queryString).toBe('?count=43')
expect(event.searchParams.get('count')).toBe('43')
expect(event.options.history).toBe('push')
})
```

</Tabs>

See issue [#259](https://github.com/47ng/nuqs/issues/259) for more testing-related discussions.
3 changes: 3 additions & 0 deletions packages/docs/mdx-components.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Callout } from 'fumadocs-ui/components/callout'
import { CodeBlock, Pre } from 'fumadocs-ui/components/codeblock'
import { Tab, Tabs } from 'fumadocs-ui/components/tabs'
import defaultMdxComponents from 'fumadocs-ui/mdx'
import type { MDXComponents } from 'mdx/types'
import { Suspense } from 'react'
Expand All @@ -10,6 +11,8 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
...components,
Callout,
Suspense,
Tab,
Tabs,
pre: ({ ref: _ref, ...props }) => (
<CodeBlock {...props}>
<Pre>{props.children}</Pre>
Expand Down
4 changes: 3 additions & 1 deletion packages/nuqs/src/adapters/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ export type UrlUpdateEvent = {
options: Required<AdapterOptions>
}

export type OnUrlUpdateFunction = (event: UrlUpdateEvent) => void

type TestingAdapterProps = {
searchParams?: string | Record<string, string> | URLSearchParams
onUrlUpdate?: (event: UrlUpdateEvent) => void
onUrlUpdate?: OnUrlUpdateFunction
rateLimitFactor?: number
children: ReactNode
}
Expand Down
Loading