Skip to content

Commit

Permalink
Extend callbackToPromise tests
Browse files Browse the repository at this point in the history
- Add 100% coverage
- Test promise reject
- Test callbackArgIndex argument usage
  • Loading branch information
rmeritz committed Jun 9, 2020
1 parent b91a452 commit 9b84555
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 0 additions & 1 deletion lib/callback_to_promise.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// istanbul ignore file
'use strict';

var Promise = require('./promise');
Expand Down
16 changes: 15 additions & 1 deletion 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 rejectValue(value, callback) {
callback(new Error('I reject this value'));
}

function sum() {
var callback = arguments[arguments.length - 1];
var result = 0;
Expand All @@ -16,11 +20,16 @@ describe('callbackToPromise', function() {
callback(null, result);
}

it('lets a function return a promise', function() {
it('lets a function return a promise that can resolve', function() {
var wrapped = callbackToPromise(returnThisPlusValue, 1);
expect(wrapped(2)).resolves.toBe(3);
});

it('lets a function return a promise that can reject', function() {
var wrapped = callbackToPromise(rejectValue, 1);
expect(wrapped(2)).rejects.toThrow(/reject this value/);
});

it('maintains the ability to call a function with a callback', function(done) {
var wrapped = callbackToPromise(returnThisPlusValue, 1);
wrapped(2, function(err, result) {
Expand All @@ -47,4 +56,9 @@ describe('callbackToPromise', function() {
});
});
});

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

0 comments on commit 9b84555

Please sign in to comment.