|
| 1 | +import { render, screen } from '@testing-library/react' |
| 2 | +import userEvent from '@testing-library/user-event' |
| 3 | +import { NuqsTestingAdapter, type UrlUpdateEvent } from 'nuqs/adapters/testing' |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | +import { SearchInput } from './search-input' |
| 6 | + |
| 7 | +describe('SearchInput', () => { |
| 8 | + it('should render the input with state loaded from the URL', () => { |
| 9 | + render(<SearchInput />, { |
| 10 | + wrapper: ({ children }) => ( |
| 11 | + <NuqsTestingAdapter |
| 12 | + searchParams={{ |
| 13 | + search: 'nuqs' |
| 14 | + }} |
| 15 | + > |
| 16 | + {children} |
| 17 | + </NuqsTestingAdapter> |
| 18 | + ) |
| 19 | + }) |
| 20 | + const input = screen.getByRole('search') |
| 21 | + expect(input).toHaveValue('nuqs') |
| 22 | + }) |
| 23 | + it('should follow the user typing text', async () => { |
| 24 | + const user = userEvent.setup() |
| 25 | + const onUrlUpdate = vi.fn<[UrlUpdateEvent]>() |
| 26 | + render(<SearchInput />, { |
| 27 | + wrapper: ({ children }) => ( |
| 28 | + <NuqsTestingAdapter onUrlUpdate={onUrlUpdate} rateLimitFactor={0}> |
| 29 | + {children} |
| 30 | + </NuqsTestingAdapter> |
| 31 | + ) |
| 32 | + }) |
| 33 | + const expectedState = 'Hello, world!' |
| 34 | + const expectedParam = 'Hello,+world!' |
| 35 | + const searchInput = screen.getByRole('search') |
| 36 | + await user.type(searchInput, expectedState) |
| 37 | + expect(searchInput).toHaveValue(expectedState) |
| 38 | + expect(onUrlUpdate).toHaveBeenCalledTimes(expectedParam.length) |
| 39 | + for (let i = 0; i < expectedParam.length; i++) { |
| 40 | + expect(onUrlUpdate.mock.calls[i][0].queryString).toBe( |
| 41 | + `?search=${expectedParam.slice(0, i + 1)}` |
| 42 | + ) |
| 43 | + } |
| 44 | + }) |
| 45 | +}) |
0 commit comments