-
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.
fix(toCallback): wrong handling of errors
- Loading branch information
Showing
2 changed files
with
35 additions
and
5 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 |
---|---|---|
@@ -1,17 +1,48 @@ | ||
import toCallback from '@promises/to-callback'; | ||
|
||
describe('toCallback', () => { | ||
it('should be reject on callback throw error', () => { | ||
let promise: Promise<string> = Promise.resolve<string>('foo'); | ||
return toCallback(promise, (error: any, result: string) => { | ||
throw 'reject'; | ||
}).then(() => { | ||
throw 'resolve'; | ||
}).catch((error: string) => { | ||
expect(error).toBe('reject'); | ||
}); | ||
}); | ||
|
||
it('should be resolve promise from callback return', () => { | ||
return toCallback<number, number>(0, (error: any, num: number) => { | ||
return Promise.resolve(++num); | ||
}).then((num: number) => { | ||
expect(num).toBe(1); | ||
}); | ||
}); | ||
|
||
it('should be reject promise from callback return', () => { | ||
return toCallback<number, never>(0, (error: any, num: number) => { | ||
return Promise.reject(++num); | ||
}).then(() => { | ||
throw 'resolve'; | ||
}).catch((num: number) => { | ||
expect(num).toBe(1); | ||
}); | ||
}); | ||
|
||
it('should be return result to callback', () => { | ||
let promise: any = Promise.resolve<string>('foo'); | ||
let promise: Promise<string> = Promise.resolve<string>('foo'); | ||
return toCallback(promise, (error: any, result: string) => { | ||
expect(error).toBeNull(); | ||
expect(result).toBe('foo'); | ||
}); | ||
}); | ||
|
||
it('should be return error to callback', () => { | ||
let promise: any = Promise.reject<string>('foo'); | ||
return toCallback(promise, (error: any) => { | ||
let promise: Promise<string> = Promise.reject<string>('foo'); | ||
return toCallback(promise, (error: string, result: any) => { | ||
expect(error).toBe('foo'); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); |