|
| 1 | +jest.unmock('../promisify'); |
| 2 | + |
| 3 | +import promisify from '../promisify'; |
| 4 | +import sinon from 'sinon' |
| 5 | + |
| 6 | +describe('promisify', () => { |
| 7 | + let NativeModule; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + NativeModule = { |
| 11 | + nativeFn: sinon.spy(), |
| 12 | + nativeRet: (callback) => callback(null, true), |
| 13 | + nativeErr: (callback) => callback({type: 'error'}) |
| 14 | + } |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns a function to be called', () => { |
| 18 | + expect( |
| 19 | + typeof promisify('nativeFn', NativeModule) |
| 20 | + ).toEqual('function') |
| 21 | + }) |
| 22 | + |
| 23 | + it('returns a function which returns a promise', () => { |
| 24 | + const promise = promisify('nativeFn', NativeModule)(); |
| 25 | + expect(promise instanceof Promise).toBeTruthy(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('calls the native function when called', () => { |
| 29 | + const promise = promisify('nativeFn', NativeModule)(); |
| 30 | + expect(NativeModule.nativeFn.called).toBeTruthy(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('resolves its promise when the native function returns', (done) => { |
| 34 | + const promise = promisify('nativeRet', NativeModule)(); |
| 35 | + promise.then((res) => { |
| 36 | + expect(res).toBeTruthy(); |
| 37 | + done(); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('rejects when the natie function returns an error', (done) => { |
| 42 | + const promise = promisify('nativeErr', NativeModule)(); |
| 43 | + promise.catch((err) => { |
| 44 | + expect(err.type).toBe('error') |
| 45 | + done(); |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | +}) |
0 commit comments