-
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
Showing
6 changed files
with
241 additions
and
1 deletion.
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
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,69 @@ | ||
import { CustomEventEmitter, CustomPayload } from '../oneshot'; | ||
|
||
describe('独自のEventEmitterのテスト', () => { | ||
let dispatcher: CustomEventEmitter; | ||
beforeEach(() => { | ||
dispatcher = new CustomEventEmitter() | ||
dispatcher.removeAllListeners(); | ||
}) | ||
|
||
test('dispatchされることを確認する', () => { | ||
const fn = jest.fn(); | ||
dispatcher.addListener('nya', () => { | ||
fn(); | ||
}) | ||
expect(fn.mock.calls.length).toBe(0); | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(1); | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(2); | ||
}) | ||
|
||
test('引数がある場合', () => { | ||
const fn = jest.fn((x: number, y: number, z: number) => x + y + z); | ||
dispatcher.addListener('add', (payload: CustomPayload['add']) => { | ||
fn(payload.a, payload.b, payload.c) | ||
}); | ||
dispatcher.emit('add', { a: 1, b: 2, c: 3}); | ||
expect(fn.mock.calls[0][0]).toBe(1); | ||
expect(fn.mock.calls[0][1]).toBe(2); | ||
expect(fn.mock.calls[0][2]).toBe(3); | ||
expect(fn.mock.results[0].value).toBe(6); | ||
}) | ||
|
||
test('特定のイベントリスナーの着脱を確認する', () => { | ||
const fn = jest.fn(() => undefined); | ||
// 1回目 | ||
dispatcher.addListener('nya', fn); | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(1); | ||
// 外す | ||
dispatcher.removeListener('nya', fn); | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(1); | ||
// 2回目 | ||
dispatcher.addListener('nya', fn); | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(2); | ||
}) | ||
|
||
test('removeできることを確認する', () => { | ||
dispatcher.addListener('nya', () => undefined); | ||
dispatcher.addListener('foo', (payload: {foo: string}) => undefined); | ||
expect(dispatcher.eventNames()).toEqual(['nya', 'foo']); | ||
dispatcher.removeAllListeners(); | ||
expect(dispatcher.eventNames()).toEqual([]); | ||
}) | ||
|
||
test('複数回セットした場合、多重実行される', () => { | ||
const fn = jest.fn(); | ||
dispatcher.addListener('nya', () => { fn(); }) | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(1); | ||
|
||
dispatcher.addListener('nya', () => { fn(); }) | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(3); | ||
}) | ||
}); | ||
|
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,63 @@ | ||
import { CustomEventEmitter, CustomPayload, HogeClass } from '../oneshot'; | ||
|
||
describe('hogeClassをセットした場合の挙動', () => { | ||
let dispatcher: CustomEventEmitter; | ||
beforeEach(() => { | ||
dispatcher = new CustomEventEmitter() | ||
dispatcher.removeAllListeners(); | ||
}) | ||
|
||
test('HogeClassのセットしたときとセットしていないときの挙動を確認', () => { | ||
const hoge = new HogeClass(); | ||
const hello = jest.fn((payload: CustomPayload['hoge']) => `hello ${payload.name}`);; | ||
expect(hello.mock.calls.length).toBe(0); | ||
dispatcher.emit('hoge', { | ||
name: 'first' | ||
}) | ||
hoge.hello = hello; | ||
dispatcher.setHogeClassEvent(hoge); | ||
dispatcher.emit('hoge', { | ||
name: 'second' | ||
}) | ||
expect(hello.mock.calls.length).toBe(1); | ||
expect(hello.mock.results[0].value).toBe('hello second'); | ||
}) | ||
|
||
test('HogeClassの状態変化', () => { | ||
const hoge = new HogeClass(); | ||
expect(hoge.result).toBeUndefined(); | ||
dispatcher.emit('hoge', { | ||
name: 'hogeee' | ||
}); | ||
expect(hoge.result).not.toBe('Hello hogeee !'); | ||
dispatcher.addListener('hoge', hoge.hello) | ||
dispatcher.emit('hoge', { | ||
name: 'ふえええ' | ||
}); | ||
expect(hoge.result).not.toBe('Hello ふえええ !'); | ||
}) | ||
|
||
test('HogeClassのリセット', () => { | ||
const hoge = new HogeClass(); | ||
const hello = jest.fn((payload: CustomPayload['hoge']) => `hello ${payload.name}`);; | ||
hoge.hello = hello; | ||
|
||
expect((dispatcher as any ).hoge).toBeUndefined(); | ||
dispatcher.emit('hoge', { name: 'foo' }); | ||
expect(hello.mock.calls.length).toBe(0); | ||
|
||
dispatcher.setHogeClassEvent(hoge); | ||
expect((dispatcher as any ).hoge).toEqual(hoge); | ||
expect((dispatcher as any ).hoge instanceof HogeClass).toBeTruthy(); | ||
|
||
dispatcher.emit('hoge', { name: 'foo' }); | ||
expect(hello.mock.calls.length).toBe(1); | ||
expect(hello.mock.results[0].value).toBe('hello foo'); | ||
|
||
dispatcher.emit('reset', undefined); | ||
expect((dispatcher as any ).hoge).toBeUndefined(); | ||
dispatcher.emit('hoge', { name: 'foo' }); | ||
expect(hello.mock.calls.length).toBe(1); | ||
expect(hello.mock.results[1]).toBeUndefined(); | ||
}) | ||
}) |
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,40 @@ | ||
import { CustomEventEmitter } from '../oneshot'; | ||
|
||
describe('独自のEventEmitterのテスト', () => { | ||
let dispatcher: CustomEventEmitter; | ||
beforeAll(() => { | ||
dispatcher = new CustomEventEmitter() | ||
dispatcher.removeAllListeners(); | ||
}) | ||
|
||
test('dispatchされることを確認する', () => { | ||
const fn = jest.fn(); | ||
dispatcher.on('nya', () => { | ||
fn(); | ||
}) | ||
expect(fn.mock.calls.length).toBe(0); | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(1); | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(2); | ||
}) | ||
|
||
test('removeできることを確認する', () => { | ||
dispatcher.on('nya', () => undefined); | ||
dispatcher.on('foo', (payload: {foo: string}) => undefined); | ||
expect(dispatcher.eventNames()).toEqual(['nya', 'foo']); | ||
dispatcher.removeAllListeners(); | ||
expect(dispatcher.eventNames()).toEqual([]); | ||
}) | ||
|
||
test('複数回セットした場合、多重実行される', () => { | ||
const fn = jest.fn(); | ||
dispatcher.on('nya', () => { fn(); }) | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(1); | ||
|
||
dispatcher.on('nya', () => { fn(); }) | ||
dispatcher.emit('nya', {}); | ||
expect(fn.mock.calls.length).toBe(3); | ||
}) | ||
}); |
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,63 @@ | ||
import { EventEmitter } from 'events'; | ||
|
||
export class HogeClass { | ||
public result: string | undefined; | ||
public hello(payload: {name: string}) { | ||
this.result = `Hello ${payload.name} !` | ||
} | ||
} | ||
|
||
export interface CustomPayload { | ||
hoge: { | ||
name: string | ||
}; | ||
foo: { | ||
foo: string; | ||
}; | ||
add: { | ||
a: number; | ||
b: number; | ||
c: number; | ||
}; | ||
nya: {}; | ||
reset: undefined; | ||
} | ||
|
||
export interface CustomListener { | ||
hoge: (payload: CustomPayload['hoge']) => void; | ||
foo: (payload: CustomPayload['foo']) => void; | ||
add: (payload: CustomPayload['add']) => void; | ||
nya: (payload: CustomPayload['nya']) => void; | ||
reset: (payload: CustomPayload['reset']) => void; | ||
} | ||
|
||
export class CustomEventEmitter extends EventEmitter { | ||
|
||
private hoge: HogeClass | undefined; | ||
|
||
public setHogeClassEvent(hoge: HogeClass) { | ||
this.hoge = hoge; | ||
this.addListener('hoge', (payload: CustomPayload['hoge'])=> { | ||
if (this.hoge) { this.hoge.hello(payload) }; | ||
}); | ||
this.addListener('reset', () => { | ||
this.hoge = undefined; | ||
}) | ||
} | ||
|
||
public on<K extends keyof CustomListener>(event: K, listener : CustomListener[K]): this { | ||
return super.on(event as string, listener); | ||
} | ||
|
||
public addListener<K extends keyof CustomListener>(event: K, listener : CustomListener[K]): this { | ||
return super.addListener(event as string, listener); | ||
} | ||
|
||
public emit<K extends keyof CustomListener>(event: K, payload: CustomPayload[K]): boolean { | ||
return super.emit(event, payload); | ||
} | ||
|
||
public removeListener<K extends keyof CustomListener>(event: K, listener: (...args: any[]) => void): this { | ||
return super.removeListener(event, listener); | ||
} | ||
} |
Empty file.