@@ -203,4 +203,130 @@ describe('Wizard', () => {
203
203
mounted . unmount ( ) ;
204
204
} ) ;
205
205
} ) ;
206
+
207
+ describe ( 'with existing history and preserving search params' , ( ) => {
208
+ let wizard ;
209
+ let mounted ;
210
+
211
+ const mockReplace = jest . fn ( ) ;
212
+ const mockPush = jest . fn ( ) ;
213
+
214
+ beforeEach ( ( ) => {
215
+ jest . clearAllMocks ( ) ;
216
+ } ) ;
217
+
218
+ describe ( 'initially at /gryffindor with ?foo=bar' , ( ) => {
219
+ const history = {
220
+ replace : mockReplace ,
221
+ listen : ( ) => ( ) => null ,
222
+ location : {
223
+ pathname : '/gryffindor' ,
224
+ search : '?foo=bar' ,
225
+ } ,
226
+ } ;
227
+
228
+ beforeEach ( ( ) => {
229
+ mounted = mount (
230
+ < Wizard history = { history } preserveQuery = { true } >
231
+ < WithWizard >
232
+ { prop => {
233
+ wizard = prop ;
234
+ return null ;
235
+ } }
236
+ </ WithWizard >
237
+ < Steps >
238
+ < Step id = "gryffindor" >
239
+ < div />
240
+ </ Step >
241
+ < Step id = "slytherin" >
242
+ < div />
243
+ </ Step >
244
+ < Step id = "hufflepuff" >
245
+ < div />
246
+ </ Step >
247
+ </ Steps >
248
+ </ Wizard >
249
+ ) ;
250
+ } ) ;
251
+
252
+ it ( 'should preserve query when calling next' , ( ) => {
253
+ wizard . history . push = mockPush ;
254
+ wizard . next ( ) ;
255
+ expect ( mockPush ) . toBeCalledWith ( { pathname : '/slytherin' , search : '?foo=bar' } ) ;
256
+ } ) ;
257
+
258
+ it ( 'should preserve query when calling replace' , ( ) => {
259
+ wizard . replace ( 'hufflepuff' ) ;
260
+ expect ( mockReplace ) . toBeCalledWith ( { pathname : '/hufflepuff' , search : '?foo=bar' } ) ;
261
+ } ) ;
262
+
263
+ it ( 'should produce the correct URL string when preserving search params' , ( ) => {
264
+ wizard . replace ( 'hufflepuff' ) ;
265
+ const callArgs = mockReplace . mock . calls [ 0 ] [ 0 ] ;
266
+ const actualURL = `${ callArgs . pathname } ${ callArgs . search } ` ;
267
+ expect ( actualURL ) . toBe ( '/hufflepuff?foo=bar' ) ;
268
+ } ) ;
269
+
270
+ it ( 'should not add search params if none existed initially when calling push' , ( ) => {
271
+ history . location . search = '' ;
272
+ wizard . push ( 'hufflepuff' ) ;
273
+ expect ( mockPush ) . toBeCalledWith ( { pathname : '/hufflepuff' , search : '' } ) ;
274
+ } ) ;
275
+ } ) ;
276
+
277
+ describe ( 'initially at /slytherin with ?quidditch=true' , ( ) => {
278
+ const history = {
279
+ replace : mockReplace ,
280
+ listen : ( ) => ( ) => null ,
281
+ location : {
282
+ pathname : '/slytherin' ,
283
+ search : '?quidditch=true' ,
284
+ } ,
285
+ } ;
286
+
287
+ beforeEach ( ( ) => {
288
+ mounted = mount (
289
+ < Wizard history = { history } preserveQuery = { true } >
290
+ < WithWizard >
291
+ { prop => {
292
+ wizard = prop ;
293
+ return null ;
294
+ } }
295
+ </ WithWizard >
296
+ < Steps >
297
+ < Step id = "gryffindor" >
298
+ < div />
299
+ </ Step >
300
+ < Step id = "slytherin" >
301
+ < div />
302
+ </ Step >
303
+ < Step id = "hufflepuff" >
304
+ < div />
305
+ </ Step >
306
+ </ Steps >
307
+ </ Wizard >
308
+ ) ;
309
+ } ) ;
310
+
311
+ it ( 'should preserve query when calling next' , ( ) => {
312
+ wizard . history . push = jest . fn ( ) ;
313
+ wizard . next ( ) ;
314
+ expect ( wizard . history . push ) . toBeCalledWith ( {
315
+ pathname : '/hufflepuff' ,
316
+ search : '?quidditch=true' ,
317
+ } ) ;
318
+ } ) ;
319
+
320
+ it ( 'should produce the correct URL string when preserving search params' , ( ) => {
321
+ wizard . replace ( 'gryffindor' ) ;
322
+ const callArgs = mockReplace . mock . calls [ 0 ] [ 0 ] ;
323
+ const actualURL = `${ callArgs . pathname } ${ callArgs . search } ` ;
324
+ expect ( actualURL ) . toBe ( '/gryffindor?quidditch=true' ) ;
325
+ } ) ;
326
+ } ) ;
327
+
328
+ afterEach ( ( ) => {
329
+ mounted . unmount ( ) ;
330
+ } ) ;
331
+ } ) ;
206
332
} ) ;
0 commit comments