@@ -230,6 +230,113 @@ export default function (test, is_dev) {
230
230
assert . equal ( page . url ( ) , 'https://www.google.com/' ) ;
231
231
} ) ;
232
232
233
+
234
+ test ( 'history index gets set on first render' , '/routing/history/a' , async ( { js, page } ) => {
235
+ if ( js ) {
236
+ const state = await page . evaluate ( 'history.state' ) ;
237
+ assert . equal ( state ?. [ 'sveltekit:index' ] , 0 ) ;
238
+ }
239
+ } ) ;
240
+
241
+ test ( 'history index increases after navigating by clicking a link' , '/routing/history/a' , async ( { js, page, clicknav } ) => {
242
+ if ( js ) {
243
+ await clicknav ( '[href="/routing/history/b"]' ) ;
244
+ const state = await page . evaluate ( 'history.state' ) ;
245
+ assert . equal ( state ?. [ 'sveltekit:index' ] , 1 ) ;
246
+ }
247
+ } ) ;
248
+
249
+ test ( 'history index increases after navigating by using goto' , '/routing/history/a' , async ( { js, app, base, page } ) => {
250
+ if ( js ) {
251
+ await app . goto ( base + '/routing/history/b' ) ;
252
+ const state = await page . evaluate ( 'history.state' ) ;
253
+ assert . equal ( state ?. [ 'sveltekit:index' ] , 1 ) ;
254
+ }
255
+ } ) ;
256
+
257
+ test ( 'history index stays after navigating by using goto with replaceState' , '/routing/history/a' , async ( { js, app, base, page } ) => {
258
+ if ( js ) {
259
+ await app . goto ( base + '/routing/history/b' , { replaceState : true } ) ;
260
+ const state = await page . evaluate ( 'history.state' ) ;
261
+ assert . equal ( state ?. [ 'sveltekit:index' ] , 0 ) ;
262
+ }
263
+ } ) ;
264
+
265
+ test ( 'history index stays after fixing tralingSlash' , '/routing/history/a' , async ( { js, app, base, page } ) => {
266
+ if ( js ) {
267
+ await app . goto ( base + '/routing/history/b/' ) ;
268
+ const state = await page . evaluate ( 'history.state' ) ;
269
+ assert . equal ( state ?. [ 'sveltekit:index' ] , 1 ) ;
270
+ }
271
+ } )
272
+
273
+ test ( 'history index decreases after navigating back' , '/routing/history/a' , async ( { js, clicknav, app, base, page } ) => {
274
+ if ( js ) {
275
+ await clicknav ( '[href="/routing/history/b"]' ) ;
276
+ await app . goto ( base + '/routing/history/c' ) ;
277
+ await page . goBack ( ) ;
278
+ const state1 = await page . evaluate ( 'history.state' ) ;
279
+ assert . equal ( state1 ?. [ 'sveltekit:index' ] , 1 ) ;
280
+ await clicknav ( 'button' ) ;
281
+ const state2 = await page . evaluate ( 'history.state' ) ;
282
+ assert . equal ( state2 ?. [ 'sveltekit:index' ] , 0 ) ;
283
+ await clicknav ( '[href="/routing/history/b"]' ) ;
284
+ const state3 = await page . evaluate ( 'history.state' ) ;
285
+ assert . equal ( state3 ?. [ 'sveltekit:index' ] , 1 ) ;
286
+ }
287
+ } ) ;
288
+
289
+ test ( 'history index survives a reload' , '/routing/history/a' , async ( { js, clicknav, app, base, page } ) => {
290
+ if ( js ) {
291
+ await clicknav ( '[href="/routing/history/b"]' ) ;
292
+ await page . reload ( { waitUntil : 'networkidle' } ) ;
293
+ await app . goto ( base + '/routing/history/c' ) ;
294
+ const state = await page . evaluate ( 'history.state' ) ;
295
+ assert . equal ( state ?. [ 'sveltekit:index' ] , 2 ) ;
296
+ }
297
+ } ) ;
298
+
299
+ test ( 'onBeforeNavigate can prevent navigation by clicking a link' , '/routing/history/a' , async ( { js, clicknav, page, app, base } ) => {
300
+ if ( js ) {
301
+ await app . goto ( base + '/routing/history/prevent-navigation' ) ;
302
+
303
+ try {
304
+ await clicknav ( '[href="/routing/history/b"]' ) ;
305
+ assert . unreachable ( 'should have thrown' ) ;
306
+ } catch ( err ) {
307
+ assert . instance ( err , Error ) ;
308
+ assert . match ( err . message , 'Timed out' ) ;
309
+ }
310
+
311
+ const state = await page . evaluate ( 'history.state' ) ;
312
+ assert . equal ( state ?. [ 'sveltekit:index' ] , 1 ) ;
313
+ assert . equal ( page . url ( ) , base + '/routing/history/prevent-navigation' ) ;
314
+ assert . equal ( await page . innerHTML ( 'pre' ) , 'true' , 'onBeforeNavigate not triggered' ) ;
315
+ }
316
+ } ) ;
317
+
318
+ test ( 'onBeforeNavigate can prevent navigation by using goto' , '/routing/history/a' , async ( { js, page, app, base } ) => {
319
+ if ( js ) {
320
+ await app . goto ( base + '/routing/history/prevent-navigation-promise' ) ;
321
+ await app . goto ( base + '/routing/history/b' ) ;
322
+ const state = await page . evaluate ( 'history.state' ) ;
323
+ assert . equal ( state ?. [ 'sveltekit:index' ] , 1 ) ;
324
+ assert . equal ( page . url ( ) , base + '/routing/history/prevent-navigation-promise' ) ;
325
+ assert . equal ( await page . innerHTML ( 'pre' ) , 'true' , 'onBeforeNavigate not triggered' ) ;
326
+ }
327
+ } ) ;
328
+
329
+ test ( 'onBeforeNavigate can prevent navigation using the browser controls' , '/routing/history/a' , async ( { js, page, app, base } ) => {
330
+ if ( js ) {
331
+ await app . goto ( base + '/routing/history/prevent-navigation' ) ;
332
+ await page . goBack ( ) ;
333
+ const state = await page . evaluate ( 'history.state' ) ;
334
+ assert . equal ( state ?. [ 'sveltekit:index' ] , 1 ) ;
335
+ assert . equal ( page . url ( ) , base + '/routing/history/prevent-navigation' ) ;
336
+ assert . equal ( await page . innerHTML ( 'pre' ) , 'true' , 'onBeforeNavigate not triggered' ) ;
337
+ }
338
+ } ) ;
339
+
233
340
// skipping this test because it causes a bunch of failures locally
234
341
test . skip ( 'watch new route in dev' , '/routing' , async ( { page, base, js, watcher } ) => {
235
342
if ( ! is_dev || js ) {
0 commit comments