From 54c8d88ae0630684974ba393681d1583ce2878e7 Mon Sep 17 00:00:00 2001 From: Rebecca Meritz Date: Tue, 9 Jun 2020 15:32:05 -0700 Subject: [PATCH 1/3] Extend callbackToPromise tests - Add 100% coverage - Test promise reject - Test callbackArgIndex argument usage --- lib/callback_to_promise.js | 1 - test/callback_to_promise.test.js | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/callback_to_promise.js b/lib/callback_to_promise.js index 755102c4..843667f1 100644 --- a/lib/callback_to_promise.js +++ b/lib/callback_to_promise.js @@ -1,4 +1,3 @@ -// istanbul ignore file 'use strict'; var Promise = require('./promise'); diff --git a/test/callback_to_promise.test.js b/test/callback_to_promise.test.js index c6aed821..b98c6686 100644 --- a/test/callback_to_promise.test.js +++ b/test/callback_to_promise.test.js @@ -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; @@ -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) { @@ -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); + }); }); From 20b1ddc2f9bbabd6f5c9e6976983db3b3e9359a5 Mon Sep 17 00:00:00 2001 From: Rebecca Meritz Date: Wed, 10 Jun 2020 12:44:14 -0700 Subject: [PATCH 2/3] Ensure that promises are correctly tested By returning the result https://jestjs.io/docs/en/expect.html#resolves --- test/callback_to_promise.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/callback_to_promise.test.js b/test/callback_to_promise.test.js index b98c6686..3a4754b4 100644 --- a/test/callback_to_promise.test.js +++ b/test/callback_to_promise.test.js @@ -22,12 +22,12 @@ describe('callbackToPromise', 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); - expect(wrapped(2)).rejects.toThrow(/reject this value/); + return expect(wrapped(2)).rejects.toThrow(/reject this value/); }); it('maintains the ability to call a function with a callback', function(done) { @@ -59,6 +59,6 @@ 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); + return expect(wrapped(2)).resolves.toBe(3); }); }); From 955616c524aed5fe5ebbe8998477ede5ced0aa47 Mon Sep 17 00:00:00 2001 From: Rebecca Meritz Date: Thu, 11 Jun 2020 10:51:05 -0700 Subject: [PATCH 3/3] Add more unit tests of callbackToPromise Include unit tests for all cases of the callback index being explicately set. --- test/callback_to_promise.test.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/callback_to_promise.test.js b/test/callback_to_promise.test.js index 3a4754b4..9af89ce3 100644 --- a/test/callback_to_promise.test.js +++ b/test/callback_to_promise.test.js @@ -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')); } @@ -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); + }); });