Skip to content

Commit 7df9b33

Browse files
committed
tests: Add unit tests for unsubscription function for location.on()
1 parent 711d94c commit 7df9b33

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/lib/core/LocationFull.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,36 @@ describe("LocationFull", () => {
6868
// Cleanup.
6969
unSub();
7070
});
71+
test("Should unregister the provided callback when the returned function is called.", () => {
72+
// Arrange.
73+
const callback = vi.fn();
74+
const unSub = location.on('beforeNavigate', callback);
75+
76+
// Act.
77+
unSub();
78+
79+
// Assert.
80+
globalThis.window.history.pushState(null, '', 'http://example.com/other');
81+
expect(callback).not.toHaveBeenCalled();
82+
});
83+
test("Should not affect other handlers when unregistering one of the event handlers.", () => {
84+
// Arrange.
85+
const callback1 = vi.fn();
86+
const callback2 = vi.fn();
87+
const unSub1 = location.on('beforeNavigate', callback1);
88+
const unSub2 = location.on('beforeNavigate', callback2);
89+
90+
// Act.
91+
unSub1();
92+
93+
// Assert.
94+
globalThis.window.history.pushState(null, '', 'http://example.com/other');
95+
expect(callback1).not.toHaveBeenCalled();
96+
expect(callback2).toHaveBeenCalledOnce();
97+
98+
// Cleanup.
99+
unSub2();
100+
});
71101
test.each([
72102
{
73103
method: 'push',

0 commit comments

Comments
 (0)