-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form-builder/validation): allow async validation (#26)
- Loading branch information
Showing
4 changed files
with
99 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
52 changes: 0 additions & 52 deletions
52
libs/form-builder/src/lib/utils/__tests__/validation.utils.spec.js
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
libs/form-builder/src/lib/utils/__tests__/validation.utils.spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { getFieldRules, handleValidateErrorMessage } from '../validation.utils'; | ||
|
||
describe('handleValidateErrorMessage', () => { | ||
const validate = jest.fn(); | ||
const message = 'error label'; | ||
|
||
it('should return validate result if truthy', async () => { | ||
validate.mockResolvedValue(true); | ||
await expect( | ||
handleValidateErrorMessage(validate, message)() | ||
).resolves.toEqual(true); | ||
}); | ||
|
||
it('should return the message if falsy', async () => { | ||
validate.mockResolvedValue(false); | ||
await expect( | ||
handleValidateErrorMessage(validate, message)() | ||
).resolves.toEqual('error label'); | ||
}); | ||
}); | ||
|
||
describe('getFieldRules', () => { | ||
describe('extraValidation', () => { | ||
const extraValidation = { | ||
func1: jest.fn(() => () => Promise.resolve(true)), | ||
func2: jest.fn(() => () => Promise.resolve(false)) | ||
}; | ||
|
||
it('should return hook rules when we provide default rule names', () => { | ||
const validation = { | ||
func1: { key: 'required', value: true, message: 'required error' }, | ||
func2: { key: 'minLength', value: 8, message: 'minLength error' } | ||
}; | ||
|
||
const resultHook = { | ||
required: { value: true, message: 'required error' } | ||
}; | ||
|
||
expect(getFieldRules({ validation, extraValidation })).toEqual( | ||
resultHook | ||
); | ||
}); | ||
|
||
it('should complete validate if we provide existing extraValidation', async () => { | ||
const validation = { | ||
func1: { key: 'func1', value: '12', message: 'error label' }, | ||
func2: { key: 'func2', value: '12', message: 'error label' } | ||
}; | ||
|
||
const rules = getFieldRules({ validation, extraValidation }); | ||
expect(rules.validate).toEqual({ | ||
func1: expect.any(Function), | ||
func2: expect.any(Function) | ||
}); | ||
await expect(rules?.validate?.func1()).resolves.toEqual(true); | ||
await expect(rules?.validate?.func2()).resolves.toBe('error label'); | ||
}); | ||
|
||
it('should not complete validate if we provide missing extraValidation function', () => { | ||
const validation = { | ||
func1: { key: 'func1', value: '', message: 'func1 error' }, | ||
badFunc: { key: 'badFunc', value: '', message: 'badFunc error' } | ||
}; | ||
|
||
const rules = getFieldRules({ validation, extraValidation }); | ||
|
||
expect(JSON.stringify(rules.validate)).toEqual( | ||
JSON.stringify({ func1: extraValidation.func1() }) | ||
); | ||
expect(rules?.validate?.func2).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