-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
3 changed files
with
129 additions
and
0 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,7 @@ | ||
export const promisify = validatorFn => (...args) => { | ||
if (typeof validatorFn !== 'function') { | ||
throw new Error('[vest/promisify]: Expected validatorFn to be a function.'); | ||
} | ||
|
||
return new Promise(resolve => validatorFn(...args).done(resolve)); | ||
}; |
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,82 @@ | ||
import faker from 'faker'; | ||
import vest from '../..'; | ||
import { dummyTest } from '../../../testUtils/testDummy'; | ||
import { promisify } from '../promisify'; | ||
|
||
describe('Utility: promisify', () => { | ||
let validatorFn; | ||
let validateAsync; | ||
|
||
beforeEach(() => { | ||
validatorFn = jest.fn(() => vest.validate('test-promisify', jest.fn())); | ||
validateAsync = promisify(validatorFn); | ||
}); | ||
|
||
describe('Test arguments', () => { | ||
it('Should throw an error', () => { | ||
const invalidValidateAsync = promisify('invalid'); | ||
expect(() => invalidValidateAsync()).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('Return value', () => { | ||
it('should be a function', () => { | ||
expect(typeof promisify(jest.fn())).toBe('function'); | ||
}); | ||
|
||
it('should be a promise', () => | ||
new Promise(done => { | ||
const res = validateAsync(); | ||
expect(typeof res?.then).toBe('function'); | ||
res.then(() => done()); | ||
})); | ||
}); | ||
|
||
describe('When returned function is invoked', () => { | ||
it('Calls `validatorFn` argument', () => | ||
new Promise(done => { | ||
const validateAsync = promisify(() => | ||
vest.validate('test-promisify', () => done()) | ||
); | ||
validateAsync(); | ||
})); | ||
|
||
it('Passes all arguments over to tests callback', async () => { | ||
const params = [ | ||
1, | ||
{ [faker.random.word()]: [1, 2, 3] }, | ||
false, | ||
[faker.random.word()], | ||
]; | ||
|
||
await validateAsync(...params); | ||
expect(validatorFn).toHaveBeenCalledWith(...params); | ||
}); | ||
}); | ||
|
||
describe('Initial run', () => { | ||
it('Produces correct validation', () => | ||
new Promise(done => { | ||
const validate = vest.create('test-promisify', () => { | ||
dummyTest.failing('field_0'); | ||
dummyTest.failingAsync('field_1'); | ||
}); | ||
|
||
const validateAsync = promisify(validate); | ||
|
||
const promise = validateAsync(); | ||
const res = vest.get('test-promisify'); | ||
|
||
expect(res.hasErrors('field_0')).toBe(true); | ||
expect(res.hasErrors('field_1')).toBe(false); | ||
|
||
promise.then(result => { | ||
expect(result.hasErrors('field_0')).toBe(true); | ||
expect(result.hasErrors('field_1')).toBe(true); | ||
expect(result).not.toBe(res); | ||
expect(result).not.isDeepCopyOf(res); | ||
done(); | ||
}); | ||
})); | ||
}); | ||
}); |