-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improved emitter design * Improved jest config * Added unit tests
- Loading branch information
Showing
8 changed files
with
162 additions
and
67 deletions.
There are no files selected for viewing
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 @@ | ||
import '@testing-library/jest-dom'; |
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,10 @@ | ||
{ | ||
"transform": { "^.+\\.ts?$": "ts-jest" }, | ||
"testMatch": ["**/*.test.ts?(x)"], | ||
"testPathIgnorePatterns": ["node_modules", "example", "dist"], | ||
"modulePathIgnorePatterns": ["dist"], | ||
"testEnvironment": "jsdom", | ||
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"], | ||
"collectCoverageFrom": ["src/**/*.ts"], | ||
"setupFilesAfterEnv": ["./jest-setupFilesAfterEnv.ts"] | ||
} |
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
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,77 @@ | ||
import LDEmitter from './LDEmitter'; | ||
|
||
describe('LDEmitter', () => { | ||
const error = { type: 'network', message: 'unreachable' }; | ||
let emitter: LDEmitter; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
emitter = new LDEmitter(); | ||
}); | ||
|
||
test('subscribe and handle', () => { | ||
const errorHandler1 = jest.fn(); | ||
const errorHandler2 = jest.fn(); | ||
|
||
emitter.on('error', errorHandler1); | ||
emitter.on('error', errorHandler2); | ||
emitter.emit('error', error); | ||
|
||
expect(errorHandler1).toHaveBeenCalledWith(error); | ||
expect(errorHandler2).toHaveBeenCalledWith(error); | ||
}); | ||
|
||
test('unsubscribe and handle', () => { | ||
const errorHandler1 = jest.fn(); | ||
const errorHandler2 = jest.fn(); | ||
|
||
emitter.on('error', errorHandler1); | ||
emitter.on('error', errorHandler2); | ||
emitter.off('error'); | ||
emitter.emit('error', error); | ||
|
||
expect(errorHandler1).not.toHaveBeenCalled(); | ||
expect(errorHandler2).not.toHaveBeenCalled(); | ||
expect(emitter.listenerCount('error')).toEqual(0); | ||
}); | ||
|
||
test('unsubscribing an event should not affect other events', () => { | ||
const errorHandler = jest.fn(); | ||
const changeHandler = jest.fn(); | ||
|
||
emitter.on('error', errorHandler); | ||
emitter.on('change', changeHandler); | ||
emitter.off('error'); // unsubscribe error handler | ||
emitter.emit('error', error); | ||
emitter.emit('change'); | ||
|
||
// change handler should still be affective | ||
expect(changeHandler).toHaveBeenCalled(); | ||
expect(errorHandler).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('eventNames', () => { | ||
const errorHandler1 = jest.fn(); | ||
const changeHandler = jest.fn(); | ||
const readyHandler = jest.fn(); | ||
|
||
emitter.on('error', errorHandler1); | ||
emitter.on('change', changeHandler); | ||
emitter.on('ready', readyHandler); | ||
|
||
expect(emitter.eventNames()).toEqual(['error', 'change', 'ready']); | ||
}); | ||
|
||
test('listener count', () => { | ||
const errorHandler1 = jest.fn(); | ||
const errorHandler2 = jest.fn(); | ||
const changeHandler = jest.fn(); | ||
|
||
emitter.on('error', errorHandler1); | ||
emitter.on('error', errorHandler2); | ||
emitter.on('change', changeHandler); | ||
|
||
expect(emitter.listenerCount('error')).toEqual(2); | ||
expect(emitter.listenerCount('change')).toEqual(1); | ||
}); | ||
}); |
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,58 @@ | ||
export type EventName = 'change' | 'internal-change' | 'ready' | 'initialized' | 'failed' | 'error'; | ||
|
||
/** | ||
* This is an event emitter using the standard built-in EventTarget web api. | ||
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget | ||
* | ||
* In react-native use event-target-shim to polyfill EventTarget. This is safe | ||
* because the react-native repo uses it too. | ||
* https://github.com/mysticatea/event-target-shim | ||
*/ | ||
export default class LDEmitter { | ||
private et: EventTarget = new EventTarget(); | ||
private listeners: Map<EventName, EventListener[]> = new Map(); | ||
|
||
/** | ||
* Cache all listeners in a Map so we can remove them later | ||
* @param name string event name | ||
* @param listener function to handle the event | ||
* @private | ||
*/ | ||
private saveListener(name: EventName, listener: EventListener) { | ||
if (!this.listeners.has(name)) { | ||
this.listeners.set(name, [listener]); | ||
} else { | ||
this.listeners.get(name)?.push(listener); | ||
} | ||
} | ||
|
||
on(name: EventName, listener: Function) { | ||
const customListener = (e: Event) => { | ||
const { detail } = e as CustomEvent; | ||
|
||
// invoke listener with args from CustomEvent | ||
listener(...detail); | ||
}; | ||
this.saveListener(name, customListener); | ||
this.et.addEventListener(name, customListener); | ||
} | ||
|
||
off(name: EventName) { | ||
this.listeners.get(name)?.forEach((l) => { | ||
this.et.removeEventListener(name, l); | ||
}); | ||
this.listeners.delete(name); | ||
} | ||
|
||
emit(name: EventName, ...detail: any[]) { | ||
this.et.dispatchEvent(new CustomEvent(name, { detail })); | ||
} | ||
|
||
eventNames(): string[] { | ||
return [...this.listeners.keys()]; | ||
} | ||
|
||
listenerCount(name: EventName): number { | ||
return this.listeners.get(name)?.length ?? 0; | ||
} | ||
} |
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