From 8217d68e18a6dd2be70eef6d37d4628c87ba97cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Yves=20G=C3=A9rardy?= Date: Tue, 20 Feb 2018 00:27:39 +0100 Subject: [PATCH] Better tests for Promise.prototype.finally --- promise/tests/test-promise.js | 83 +++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 18 deletions(-) diff --git a/promise/tests/test-promise.js b/promise/tests/test-promise.js index 4e85c9a4..7dcc21f6 100644 --- a/promise/tests/test-promise.js +++ b/promise/tests/test-promise.js @@ -54,30 +54,77 @@ o.spec("promise", function() { o(value).equals(1) }).then(done) }) - o("finally returns promise", function(done) { + o("finally lets a fulfilled value pass though", function(done) { var promise = Promise.resolve(1) + var spy = o.spy(function(){return 2}) - promise.finally(function(value) { - o(value).equals(undefined) - }).finally(done) - }) - o("finally after then returns promise", function(done) { - var promise = Promise.resolve(1) - - promise.then(function(value) { + promise.finally(spy).then(function(value){ o(value).equals(1) - }).finally(function(value) { - o(value).equals(undefined) - }).then(done) + o(spy.callCount).equals(1) + o(spy.args.length).equals(0) + o(spy.this).equals(undefined) + done() + }) }) - o("finally after catch returns promise", function(done) { + o("finally lets a rejected reason pass though", function(done) { var promise = Promise.reject(1) + var spy = o.spy(function(){return 2}) - promise.catch(function(value) { - o(value).equals(1) - }).finally(function(value) { - o(value).equals(undefined) - }).then(done) + promise.finally(spy).catch(function(reason){ + o(reason).equals(1) + o(spy.callCount).equals(1) + o(spy.args.length).equals(0) + o(spy.this).equals(undefined) + done() + }) + }) + o("finally overrrides a fulfilled value when it throws", function(done) { + var promise = Promise.resolve(1) + var spy = o.spy(function(){throw 2}) + + promise.finally(spy).catch(function(reason){ + o(reason).equals(2) + o(spy.callCount).equals(1) + o(spy.args.length).equals(0) + o(spy.this).equals(undefined) + done() + }) + }) + o("finally overrrides a fulfilled value when it returns a rejected Promise", function(done) { + var promise = Promise.resolve(1) + var spy = o.spy(function(){return Promise.reject(2)}) + + promise.finally(spy).catch(function(reason){ + o(reason).equals(2) + o(spy.callCount).equals(1) + o(spy.args.length).equals(0) + o(spy.this).equals(undefined) + done() + }) + }) + o("finally overrrides a rejected reason when it throws", function(done) { + var promise = Promise.reject(1) + var spy = o.spy(function(){throw 2}) + + promise.finally(spy).catch(function(reason){ + o(reason).equals(2) + o(spy.callCount).equals(1) + o(spy.args.length).equals(0) + o(spy.this).equals(undefined) + done() + }) + }) + o("finally overrrides a rejected reason when it returns a rejected Promise", function(done) { + var promise = Promise.reject(1) + var spy = o.spy(function(){return Promise.reject(2)}) + + promise.finally(spy).catch(function(reason){ + o(reason).equals(2) + o(spy.callCount).equals(1) + o(spy.args.length).equals(0) + o(spy.this).equals(undefined) + done() + }) }) }) o.spec("resolve", function() {