Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend callbackToPromise tests #181

Merged
merged 3 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 30 additions & 2 deletions test/callback_to_promise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ 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'));
}

function sum() {
var callback = arguments[arguments.length - 1];
var result = 0;
Expand All @@ -16,9 +24,14 @@ 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);
return expect(wrapped(2)).resolves.toBe(3);
});

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

it('maintains the ability to call a function with a callback', function(done) {
Expand Down Expand Up @@ -47,4 +60,19 @@ describe('callbackToPromise', function() {
});
});
});

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);
});
});