(
router: AnyRouter,
expectedSearch: { root: { value?: string }; posts: { value?: string } },
)
| 91 | } |
| 92 | |
| 93 | async function runTest( |
| 94 | router: AnyRouter, |
| 95 | expectedSearch: { root: { value?: string }; posts: { value?: string } }, |
| 96 | ) { |
| 97 | render(<RouterProvider router={router} />) |
| 98 | |
| 99 | const postsLink = await screen.findByTestId('posts-link') |
| 100 | expect(postsLink).toHaveAttribute('href') |
| 101 | const href = postsLink.getAttribute('href') |
| 102 | const linkSearchOnRoot = getSearchParamsFromURI(href!) |
| 103 | checkLocationSearch(expectedSearch.root) |
| 104 | expect(router.state.location.search).toEqual(expectedSearch.root) |
| 105 | |
| 106 | fireEvent.click(postsLink) |
| 107 | |
| 108 | const postHeading = await screen.findByTestId('posts-heading') |
| 109 | expect(postHeading).toBeInTheDocument() |
| 110 | expect(window.location.pathname).toBe('/posts') |
| 111 | |
| 112 | expect(await screen.findByTestId('search')).toHaveTextContent( |
| 113 | expectedSearch.posts.value ?? '$undefined', |
| 114 | ) |
| 115 | checkLocationSearch(expectedSearch.posts) |
| 116 | expect(router.state.location.search).toEqual(expectedSearch.posts) |
| 117 | return linkSearchOnRoot |
| 118 | } |
| 119 | |
| 120 | function checkLocationSearch(search: object) { |
| 121 | const parsedSearch = new URLSearchParams(window.location.search) |
no test coverage detected