Skip to content

Commit

Permalink
Add more unit tests of callbackToPromise
Browse files Browse the repository at this point in the history
Include unit tests for all cases of the callback index being explicately
set.
  • Loading branch information
rmeritz committed Jun 11, 2020
1 parent 20b1ddc commit 955616c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions test/callback_to_promise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ describe('callbackToPromise', function() {
callback(null, this + value);
}

function returnThisPlusUpToThreeValues(v1, v2, v3, callback) {
callback(null, this + v1 + (v2 || 0) + (v3 || 0));
}

function rejectValue(value, callback) {
callback(new Error('I reject this value'));
}
Expand Down Expand Up @@ -57,8 +61,18 @@ describe('callbackToPromise', function() {
});
});

it('can allow the user to explicately identify the index of the callback', function() {
var wrapped = callbackToPromise(returnThisPlusValue, 1, 1);
it('can allow the user to explicitly identify the index of the callback', function() {
var wrapped = callbackToPromise(returnThisPlusUpToThreeValues, 1, 3);
return expect(wrapped(2, 3, 4)).resolves.toBe(10);
});

it('can allow the user to explicitly identify the index of the callback with too few arguments', function() {
var wrapped = callbackToPromise(returnThisPlusUpToThreeValues, 1, 3);
return expect(wrapped(2)).resolves.toBe(3);
});

it('can allow the user to explicitly identify the index of the callback with too many arguments', function() {
var wrapped = callbackToPromise(sum, 1, 3);
return expect(wrapped(2, 3, 4, 5)).resolves.toBe(14);
});
});

0 comments on commit 955616c

Please sign in to comment.