-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92a92e0
commit 735c235
Showing
5 changed files
with
114 additions
and
32 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import createSignal, { Signal } from '../src/nanosignal'; | ||
|
||
describe('when a listener unsubscribes another listener during dispatch', () => { | ||
let dated: Signal<Date>; | ||
let listenerA: jest.Mock<any>; | ||
let listenerB: jest.Mock<any>; | ||
let listenerC: jest.Mock<any>; | ||
beforeEach(() => { | ||
dated = createSignal(); | ||
listenerA = jest.fn(); | ||
const unsubscribeA = dated.subscribe(listenerA); | ||
listenerB = jest.fn(unsubscribeA); | ||
listenerC = jest.fn(); | ||
dated.subscribe(listenerB); | ||
dated.subscribe(listenerC); | ||
// dispatch twice and expect the unsubscribed listener to be called only once | ||
dated(new Date()); | ||
dated(new Date()); | ||
}); | ||
|
||
it('the first listener is called once then unsubscribed', () => { | ||
expect(listenerA).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('no other listener is skipped', () => { | ||
expect(listenerB).toHaveBeenCalledTimes(2); | ||
expect(listenerC).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import createSignal, { Signal, Unsubscriber } from '../src/nanosignal'; | ||
|
||
describe('Signal', () => { | ||
let dated: Signal<Date>; | ||
beforeEach(() => { | ||
dated = createSignal(); | ||
}); | ||
it('is a function', () => expect(typeof dated).toBe('function')); | ||
|
||
describe('given 2 listeners', () => { | ||
let listenerA: jest.Mock<any>; | ||
let listenerB: jest.Mock<any>; | ||
const payload = new Date(); | ||
beforeEach(() => { | ||
listenerA = jest.fn(); | ||
listenerB = jest.fn(); | ||
dated.subscribe(listenerA); | ||
dated.subscribe(listenerB); | ||
dated(payload); | ||
}); | ||
|
||
it('when the signal fires, the listeners receive the payload', () => { | ||
expect(listenerA).toHaveBeenCalledWith(payload); | ||
expect(listenerB).toHaveBeenCalledWith(payload); | ||
}); | ||
}); | ||
|
||
describe('given a firstListener argument and then subscribe() a second listener', () => { | ||
let listenerA: jest.Mock<any>; | ||
let listenerB: jest.Mock<any>; | ||
const payload = new Date(); | ||
beforeEach(() => { | ||
listenerA = jest.fn(); | ||
listenerB = jest.fn(); | ||
dated = createSignal(listenerA); | ||
dated.subscribe(listenerB); | ||
dated(payload); | ||
}); | ||
|
||
it('when the signal fires, the listeners receive the payload', () => { | ||
expect(listenerA).toHaveBeenCalledWith(payload); | ||
expect(listenerB).toHaveBeenCalledWith(payload); | ||
}); | ||
}); | ||
|
||
describe('when removing a listener before firing', () => { | ||
let listenerA: jest.Mock<any>; | ||
let removeA: Unsubscriber; | ||
beforeEach(() => { | ||
listenerA = jest.fn(); | ||
removeA = dated.subscribe(listenerA); | ||
removeA(); | ||
dated(new Date()); | ||
}); | ||
|
||
it('the listener is not called', () => { | ||
expect(listenerA).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters