finally.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. var assert = require("chai").assert
  3. , sinon = require("sinon")
  4. , identity = require("../../function/identity")
  5. , finallyMethod = require("../../thenable_/finally");
  6. var throwUnexpected = function () { throw new Error("Unexpected"); };
  7. describe("thenable_/finally", function () {
  8. describe("Successful on fulfilled", function () {
  9. var callback, input, result;
  10. before(function () {
  11. callback = sinon.fake();
  12. input = Promise.resolve("foo");
  13. result = finallyMethod.call(input, callback);
  14. return result;
  15. });
  16. it("Should invoke finally callback", function () { assert(callback.calledOnce); });
  17. it("Return promise should fulfill with original result", function () {
  18. return Promise.all([input, result]).then(function (results) {
  19. assert.equal(results[0], results[1]);
  20. });
  21. });
  22. });
  23. describe("Successful on rejected", function () {
  24. var callback, input, result;
  25. before(function () {
  26. var inputError = new Error("Rejected");
  27. callback = sinon.fake();
  28. input = Promise.reject(inputError);
  29. result = finallyMethod.call(input, callback);
  30. return result["catch"](function (error) { if (error !== inputError) throw error; });
  31. });
  32. it("Should invoke finally callback", function () { assert(callback.calledOnce); });
  33. it("Return promise should fulfill with original result", function () {
  34. return Promise.all([input["catch"](identity), result["catch"](identity)]).then(
  35. function (results) { assert.equal(results[0], results[1]); }
  36. );
  37. });
  38. });
  39. describe("Failed on fulfilled", function () {
  40. var callback, result, finallyError;
  41. before(function () {
  42. finallyError = new Error("Finally Rejected");
  43. callback = sinon.fake["throws"](finallyError);
  44. var input = Promise.resolve("foo");
  45. result = finallyMethod.call(input, callback);
  46. return result["catch"](function (error) { if (error !== finallyError) throw error; });
  47. });
  48. it("Should invoke finally callback", function () { assert(callback.calledOnce); });
  49. it("Return promise should be rejected with finally error", function () {
  50. return result.then(throwUnexpected, function (error) {
  51. assert.equal(finallyError, error);
  52. });
  53. });
  54. });
  55. describe("Failed on rejected", function () {
  56. var callback, result, finallyError;
  57. before(function () {
  58. finallyError = new Error("Finally Rejected");
  59. callback = sinon.fake["throws"](finallyError);
  60. var input = Promise.reject(new Error("Rejected"));
  61. result = finallyMethod.call(input, callback);
  62. return result["catch"](function (error) { if (error !== finallyError) throw error; });
  63. });
  64. it("Should invoke finally callback", function () { assert(callback.calledOnce); });
  65. it("Return promise should be rejected with finally error", function () {
  66. return result.then(throwUnexpected, function (error) {
  67. assert.equal(finallyError, error);
  68. });
  69. });
  70. });
  71. });