generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathsearch-input.test.tsx
38 lines (37 loc) · 1.3 KB
/
search-input.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
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: withNuqsTestingAdapter({ searchParams: { search: 'nuqs' } })
})
const input = screen.getByRole('search')
expect(input).toHaveValue('nuqs')
})
it('should follow the user typing text', async () => {
const user = userEvent.setup()
const onUrlUpdate = vi.fn<[UrlUpdateEvent]>()
render(<SearchInput />, {
wrapper: withNuqsTestingAdapter({
onUrlUpdate
})
})
const expectedState = 'Hello, world!'
const expectedParam = 'Hello,+world!'
const searchInput = screen.getByRole('search')
await user.type(searchInput, expectedState)
expect(searchInput).toHaveValue(expectedState)
expect(onUrlUpdate).toHaveBeenCalledTimes(expectedParam.length)
for (let i = 0; i < expectedParam.length; i++) {
expect(onUrlUpdate.mock.calls[i][0].queryString).toBe(
`?search=${expectedParam.slice(0, i + 1)}`
)
}
})
})