11import { screen , waitFor } from '@testing-library/react' ;
22import userEvent from '@testing-library/user-event' ;
3- import { describe , expect , it , vi } from 'vitest' ;
3+ import { afterEach , describe , expect , it , vi } from 'vitest' ;
44
55import { serveFapi } from '../../__tests__/feature/fake-fapi' ;
66import { renderWithClerk } from '../../__tests__/feature/render' ;
7+ import type { MosaicRouter } from '../use-mosaic-router' ;
78import { useMosaicRouter } from '../use-mosaic-router' ;
89
910function Navigation ( { to, hard = false } : { to : string ; hard ?: boolean } ) {
@@ -18,6 +19,21 @@ function Navigation({ to, hard = false }: { to: string; hard?: boolean }) {
1819 ) ;
1920}
2021
22+ function CaptureRouter ( { onRouter } : { onRouter : ( router : MosaicRouter ) => void } ) {
23+ onRouter ( useMosaicRouter ( ) ) ;
24+ return null ;
25+ }
26+
27+ async function renderRouter ( ) {
28+ serveFapi ( ) ;
29+ let captured : MosaicRouter | undefined ;
30+ await renderWithClerk ( < CaptureRouter onRouter = { router => ( captured = router ) } /> ) ;
31+ if ( ! captured ) {
32+ throw new Error ( 'Router was not rendered' ) ;
33+ }
34+ return captured ;
35+ }
36+
2137describe ( 'Mosaic router' , ( ) => {
2238 it ( 'uses the host router for ordinary same-origin navigation' , async ( ) => {
2339 serveFapi ( ) ;
@@ -87,4 +103,41 @@ describe('Mosaic router', () => {
87103 }
88104 }
89105 } ) ;
106+
107+ describe ( 'getSearchParams' , ( ) => {
108+ const originalUrl = window . location . href ;
109+
110+ afterEach ( ( ) => {
111+ window . history . replaceState ( null , '' , originalUrl ) ;
112+ } ) ;
113+
114+ it ( 'reads the current query with values decoded' , async ( ) => {
115+ const router = await renderRouter ( ) ;
116+ window . history . replaceState ( null , '' , '?email_address=bob%40example.com' ) ;
117+ expect ( router . getSearchParams ( ) . get ( 'email_address' ) ) . toBe ( 'bob@example.com' ) ;
118+ } ) ;
119+
120+ it ( 'returns null for a key that is not in the query' , async ( ) => {
121+ const router = await renderRouter ( ) ;
122+ window . history . replaceState ( null , '' , '?email_address=bob%40example.com' ) ;
123+ expect ( router . getSearchParams ( ) . get ( 'missing' ) ) . toBeNull ( ) ;
124+ } ) ;
125+
126+ it ( 'reads the latest query after the URL changes' , async ( ) => {
127+ const router = await renderRouter ( ) ;
128+ window . history . replaceState ( null , '' , '?step=one' ) ;
129+ expect ( router . getSearchParams ( ) . get ( 'step' ) ) . toBe ( 'one' ) ;
130+ window . history . replaceState ( null , '' , '?step=two' ) ;
131+ expect ( router . getSearchParams ( ) . get ( 'step' ) ) . toBe ( 'two' ) ;
132+ } ) ;
133+
134+ it ( 'returns a fresh instance on each call' , async ( ) => {
135+ const router = await renderRouter ( ) ;
136+ window . history . replaceState ( null , '' , '?step=one' ) ;
137+ const first = router . getSearchParams ( ) ;
138+ first . set ( 'step' , 'two' ) ;
139+ first . append ( 'extra' , 'value' ) ;
140+ expect ( router . getSearchParams ( ) . toString ( ) ) . toBe ( 'step=one' ) ;
141+ } ) ;
142+ } ) ;
90143} ) ;
0 commit comments