Skip to content

Commit

Permalink
test: Use HOC in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Nov 14, 2024
1 parent 8a4b166 commit f8510f5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 52 deletions.
20 changes: 9 additions & 11 deletions packages/e2e/react-router/src/components/counter-button.test.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { NuqsTestingAdapter, type UrlUpdateEvent } from 'nuqs/adapters/testing'
import {
withNuqsTestingAdapter,
type UrlUpdateEvent
} from 'nuqs/adapters/testing'
import { describe, expect, it, vi } from 'vitest'
import { CounterButton } from './counter-button'

describe('CounterButton', () => {
it('should render the button with state loaded from the URL', () => {
render(<CounterButton />, {
wrapper: ({ children }) => (
<NuqsTestingAdapter searchParams="?count=42">
{children}
</NuqsTestingAdapter>
)
wrapper: withNuqsTestingAdapter({ searchParams: '?count=42' })
})
expect(screen.getByRole('button')).toHaveTextContent('count is 42')
})
it('should increment the count when clicked', async () => {
const user = userEvent.setup()
const onUrlUpdate = vi.fn<[UrlUpdateEvent]>()
render(<CounterButton />, {
wrapper: ({ children }) => (
<NuqsTestingAdapter searchParams="?count=42" onUrlUpdate={onUrlUpdate}>
{children}
</NuqsTestingAdapter>
)
wrapper: withNuqsTestingAdapter({
searchParams: '?count=42',
onUrlUpdate
})
})
const button = screen.getByRole('button')
await user.click(button)
Expand Down
23 changes: 8 additions & 15 deletions packages/e2e/react-router/src/components/search-input.test.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { NuqsTestingAdapter, type UrlUpdateEvent } from 'nuqs/adapters/testing'
import {
withNuqsTestingAdapter,
type UrlUpdateEvent
} from 'nuqs/adapters/testing'
import { describe, expect, it, vi } from 'vitest'
import { SearchInput } from './search-input'

describe('SearchInput', () => {
it('should render the input with state loaded from the URL', () => {
render(<SearchInput />, {
wrapper: ({ children }) => (
<NuqsTestingAdapter
searchParams={{
search: 'nuqs'
}}
>
{children}
</NuqsTestingAdapter>
)
wrapper: withNuqsTestingAdapter({ searchParams: { search: 'nuqs' } })
})
const input = screen.getByRole('search')
expect(input).toHaveValue('nuqs')
Expand All @@ -24,11 +19,9 @@ describe('SearchInput', () => {
const user = userEvent.setup()
const onUrlUpdate = vi.fn<[UrlUpdateEvent]>()
render(<SearchInput />, {
wrapper: ({ children }) => (
<NuqsTestingAdapter onUrlUpdate={onUrlUpdate} rateLimitFactor={0}>
{children}
</NuqsTestingAdapter>
)
wrapper: withNuqsTestingAdapter({
onUrlUpdate
})
})
const expectedState = 'Hello, world!'
const expectedParam = 'Hello,+world!'
Expand Down
20 changes: 9 additions & 11 deletions packages/e2e/react/src/components/counter-button.test.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { NuqsTestingAdapter, type UrlUpdateEvent } from 'nuqs/adapters/testing'
import {
withNuqsTestingAdapter,
type UrlUpdateEvent
} from 'nuqs/adapters/testing'
import { describe, expect, it, vi } from 'vitest'
import { CounterButton } from './counter-button'

describe('CounterButton', () => {
it('should render the button with state loaded from the URL', () => {
render(<CounterButton />, {
wrapper: ({ children }) => (
<NuqsTestingAdapter searchParams="?count=42">
{children}
</NuqsTestingAdapter>
)
wrapper: withNuqsTestingAdapter({ searchParams: '?count=42' })
})
expect(screen.getByRole('button')).toHaveTextContent('count is 42')
})
it('should increment the count when clicked', async () => {
const user = userEvent.setup()
const onUrlUpdate = vi.fn<[UrlUpdateEvent]>()
render(<CounterButton />, {
wrapper: ({ children }) => (
<NuqsTestingAdapter searchParams="?count=42" onUrlUpdate={onUrlUpdate}>
{children}
</NuqsTestingAdapter>
)
wrapper: withNuqsTestingAdapter({
searchParams: '?count=42',
onUrlUpdate
})
})
const button = screen.getByRole('button')
await user.click(button)
Expand Down
23 changes: 8 additions & 15 deletions packages/e2e/react/src/components/search-input.test.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { NuqsTestingAdapter, type UrlUpdateEvent } from 'nuqs/adapters/testing'
import {
withNuqsTestingAdapter,
type UrlUpdateEvent
} from 'nuqs/adapters/testing'
import { describe, expect, it, vi } from 'vitest'
import { SearchInput } from './search-input'

describe('SearchInput', () => {
it('should render the input with state loaded from the URL', () => {
render(<SearchInput />, {
wrapper: ({ children }) => (
<NuqsTestingAdapter
searchParams={{
search: 'nuqs'
}}
>
{children}
</NuqsTestingAdapter>
)
wrapper: withNuqsTestingAdapter({ searchParams: { search: 'nuqs' } })
})
const input = screen.getByRole('search')
expect(input).toHaveValue('nuqs')
Expand All @@ -24,11 +19,9 @@ describe('SearchInput', () => {
const user = userEvent.setup()
const onUrlUpdate = vi.fn<[UrlUpdateEvent]>()
render(<SearchInput />, {
wrapper: ({ children }) => (
<NuqsTestingAdapter onUrlUpdate={onUrlUpdate} rateLimitFactor={0}>
{children}
</NuqsTestingAdapter>
)
wrapper: withNuqsTestingAdapter({
onUrlUpdate
})
})
const expectedState = 'Hello, world!'
const expectedParam = 'Hello,+world!'
Expand Down

0 comments on commit f8510f5

Please sign in to comment.