@@ -57,13 +57,16 @@ describe("LocationFull", () => {
5757 test ( "Should register the provided callback for the 'beforeNavigate' event." , ( ) => {
5858 // Arrange.
5959 const callback = vi . fn ( ) ;
60- location . on ( 'beforeNavigate' , callback ) ;
60+ const unSub = location . on ( 'beforeNavigate' , callback ) ;
6161
6262 // Act.
6363 globalThis . window . history . pushState ( null , '' , 'http://example.com/other' ) ;
6464
6565 // Assert.
6666 expect ( callback ) . toHaveBeenCalledOnce ( ) ;
67+
68+ // Cleanup.
69+ unSub ( ) ;
6770 } ) ;
6871 test . each ( [
6972 {
@@ -78,7 +81,7 @@ describe("LocationFull", () => {
7881 // Arrange.
7982 const callback = vi . fn ( ) ;
8083 const state = { path : { test : 'value' } , hash : { } } ;
81- location . on ( 'beforeNavigate' , callback ) ;
84+ const unSub = location . on ( 'beforeNavigate' , callback ) ;
8285
8386 // Act.
8487 // @ts -expect-error stateFn cannot enumerate history.
@@ -93,12 +96,15 @@ describe("LocationFull", () => {
9396 cancelReason : undefined ,
9497 cancel : expect . any ( Function )
9598 } ) ;
99+
100+ // Cleanup.
101+ unSub ( ) ;
96102 } ) ;
97103 test ( "Should set wasCancelled to true and cancelReason to the provided reason when the event is cancelled to subsequent callbacks." , ( ) => {
98104 // Arrange.
99105 const callback = vi . fn ( ) ;
100- location . on ( 'beforeNavigate' , ( event ) => event . cancel ( 'test' ) ) ;
101- location . on ( 'beforeNavigate' , callback ) ;
106+ const unSub1 = location . on ( 'beforeNavigate' , ( event ) => event . cancel ( 'test' ) ) ;
107+ const unSub2 = location . on ( 'beforeNavigate' , callback ) ;
102108
103109 // Act.
104110 globalThis . window . history . pushState ( null , '' , 'http://example.com/other' ) ;
@@ -112,13 +118,17 @@ describe("LocationFull", () => {
112118 cancelReason : 'test' ,
113119 cancel : expect . any ( Function )
114120 } ) ;
121+
122+ // Cleanup.
123+ unSub1 ( ) ;
124+ unSub2 ( ) ;
115125 } ) ;
116126 test ( "Should ignore cancellation reasons from callbacks if the event has already been cancelled." , ( ) => {
117127 // Arrange.
118128 const callback = vi . fn ( ) ;
119- location . on ( 'beforeNavigate' , ( event ) => event . cancel ( 'test' ) ) ;
120- location . on ( 'beforeNavigate' , ( event ) => event . cancel ( 'ignored' ) ) ;
121- location . on ( 'beforeNavigate' , callback ) ;
129+ const unSub1 = location . on ( 'beforeNavigate' , ( event ) => event . cancel ( 'test' ) ) ;
130+ const unSub2 = location . on ( 'beforeNavigate' , ( event ) => event . cancel ( 'ignored' ) ) ;
131+ const unSub3 = location . on ( 'beforeNavigate' , callback ) ;
122132
123133 // Act.
124134 globalThis . window . history . pushState ( null , '' , 'http://example.com/other' ) ;
@@ -132,6 +142,11 @@ describe("LocationFull", () => {
132142 cancelReason : 'test' ,
133143 cancel : expect . any ( Function )
134144 } ) ;
145+
146+ // Cleanup.
147+ unSub1 ( ) ;
148+ unSub2 ( ) ;
149+ unSub3 ( ) ;
135150 } ) ;
136151 test . each ( [
137152 'pushState' as const ,
@@ -157,28 +172,36 @@ describe("LocationFull", () => {
157172 test ( "Should register the provided callback for the 'navigationCancelled' event." , ( ) => {
158173 // Arrange.
159174 const callback = vi . fn ( ) ;
160- location . on ( 'beforeNavigate' , ( event ) => event . cancel ( ) ) ;
161- location . on ( 'navigationCancelled' , callback ) ;
175+ const unSub1 = location . on ( 'beforeNavigate' , ( event ) => event . cancel ( ) ) ;
176+ const unSub2 = location . on ( 'navigationCancelled' , callback ) ;
162177
163178 // Act.
164179 globalThis . window . history . pushState ( null , '' , 'http://example.com/other' ) ;
165180
166181 // Assert.
167182 expect ( callback ) . toHaveBeenCalledOnce ( ) ;
183+
184+ // Cleanup.
185+ unSub1 ( ) ;
186+ unSub2 ( ) ;
168187 } ) ;
169188 test ( "Should transfer the cause of cancellation and the state to the 'navigationCancelled' event." , ( ) => {
170189 // Arrange.
171190 const callback = vi . fn ( ) ;
172191 const reason = 'test' ;
173192 const state = { test : 'value' } ;
174- location . on ( 'beforeNavigate' , ( event ) => event . cancel ( reason ) ) ;
175- location . on ( 'navigationCancelled' , callback ) ;
193+ const unSub1 = location . on ( 'beforeNavigate' , ( event ) => event . cancel ( reason ) ) ;
194+ const unSub2 = location . on ( 'navigationCancelled' , callback ) ;
176195
177196 // Act.
178197 globalThis . window . history . pushState ( state , '' , 'http://example.com/other' ) ;
179198
180199 // Assert.
181200 expect ( callback ) . toHaveBeenCalledWith ( { url : 'http://example.com/other' , cause : 'test' , method : 'push' , state } ) ;
201+
202+ // Cleanup.
203+ unSub1 ( ) ;
204+ unSub2 ( ) ;
182205 } ) ;
183206 } ) ;
184207 describe ( 'url' , ( ) => {
0 commit comments