3
3
* see https://github.com/denoland/deno/blob/main/docs/runtime/location_api.md
4
4
* and explanation below.
5
5
*/
6
+
6
7
import { assertEquals } from 'std/testing/asserts.ts'
8
+ import events from './events.ts'
7
9
import { redirect } from './redirect.ts'
8
10
9
11
// 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
+ } )
17
18
18
19
// 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()
27
23
}
28
24
29
- const resetCallCount = ( ) => {
30
- calls . pushCalls = 0
31
- calls . replaceCalls = 0
25
+ const resetStacks = ( ) => {
26
+ stacks . pushCalls = 0
27
+ stacks . replaceCalls = 0
32
28
}
33
29
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'
36
32
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 )
43
39
44
- resetCallCount ( )
40
+ resetStacks ( )
45
41
} )
46
42
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 '
49
45
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 )
53
50
54
- assertEquals ( calls . replaceCalls , 2 )
55
51
56
- resetCallCount ( )
52
+ resetStacks ( )
57
53
} )
58
54
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 )
61
66
62
- redirect ( url )
67
+ events . on ( 'popstate' , ( e ) => { popstate = e } )
68
+ assertEquals ( popstate , null )
63
69
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 )
66
74
75
+ resetStacks ( )
67
76
} )
68
77
69
78
/**
@@ -72,7 +81,7 @@ Deno.test('redirect: empty string url should not call history methods', () => {
72
81
* https://github.com/denoland/deno/blob/main/docs/runtime/location_api.md
73
82
* This errors out on line 12 of redirect().
74
83
*/
75
- // Deno.test('redirect: file url should set location.href', () => {
84
+ // Deno.test('fw/core/ redirect: file url should set location.href', () => {
76
85
// const url = 'file://foo/file.ts'
77
86
78
87
// redirect(url)
@@ -81,5 +90,5 @@ Deno.test('redirect: empty string url should not call history methods', () => {
81
90
// assertEquals(calls.pushCalls, 0)
82
91
// assertEquals(calls.replaceCalls, 0)
83
92
84
- // resetCallCount ()
93
+ // resetStacks ()
85
94
// })
0 commit comments