@@ -16,6 +16,11 @@ describe('History HTMl5', () => {
16
16
dom = createDom ( )
17
17
} )
18
18
19
+ beforeEach ( ( ) => {
20
+ // empty the state to simulate an initial navigation by default
21
+ window . history . replaceState ( null , '' , '' )
22
+ } )
23
+
19
24
afterAll ( ( ) => {
20
25
dom . window . close ( )
21
26
} )
@@ -30,6 +35,9 @@ describe('History HTMl5', () => {
30
35
it ( 'handles a basic base' , ( ) => {
31
36
expect ( createWebHistory ( ) . base ) . toBe ( '' )
32
37
expect ( createWebHistory ( '/' ) . base ) . toBe ( '' )
38
+ expect ( createWebHistory ( '/#' ) . base ) . toBe ( '/#' )
39
+ expect ( createWebHistory ( '#!' ) . base ) . toBe ( '#!' )
40
+ expect ( createWebHistory ( '#other' ) . base ) . toBe ( '#other' )
33
41
} )
34
42
35
43
it ( 'handles a base tag' , ( ) => {
@@ -68,11 +76,15 @@ describe('History HTMl5', () => {
68
76
it ( 'handles a single hash base' , ( ) => {
69
77
expect ( createWebHistory ( '#' ) . base ) . toBe ( '#' )
70
78
expect ( createWebHistory ( '#/' ) . base ) . toBe ( '#' )
79
+ expect ( createWebHistory ( '#!/' ) . base ) . toBe ( '#!' )
80
+ expect ( createWebHistory ( '#other/' ) . base ) . toBe ( '#other' )
71
81
} )
72
82
73
83
it ( 'handles a non-empty hash base' , ( ) => {
74
84
expect ( createWebHistory ( '#/bar' ) . base ) . toBe ( '#/bar' )
75
85
expect ( createWebHistory ( '#/bar/' ) . base ) . toBe ( '#/bar' )
86
+ expect ( createWebHistory ( '#!/bar/' ) . base ) . toBe ( '#!/bar' )
87
+ expect ( createWebHistory ( '#other/bar/' ) . base ) . toBe ( '#other/bar' )
76
88
} )
77
89
78
90
it ( 'prepends the host to support // urls' , ( ) => {
@@ -93,29 +105,95 @@ describe('History HTMl5', () => {
93
105
spy . mockRestore ( )
94
106
} )
95
107
96
- it ( 'calls push with hash part of the url with a base' , ( ) => {
97
- dom . reconfigure ( { url : 'file:///usr/etc/index.html' } )
98
- let history = createWebHistory ( '#' )
99
- let spy = jest . spyOn ( window . history , 'pushState' )
100
- history . push ( '/foo' )
101
- expect ( spy ) . toHaveBeenCalledWith (
102
- expect . anything ( ) ,
103
- expect . any ( String ) ,
104
- '#/foo'
105
- )
106
- spy . mockRestore ( )
107
- } )
108
+ describe ( 'specific to base containing a hash' , ( ) => {
109
+ it ( 'calls push with hash part of the url with a base' , ( ) => {
110
+ dom . reconfigure ( { url : 'file:///usr/etc/index.html' } )
111
+ let initialSpy = jest . spyOn ( window . history , 'replaceState' )
112
+ let history = createWebHistory ( '#' )
113
+ // initial navigation
114
+ expect ( initialSpy ) . toHaveBeenCalledWith (
115
+ expect . anything ( ) ,
116
+ expect . any ( String ) ,
117
+ '#/'
118
+ )
119
+ let spy = jest . spyOn ( window . history , 'pushState' )
120
+ history . push ( '/foo' )
121
+ expect ( spy ) . toHaveBeenCalledWith (
122
+ expect . anything ( ) ,
123
+ expect . any ( String ) ,
124
+ '#/foo'
125
+ )
126
+ spy . mockRestore ( )
127
+ initialSpy . mockRestore ( )
128
+ } )
108
129
109
- it ( 'works with something after the hash in the base' , ( ) => {
110
- dom . reconfigure ( { url : 'file:///usr/etc/index.html' } )
111
- let history = createWebHistory ( '#something' )
112
- let spy = jest . spyOn ( window . history , 'pushState' )
113
- history . push ( '/foo' )
114
- expect ( spy ) . toHaveBeenCalledWith (
115
- expect . anything ( ) ,
116
- expect . any ( String ) ,
117
- '#something/foo'
118
- )
119
- spy . mockRestore ( )
130
+ it ( 'works with something after the hash in the base' , ( ) => {
131
+ dom . reconfigure ( { url : 'file:///usr/etc/index.html' } )
132
+ let initialSpy = jest . spyOn ( window . history , 'replaceState' )
133
+ let history = createWebHistory ( '#something' )
134
+ // initial navigation
135
+ expect ( initialSpy ) . toHaveBeenCalledWith (
136
+ expect . anything ( ) ,
137
+ expect . any ( String ) ,
138
+ '#something/'
139
+ )
140
+ let spy = jest . spyOn ( window . history , 'pushState' )
141
+ history . push ( '/foo' )
142
+ expect ( spy ) . toHaveBeenCalledWith (
143
+ expect . anything ( ) ,
144
+ expect . any ( String ) ,
145
+ '#something/foo'
146
+ )
147
+ spy . mockRestore ( )
148
+ initialSpy . mockRestore ( )
149
+ } )
150
+
151
+ it ( 'works with #! and on a file with initial location' , ( ) => {
152
+ dom . reconfigure ( { url : 'file:///usr/etc/index.html#!/foo' } )
153
+ let spy = jest . spyOn ( window . history , 'replaceState' )
154
+ createWebHistory ( '#!' )
155
+ expect ( spy ) . toHaveBeenCalledWith (
156
+ expect . anything ( ) ,
157
+ expect . any ( String ) ,
158
+ '#!/foo'
159
+ )
160
+ spy . mockRestore ( )
161
+ } )
162
+
163
+ it ( 'works with #other' , ( ) => {
164
+ dom . reconfigure ( { url : 'file:///usr/etc/index.html' } )
165
+ let spy = jest . spyOn ( window . history , 'replaceState' )
166
+ createWebHistory ( '#other' )
167
+ expect ( spy ) . toHaveBeenCalledWith (
168
+ expect . anything ( ) ,
169
+ expect . any ( String ) ,
170
+ '#other/'
171
+ )
172
+ spy . mockRestore ( )
173
+ } )
174
+
175
+ it ( 'works with custom#other in domain' , ( ) => {
176
+ dom . reconfigure ( { url : 'https://esm.dev/custom' } )
177
+ let spy = jest . spyOn ( window . history , 'replaceState' )
178
+ createWebHistory ( 'custom#other' )
179
+ expect ( spy ) . toHaveBeenCalledWith (
180
+ expect . anything ( ) ,
181
+ expect . any ( String ) ,
182
+ '#other/'
183
+ )
184
+ spy . mockRestore ( )
185
+ } )
186
+
187
+ it ( 'works with #! and a host with initial location' , ( ) => {
188
+ dom . reconfigure ( { url : 'https://esm.dev/#!/foo' } )
189
+ let spy = jest . spyOn ( window . history , 'replaceState' )
190
+ createWebHistory ( '/#!' )
191
+ expect ( spy ) . toHaveBeenCalledWith (
192
+ expect . anything ( ) ,
193
+ expect . any ( String ) ,
194
+ '#!/foo'
195
+ )
196
+ spy . mockRestore ( )
197
+ } )
120
198
} )
121
199
} )
0 commit comments