-
Notifications
You must be signed in to change notification settings - Fork 160
/
promiser.spec.js
45 lines (36 loc) 路 1.33 KB
/
promiser.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const promiser = require('../promiser');
const config = { foo: 'foo' };
describe('promiser', () => {
it('should execute function without error and return promise if no callback provided', async () => {
const func = jest.fn(() => Promise.resolve('bar'));
const result = await promiser(func)(config)();
expect(func).toHaveBeenCalledWith(config);
expect(result).toEqual('bar');
});
it('should execute function with error and return promise if no callback provided', async () => {
expect.assertions(2);
const func = jest.fn(() => Promise.reject('error'));
try {
await promiser(func)(config)();
} catch (error) {
expect(func).toHaveBeenCalledWith(config);
expect(error).toEqual('error');
}
});
it('should execute function without error and call callback', done => {
const func = jest.fn(() => Promise.resolve('bar'));
promiser(func)(config)((error, result) => {
expect(func).toHaveBeenCalledWith(config);
expect(result).toEqual('bar');
done();
});
});
it('should execute function with error and return promise if no callback provided', done => {
const func = jest.fn(() => Promise.reject('error'));
promiser(func)(config)(error => {
expect(func).toHaveBeenCalledWith(config);
expect(error).toEqual('error');
done();
});
});
});