Skip to content

Commit 6ec0221

Browse files
committed
add unsubscribe test
1 parent 741d8bc commit 6ec0221

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/signals/signals/src/core/signal-generators/dom-gen/__tests__/navigation-gen.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,40 @@ describe(OnNavigationEventGenerator, () => {
125125
const lastCall = emitSpy.mock.lastCall![0].data as URLChangeNavigationData
126126
expect(lastCall.changedProperties).toEqual(['hash'])
127127
})
128+
129+
it('should stop emitting events after unsubscribe is called', () => {
130+
const generator = new OnNavigationEventGenerator()
131+
132+
const unsubscribe = generator.register(emitter)
133+
134+
// Simulate a URL change
135+
const newUrl = new URL(location.href)
136+
newUrl.pathname = '/new-path'
137+
setLocation({
138+
href: newUrl.href,
139+
pathname: newUrl.pathname,
140+
})
141+
142+
// Advance the timers to trigger the polling
143+
jest.advanceTimersByTime(1000)
144+
145+
// Ensure the event is emitted before unsubscribe
146+
expect(emitSpy).toHaveBeenCalledTimes(2)
147+
148+
// Unsubscribe the generator
149+
unsubscribe()
150+
151+
// Simulate another URL change
152+
newUrl.pathname = '/another-path'
153+
setLocation({
154+
href: newUrl.href,
155+
pathname: newUrl.pathname,
156+
})
157+
158+
// Advance the timers again
159+
jest.advanceTimersByTime(1000)
160+
161+
// Ensure no additional events are emitted after unsubscribe
162+
expect(emitSpy).toHaveBeenCalledTimes(2)
163+
})
128164
})

packages/signals/signals/src/core/signal-generators/dom-gen/navigation-gen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createNavigationSignal } from '../../../types/factories'
44
import { SignalEmitter } from '../../emitter'
55
import { SignalGenerator } from '../types'
66

7-
function getURLDifferences(url1: URL, url2: URL): ChangedProperties[] {
7+
function getChangedProperties(url1: URL, url2: URL): ChangedProperties[] {
88
const changed: ChangedProperties[] = []
99
const propertiesToCompare = ['pathname', 'search', 'hash'] as const
1010

@@ -44,7 +44,7 @@ export class OnNavigationEventGenerator implements SignalGenerator {
4444
createNavigationSignal({
4545
action: 'urlChange',
4646
prevUrl: previous.href,
47-
changedProperties: getURLDifferences(current, previous),
47+
changedProperties: getChangedProperties(current, previous),
4848
...this.createCommonFields(),
4949
})
5050
)

0 commit comments

Comments
 (0)