33 * see https://github.com/denoland/deno/blob/main/docs/runtime/location_api.md
44 * and explanation below.
55 */
6+
67import { assertEquals } from 'std/testing/asserts.ts'
8+ import events from './events.ts'
79import { redirect } from './redirect.ts'
810
911// mock history functions used in redirect()
10- interface MockWindow extends Window {
11- history : {
12- replaceState : ( state : object | null , title : string | '' , url ?: string ) => void ,
13- pushState : ( state : object | null , title : string | '' , url ?: string ) => void
14- }
15- }
16- declare let window : MockWindow
12+ Object . assign ( window , {
13+ history : {
14+ replaceState : ( url : string ) => { stacks . replaceCalls ++ } ,
15+ pushState : ( url : string ) => { stacks . pushCalls ++ }
16+ }
17+ } )
1718
1819// track calls to history functions
19- const calls = {
20- pushCalls : 0 , // tracks calls to pushState()
21- replaceCalls : 0 // tracks calls to replaceState()
22- }
23- // create mock history impl
24- window . history = {
25- replaceState : ( url ) => { calls . replaceCalls ++ ; return null } ,
26- pushState : ( url ) => { calls . pushCalls ++ ; return null }
20+ const stacks = {
21+ pushCalls : 0 , // tracks calls to pushState()
22+ replaceCalls : 0 // tracks calls to replaceState()
2723}
2824
29- const resetCallCount = ( ) => {
30- calls . pushCalls = 0
31- calls . replaceCalls = 0
25+ const resetStacks = ( ) => {
26+ stacks . pushCalls = 0
27+ stacks . replaceCalls = 0
3228}
3329
34- Deno . test ( 'redirect: replace=false should call history.pushState' , ( ) => {
35- const url = '/foo/bar.ts '
30+ Deno . test ( 'fw/core/ redirect: replace=false should call history.pushState' , ( ) => {
31+ const url = '/foo/bar'
3632
37- redirect ( url )
38- assertEquals ( calls . pushCalls , 1 )
39- redirect ( url )
40- assertEquals ( calls . pushCalls , 2 )
41- redirect ( url )
42- assertEquals ( calls . pushCalls , 3 )
33+ redirect ( url )
34+ assertEquals ( stacks . pushCalls , 1 )
35+ redirect ( url )
36+ assertEquals ( stacks . pushCalls , 2 )
37+ redirect ( url )
38+ assertEquals ( stacks . pushCalls , 3 )
4339
44- resetCallCount ( )
40+ resetStacks ( )
4541} )
4642
47- Deno . test ( 'redirect: replace=true should call history.replaceState' , ( ) => {
48- const url = '/foo/bar2.ts '
43+ Deno . test ( 'fw/core/ redirect: replace=true should call history.replaceState' , ( ) => {
44+ const url = '/foo/bar '
4945
50- redirect ( url , true )
51- assertEquals ( calls . replaceCalls , 1 )
52- redirect ( url , true )
46+ redirect ( url , true )
47+ assertEquals ( stacks . replaceCalls , 1 )
48+ redirect ( url , true )
49+ assertEquals ( stacks . replaceCalls , 2 )
5350
54- assertEquals ( calls . replaceCalls , 2 )
5551
56- resetCallCount ( )
52+ resetStacks ( )
5753} )
5854
59- Deno . test ( 'redirect: empty string url should not call history methods' , ( ) => {
60- const url = ''
55+ Deno . test ( 'fw/core/redirect: empty string url should not call history methods' , ( ) => {
56+ redirect ( '' )
57+
58+ assertEquals ( stacks . pushCalls , 0 )
59+ assertEquals ( stacks . replaceCalls , 0 )
60+ } )
61+
62+ Deno . test ( 'fw/core/redirect: pre-redirect should emit "popstate" event deferredly' , ( ) => {
63+ let popstate : any = null
64+
65+ redirect ( '/foo/bar' , true )
6166
62- redirect ( url )
67+ events . on ( 'popstate' , ( e ) => { popstate = e } )
68+ assertEquals ( popstate , null )
6369
64- assertEquals ( calls . pushCalls , 0 )
65- assertEquals ( calls . replaceCalls , 0 )
70+ events . emit ( 'routerstate' , { ready : true } )
71+ assertEquals ( popstate , { type : 'popstate' , resetScroll : true } )
72+ assertEquals ( stacks . pushCalls , 0 )
73+ assertEquals ( stacks . replaceCalls , 1 )
6674
75+ resetStacks ( )
6776} )
6877
6978/**
@@ -72,7 +81,7 @@ Deno.test('redirect: empty string url should not call history methods', () => {
7281 * https://github.com/denoland/deno/blob/main/docs/runtime/location_api.md
7382 * This errors out on line 12 of redirect().
7483 */
75- // Deno.test('redirect: file url should set location.href', () => {
84+ // Deno.test('fw/core/ redirect: file url should set location.href', () => {
7685// const url = 'file://foo/file.ts'
7786
7887// redirect(url)
@@ -81,5 +90,5 @@ Deno.test('redirect: empty string url should not call history methods', () => {
8190// assertEquals(calls.pushCalls, 0)
8291// assertEquals(calls.replaceCalls, 0)
8392
84- // resetCallCount ()
93+ // resetStacks ()
8594// })
0 commit comments