1+ var assert = require ( 'assert' ) ;
2+ var wrapPromiseCallback = require ( '../utils.js' ) . wrapPromiseCallback ;
3+
4+ describe ( 'utils' , function ( ) {
5+ describe ( 'wrapPromiseCallback' , function ( ) {
6+ it ( 'should resolve to the value' , function ( done ) {
7+ const promise = wrapPromiseCallback ( Promise . resolve ( 'woohoo' ) ) ;
8+ promise . then ( function ( value ) {
9+ expect ( value ) . to . equal ( 'woohoo' ) ;
10+ done ( ) ;
11+ } ) ;
12+ } ) ;
13+
14+ it ( 'should reject with the error' , function ( done ) {
15+ const error = new Error ( 'something went wrong' ) ;
16+ const promise = wrapPromiseCallback ( Promise . reject ( error ) ) ;
17+ promise . catch ( function ( error ) {
18+ expect ( error ) . to . equal ( error ) ;
19+ done ( ) ;
20+ } ) ;
21+ } ) ;
22+
23+ it ( 'should call the callback with a value if the promise resolves' , function ( done ) {
24+ const callback = sinon . spy ( ) ;
25+ const promise = wrapPromiseCallback ( Promise . resolve ( 'woohoo' ) , callback ) ;
26+
27+ promise . then ( function ( result ) {
28+ expect ( result ) . to . equal ( 'woohoo' ) ;
29+ // callback run on next tick to maintain asynchronous expections
30+ setTimeout ( function ( ) {
31+ expect ( callback . calledWith ( null , 'woohoo' ) ) . to . be . true ;
32+ done ( ) ;
33+ } , 0 ) ;
34+ } ) ;
35+ } ) ;
36+
37+ it ( 'should call the callback with an error if the promise rejects' , function ( done ) {
38+ const error = new Error ( 'something went wrong' ) ;
39+ const callback = sinon . spy ( ) ;
40+ const promise = wrapPromiseCallback ( Promise . reject ( error ) , callback ) ;
41+
42+ promise . catch ( function ( v ) {
43+ expect ( v ) . to . equal ( error ) ;
44+ // callback run on next tick to maintain asynchronous expections
45+ setTimeout ( function ( ) {
46+ expect ( callback . calledWith ( error , null ) ) . to . be . true ;
47+ done ( ) ;
48+ } , 0 ) ;
49+ } ) ;
50+ } ) ;
51+ } ) ;
52+ } ) ;
0 commit comments