-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(hook): seriesHook should discard handlers that returns undefined (#…
…441)
- Loading branch information
Showing
5 changed files
with
270 additions
and
150 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
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 |
---|---|---|
@@ -1,56 +1,56 @@ | ||
import { createSyncBailHook } from '../../index'; | ||
import { createSyncBailHook, SyncBailHookHandler } from '../../index'; | ||
|
||
beforeEach(() => {}); | ||
|
||
afterEach(async () => {}); | ||
|
||
describe('syncBailHook', () => { | ||
console.log = jest.fn(); | ||
test('order', async () => { | ||
describe('SyncBailHook', () => { | ||
test('hooks should run in sequence', () => { | ||
const hook = createSyncBailHook<void>(); | ||
const logs: string[] = []; | ||
hook.use(() => { | ||
console.log('hook 1'); | ||
logs.push('hook 1'); | ||
}); | ||
hook.use(() => { | ||
console.log('hook 2'); | ||
logs.push('hook 2'); | ||
}); | ||
hook.run(); | ||
expect(console.log).toHaveBeenNthCalledWith(1, 'hook 1'); | ||
expect(console.log).toHaveBeenNthCalledWith(2, 'hook 2'); | ||
expect(logs).toEqual(['hook 1', 'hook 2']); | ||
}); | ||
|
||
test('with initialValue', async () => { | ||
const hook = createSyncBailHook<number>(); | ||
hook.use(e => { | ||
console.log(e); | ||
}); | ||
hook.run(10); | ||
expect(console.log).toHaveBeenCalledWith(10); | ||
}); | ||
test('with extraArg', async () => { | ||
const hook = createSyncBailHook<void, number>(); | ||
hook.use(e => { | ||
console.log(e); | ||
}); | ||
hook.run(10); | ||
expect(console.log).toHaveBeenCalledWith(10); | ||
test('the params of run() should be as the params of the hook handler', () => { | ||
const hook = createSyncBailHook<number, number>(); | ||
const handler: SyncBailHookHandler<number, number, void> = jest.fn( | ||
(i, e) => { | ||
expect(i).toBe(1); | ||
expect(e).toBe(2); | ||
} | ||
); | ||
hook.use(handler); | ||
hook.run(1, 2); | ||
expect(handler).toBeCalledTimes(1); | ||
}); | ||
|
||
test('with initialValue and extraArg', async () => { | ||
const hook = createSyncBailHook<number, number>(); | ||
hook.use((i, e) => { | ||
console.log(i, e); | ||
}); | ||
hook.run(10, 20); | ||
expect(console.log).toHaveBeenCalledWith(10, 20); | ||
test('hook result should be the return value of the first handler that returns value', () => { | ||
const hook = createSyncBailHook<void, void, number>(); | ||
const handler1 = jest.fn(() => {}); | ||
const handler2 = jest.fn(() => 10); | ||
const handler3 = jest.fn(() => 20); | ||
hook.use(handler1); | ||
hook.use(handler2); | ||
hook.use(handler3); | ||
const result = hook.run(); | ||
expect(result).toBe(10); | ||
expect(handler1).toBeCalledTimes(1); | ||
expect(handler2).toBeCalledTimes(1); | ||
expect(handler3).toBeCalledTimes(0); | ||
}); | ||
|
||
test('with return value', async () => { | ||
test('hook result should be undefined if none of the handlers returns value', () => { | ||
const hook = createSyncBailHook<void, void, number>(); | ||
hook.use(() => {}); | ||
hook.use(() => 10); | ||
hook.use(() => 20); | ||
const handler1 = jest.fn(() => {}); | ||
const handler2 = jest.fn(() => {}); | ||
hook.use(handler1); | ||
hook.use(handler2); | ||
const result = hook.run(); | ||
expect(result).toStrictEqual(10); | ||
expect(result).toBe(undefined); | ||
expect(handler1).toBeCalledTimes(1); | ||
expect(handler2).toBeCalledTimes(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 |
---|---|---|
@@ -1,107 +1,130 @@ | ||
import { createSyncHook } from '../../hooks'; | ||
import { SyncHookHandler, createSyncHook } from '../../hooks'; | ||
|
||
beforeEach(() => {}); | ||
|
||
afterEach(async () => {}); | ||
|
||
describe('syncHook', () => { | ||
console.log = jest.fn(); | ||
test('order', async () => { | ||
describe('AsyncSeriesHook', () => { | ||
test('hooks should run in sequence', () => { | ||
const hook = createSyncHook<void>(); | ||
const logs: string[] = []; | ||
hook.use(() => { | ||
console.log('hook 1'); | ||
logs.push('hook 1'); | ||
}); | ||
hook.use(() => { | ||
console.log('hook 2'); | ||
logs.push('hook 2'); | ||
}); | ||
hook.run(); | ||
expect(console.log).toHaveBeenNthCalledWith(1, 'hook 1'); | ||
expect(console.log).toHaveBeenNthCalledWith(2, 'hook 2'); | ||
expect(logs).toEqual(['hook 1', 'hook 2']); | ||
}); | ||
|
||
test('initialValue-normal extraArg-normal', async () => { | ||
test('the params of run() should be as the params of the hook handler', () => { | ||
const hook = createSyncHook<number, number>(); | ||
hook.use((i, e) => { | ||
console.log(i, e); | ||
}); | ||
hook.run(10, 20); | ||
expect(console.log).toHaveBeenCalledWith(10, 20); | ||
}); | ||
test('initialValue-void extraArg-normal', async () => { | ||
const hook = createSyncHook<void, number>(); | ||
hook.use(e => { | ||
console.log(e); | ||
const handler: SyncHookHandler<number, number, void> = jest.fn((i, e) => { | ||
expect(i).toBe(1); | ||
expect(e).toBe(2); | ||
}); | ||
hook.run(10); | ||
expect(console.log).toHaveBeenCalledWith(10); | ||
hook.use(handler); | ||
hook.run(1, 2); | ||
expect(handler).toBeCalledTimes(1); | ||
}); | ||
|
||
test('initialValue-any extraArg-normal', async () => { | ||
const hook = createSyncHook<any, number>(); | ||
hook.use((i, e) => { | ||
console.log(i, e); | ||
}); | ||
hook.run('10', 20); | ||
expect(console.log).toHaveBeenCalledWith('10', 20); | ||
test('result should be array', () => { | ||
const hook = createSyncHook<void, void, number | void>(); | ||
hook.use(() => 10); | ||
hook.use(() => {}); | ||
hook.use(() => 20); | ||
const result = hook.run(); | ||
expect(result).toStrictEqual([10, undefined, 20]); | ||
}); | ||
|
||
test('initialValue-normal extraArg-void', async () => { | ||
const hook = createSyncHook<number, void>(); | ||
hook.use(i => { | ||
console.log(i); | ||
describe('types', () => { | ||
test('initialValue-normal extraArg-normal', async () => { | ||
let initialValue: number; | ||
let extraArg: number; | ||
const hook = createSyncHook<number, number>(); | ||
hook.use((i, e) => { | ||
initialValue = i; | ||
extraArg = e; | ||
}); | ||
hook.run(10, 20); | ||
expect(true).toBe(true); | ||
}); | ||
hook.run(10); | ||
expect(console.log).toHaveBeenCalledWith(10); | ||
}); | ||
test('initialValue-void extraArg-void', async () => { | ||
const hook = createSyncHook<void, void>(); | ||
hook.use(() => { | ||
console.log('test'); | ||
test('initialValue-void extraArg-normal', async () => { | ||
let extraArg: number; | ||
const hook = createSyncHook<void, number>(); | ||
hook.use(e => { | ||
extraArg = e; | ||
}); | ||
hook.run(10); | ||
expect(true).toBe(true); | ||
}); | ||
hook.run(); | ||
expect(console.log).toHaveBeenCalledWith('test'); | ||
}); | ||
|
||
test('initialValue-any extraArg-void', async () => { | ||
const hook = createSyncHook<any, void>(); | ||
hook.use(i => { | ||
console.log(i); | ||
test('initialValue-any extraArg-normal', async () => { | ||
let initialValue: any; | ||
let extraArg: number; | ||
const hook = createSyncHook<any, number>(); | ||
hook.use((i, e) => { | ||
initialValue = i; | ||
extraArg = e; | ||
}); | ||
hook.run('10', 20); | ||
expect(true).toBe(true); | ||
}); | ||
hook.run('test any'); | ||
expect(console.log).toHaveBeenCalledWith('test any'); | ||
}); | ||
|
||
test('initialValue-normal extraArg-any', async () => { | ||
const hook = createSyncHook<number, any>(); | ||
hook.use((i, e) => { | ||
console.log(i, e); | ||
test('initialValue-normal extraArg-void', async () => { | ||
let initialValue: number; | ||
const hook = createSyncHook<number, void>(); | ||
hook.use(i => { | ||
initialValue = i; | ||
}); | ||
hook.run(10); | ||
expect(true).toBe(true); | ||
}); | ||
hook.run(10, '20'); | ||
expect(console.log).toHaveBeenCalledWith(10, '20'); | ||
}); | ||
test('initialValue-void extraArg-any', async () => { | ||
const hook = createSyncHook<void, any>(); | ||
hook.use(() => { | ||
console.log('test any'); | ||
test('initialValue-void extraArg-void', async () => { | ||
const hook = createSyncHook<void, void>(); | ||
hook.use(() => {}); | ||
hook.run(); | ||
expect(true).toBe(true); | ||
}); | ||
hook.run('test any'); | ||
expect(console.log).toHaveBeenCalledWith('test'); | ||
}); | ||
|
||
test('initialValue-any extraArg-any', async () => { | ||
const hook = createSyncHook<any, any>(); | ||
hook.use((i, e) => { | ||
console.log(i, e); | ||
test('initialValue-any extraArg-void', async () => { | ||
let initialValue: any; | ||
const hook = createSyncHook<any, void>(); | ||
hook.use(i => { | ||
initialValue = i; | ||
}); | ||
hook.run('test any'); | ||
expect(true).toBe(true); | ||
}); | ||
hook.run('test 1', 'test 2'); | ||
expect(console.log).toHaveBeenCalledWith('test 1', 'test 2'); | ||
}); | ||
|
||
test('with return value', async () => { | ||
const hook = createSyncHook<void, void, number>(); | ||
hook.use(() => 10); | ||
hook.use(() => 20); | ||
const result = hook.run(); | ||
expect(result).toStrictEqual([10, 20]); | ||
test('initialValue-normal extraArg-any', async () => { | ||
let initialValue: number; | ||
let extraArg: any; | ||
const hook = createSyncHook<number, any>(); | ||
hook.use((i, e) => { | ||
initialValue = i; | ||
extraArg = e; | ||
}); | ||
hook.run(10, '20'); | ||
expect(true).toBe(true); | ||
}); | ||
test('initialValue-void extraArg-any', async () => { | ||
let extraArg: any; | ||
const hook = createSyncHook<void, any>(); | ||
hook.use(e => { | ||
extraArg = e; | ||
}); | ||
hook.run('test any'); | ||
expect(true).toBe(true); | ||
}); | ||
|
||
test('initialValue-any extraArg-any', async () => { | ||
let initialValue: any; | ||
let extraArg: any; | ||
const hook = createSyncHook<any, any>(); | ||
hook.use((i, e) => { | ||
initialValue = i; | ||
extraArg = e; | ||
}); | ||
hook.run('test 1', 'test 2'); | ||
expect(true).toBe(true); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.