-
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.
test: ✅ add tests on hooks and small refactor
- Loading branch information
Showing
7 changed files
with
592 additions
and
57 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 |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { PluginApi } from '../utils/utils.ts' | ||
import { createHook, executeStep } from './hook.ts' | ||
|
||
const okStatus = { status: { result: 'OK' } } as const | ||
const koStatus = { status: { result: 'KO', message: 'Failed' } } as const | ||
|
||
async function simpleOkHookCall() { | ||
return okStatus | ||
} | ||
async function simpleFailedHookCall() { | ||
return koStatus | ||
} | ||
|
||
describe('test executeStep mechanism', () => { | ||
it('test payload results, everything ok', async () => { | ||
const results = await executeStep({ | ||
plugin1: simpleOkHookCall, | ||
plugin2: simpleOkHookCall, | ||
}, { apis: {}, args: {}, config: {}, failed: false, results: {} }, 'main') | ||
|
||
expect(results.apis).toEqual({}) | ||
expect(results.args).toEqual({}) | ||
expect(results.config).toEqual({}) | ||
expect(results.failed).toBe(false) | ||
expect(results.results).toEqual({ | ||
plugin1: { ...okStatus, executionTime: { main: expect.any(Number) } }, | ||
plugin2: { ...okStatus, executionTime: { main: expect.any(Number) } }, | ||
}) | ||
}) | ||
|
||
it('test payload results, everything ko', async () => { | ||
const results = await executeStep({ | ||
plugin1: simpleFailedHookCall, | ||
plugin2: simpleFailedHookCall, | ||
}, { apis: {}, args: {}, config: {}, failed: false, results: {} }, 'main') | ||
|
||
expect(results.apis).toEqual({}) | ||
expect(results.args).toEqual({}) | ||
expect(results.config).toEqual({}) | ||
expect(results.failed).contain('plugin1') | ||
expect(results.failed).contain('plugin2') | ||
expect(results.results).toEqual({ | ||
plugin1: { ...koStatus, executionTime: { main: expect.any(Number) } }, | ||
plugin2: { ...koStatus, executionTime: { main: expect.any(Number) } }, | ||
}) | ||
}) | ||
|
||
it('test payload results, partial ko', async () => { | ||
const results = await executeStep({ | ||
plugin1: simpleOkHookCall, | ||
plugin2: simpleFailedHookCall, | ||
}, { apis: {}, args: {}, config: {}, failed: false, results: {} }, 'main') | ||
|
||
expect(results.apis).toEqual({}) | ||
expect(results.args).toEqual({}) | ||
expect(results.config).toEqual({}) | ||
expect(results.failed).not.contain('plugin1') | ||
expect(results.failed).contain('plugin2') | ||
expect(results.results).toEqual({ | ||
plugin1: { ...okStatus, executionTime: { main: expect.any(Number) } }, | ||
plugin2: { ...koStatus, executionTime: { main: expect.any(Number) } }, | ||
}) | ||
}) | ||
}) | ||
|
||
describe('createHook', () => { | ||
it('test empty hookStructure', async () => { | ||
const hook = createHook(false) | ||
expect(hook).toEqual({ | ||
apis: {}, | ||
steps: { | ||
check: {}, | ||
pre: {}, | ||
main: {}, | ||
post: {}, | ||
revert: {}, | ||
}, | ||
execute: expect.any(Function), | ||
validate: expect.any(Function), | ||
}) | ||
|
||
const hookUnique = createHook(true) | ||
expect(hookUnique.uniquePlugin).toBe('') | ||
}) | ||
|
||
it('test hook execution, simple ok', async () => { | ||
const hook = createHook(false) | ||
hook.steps.main.plugin1 = simpleOkHookCall | ||
|
||
const hookResult = await hook.execute({}, {}) | ||
|
||
expect(hookResult.args).toEqual({}) | ||
expect(hookResult.args).toEqual({}) | ||
expect(hookResult.config).toEqual({}) | ||
expect(hookResult.totalExecutionTime).toEqual(expect.any(Number)) | ||
expect(hookResult.failed).toEqual(false) | ||
expect(hookResult.results).toEqual({ | ||
plugin1: { ...okStatus, executionTime: { main: expect.any(Number) } }, | ||
}) | ||
}) | ||
|
||
it('test payload results, multistep ok', async () => { | ||
const hook = createHook(false) | ||
hook.steps.pre.plugin1 = simpleOkHookCall | ||
hook.steps.main.plugin1 = simpleOkHookCall | ||
hook.steps.post.plugin1 = simpleOkHookCall | ||
|
||
const hookResult = await hook.execute({}, {}) | ||
|
||
expect(hookResult.args).toEqual({}) | ||
expect(hookResult.config).toEqual({}) | ||
expect(hookResult.totalExecutionTime).toEqual(expect.any(Number)) | ||
expect(hookResult.failed).toEqual(false) | ||
expect(hookResult.results).toEqual({ plugin1: { ...okStatus, executionTime: { | ||
pre: expect.any(Number), | ||
main: expect.any(Number), | ||
post: expect.any(Number), | ||
} } }) | ||
}) | ||
|
||
it('test payload results, main fails', async () => { | ||
const hook = createHook(false) | ||
hook.apis.plugin1 = () => new PluginApi() // à tester ailleurs | ||
hook.steps.pre.plugin1 = simpleOkHookCall | ||
hook.steps.main.plugin1 = simpleFailedHookCall | ||
hook.steps.post.plugin1 = simpleOkHookCall | ||
|
||
const hookResult = await hook.execute({}, {}) | ||
expect(hookResult.args).toEqual({}) | ||
expect(hookResult.config).toEqual({}) | ||
expect(hookResult.totalExecutionTime).toEqual(expect.any(Number)) | ||
expect(hookResult.failed).toEqual(['plugin1']) | ||
expect(hookResult.results).toEqual({ | ||
plugin1: { | ||
...koStatus, | ||
executionTime: { | ||
pre: expect.any(Number), | ||
main: expect.any(Number), | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
it('test hook validate, simple ok', async () => { | ||
const hook = createHook(false) | ||
hook.steps.check.plugin1 = simpleOkHookCall | ||
hook.apis.plugin1 = () => new PluginApi() // à tester ailleurs | ||
|
||
const hookResult = await hook.validate({}, {}) | ||
|
||
expect(hookResult.args).toEqual({}) | ||
expect(hookResult.args).toEqual({}) | ||
expect(hookResult.config).toEqual({}) | ||
expect(hookResult.totalExecutionTime).toEqual(expect.any(Number)) | ||
expect(hookResult.failed).toEqual(false) | ||
expect(hookResult.results).toEqual({ | ||
plugin1: { ...okStatus, executionTime: { validate: expect.any(Number) } }, | ||
}) | ||
}) | ||
|
||
it('test hook validate, fail', async () => { | ||
const hook = createHook(false) | ||
hook.steps.check.plugin1 = simpleFailedHookCall | ||
|
||
const hookResult = await hook.validate({}, {}) | ||
|
||
expect(hookResult.args).toEqual({}) | ||
expect(hookResult.args).toEqual({}) | ||
expect(hookResult.config).toEqual({}) | ||
expect(hookResult.totalExecutionTime).toEqual(expect.any(Number)) | ||
expect(hookResult.failed).toEqual(['plugin1']) | ||
expect(hookResult.results).toEqual({ | ||
plugin1: { ...koStatus, executionTime: { validate: expect.any(Number) } }, | ||
}) | ||
}) | ||
}) |
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
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,76 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { defaultOrNullish, disabledOrDefault, enabledOrDefaultOrNullish, objectEntries, objectKeys, objectValues, specificallyDisabled, specificallyEnabled } from './utils.ts' | ||
|
||
const object = { test1: 1, test2: 2, 3: 'test3' } | ||
|
||
it('should return object keys', () => { | ||
const keys = objectKeys(object) | ||
console.log(typeof keys[2]) | ||
|
||
// cannot gaurantee order in keys | ||
expect(keys[0]).contain('3') | ||
expect(keys[1]).contain('test1') | ||
expect(keys[2]).contain('test2') | ||
}) | ||
|
||
it('should return object entries', () => { | ||
const keys = objectEntries(object) | ||
|
||
expect(keys[0]).toEqual(['3', 'test3']) | ||
expect(keys[1]).toEqual(['test1', 1]) | ||
expect(keys[2]).toEqual(['test2', 2]) | ||
}) | ||
|
||
it('should return object values', () => { | ||
const keys = objectValues(object) | ||
|
||
// cannot gaurantee order in values | ||
expect(keys).contain('test3') | ||
expect(keys).contain(1) | ||
expect(keys).contain(2) | ||
}) | ||
|
||
const values = [ | ||
'', | ||
'nimp', | ||
'enabled', | ||
'default', | ||
'disabled', | ||
] as const | ||
describe('test config parsing', () => { | ||
it('enabledOrDefaultOrNullish', () => { | ||
expect(enabledOrDefaultOrNullish(values[0])).toBeTruthy() | ||
expect(enabledOrDefaultOrNullish(values[1])).toBeFalsy() | ||
expect(enabledOrDefaultOrNullish(values[2])).toBeTruthy() | ||
expect(enabledOrDefaultOrNullish(values[3])).toBeTruthy() | ||
expect(enabledOrDefaultOrNullish(values[4])).toBeFalsy() | ||
}) | ||
it('specificallyDisabled', () => { | ||
expect(specificallyDisabled(values[0])).toBeFalsy() | ||
expect(specificallyDisabled(values[1])).toBeFalsy() | ||
expect(specificallyDisabled(values[2])).toBeFalsy() | ||
expect(specificallyDisabled(values[3])).toBeFalsy() | ||
expect(specificallyDisabled(values[4])).toBeTruthy() | ||
}) | ||
it('specificallyEnabled', () => { | ||
expect(specificallyEnabled(values[0])).toBeFalsy() | ||
expect(specificallyEnabled(values[1])).toBeFalsy() | ||
expect(specificallyEnabled(values[2])).toBeTruthy() | ||
expect(specificallyEnabled(values[3])).toBeFalsy() | ||
expect(specificallyEnabled(values[4])).toBeFalsy() | ||
}) | ||
it('defaultOrNullish', () => { | ||
expect(defaultOrNullish(values[0])).toBeTruthy() | ||
expect(defaultOrNullish(values[1])).toBeFalsy() | ||
expect(defaultOrNullish(values[2])).toBeFalsy() | ||
expect(defaultOrNullish(values[3])).toBeTruthy() | ||
expect(defaultOrNullish(values[4])).toBeFalsy() | ||
}) | ||
it('disabledOrDefault', () => { | ||
expect(disabledOrDefault(values[0])).toBeFalsy() | ||
expect(disabledOrDefault(values[1])).toBeFalsy() | ||
expect(disabledOrDefault(values[2])).toBeFalsy() | ||
expect(disabledOrDefault(values[3])).toBeTruthy() | ||
expect(disabledOrDefault(values[4])).toBeTruthy() | ||
}) | ||
}) |
Oops, something went wrong.