Promise.allSettled.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. test("length is 1", () => {
  2. expect(Promise.allSettled).toHaveLength(1);
  3. });
  4. describe("normal behavior", () => {
  5. test("returns a Promise", () => {
  6. const promise = Promise.allSettled();
  7. expect(promise).toBeInstanceOf(Promise);
  8. });
  9. test("resolve", () => {
  10. const promise1 = Promise.resolve(3);
  11. const promise2 = 42;
  12. const promise3 = new Promise((resolve, reject) => {
  13. resolve("foo");
  14. });
  15. let resolvedValues = null;
  16. let wasRejected = false;
  17. Promise.allSettled([promise1, promise2, promise3]).then(
  18. values => {
  19. resolvedValues = values;
  20. },
  21. () => {
  22. wasRejected = true;
  23. }
  24. );
  25. runQueuedPromiseJobs();
  26. expect(resolvedValues).toEqual([
  27. { status: "fulfilled", value: 3 },
  28. { status: "fulfilled", value: 42 },
  29. { status: "fulfilled", value: "foo" },
  30. ]);
  31. expect(wasRejected).toBeFalse();
  32. });
  33. test("reject", () => {
  34. const promise1 = Promise.resolve(3);
  35. const promise2 = 42;
  36. const promise3 = new Promise((resolve, reject) => {
  37. reject("foo");
  38. });
  39. let resolvedValues = null;
  40. let wasRejected = false;
  41. Promise.allSettled([promise1, promise2, promise3]).then(
  42. values => {
  43. resolvedValues = values;
  44. },
  45. () => {
  46. wasRejected = true;
  47. }
  48. );
  49. runQueuedPromiseJobs();
  50. expect(resolvedValues).toEqual([
  51. { status: "fulfilled", value: 3 },
  52. { status: "fulfilled", value: 42 },
  53. { status: "rejected", reason: "foo" },
  54. ]);
  55. expect(wasRejected).toBeFalse();
  56. });
  57. });
  58. describe("exceptional behavior", () => {
  59. test("cannot invoke capabilities executor twice", () => {
  60. function fn() {}
  61. expect(() => {
  62. function promise(executor) {
  63. executor(fn, fn);
  64. executor(fn, fn);
  65. }
  66. Promise.allSettled.call(promise, []);
  67. }).toThrow(TypeError);
  68. expect(() => {
  69. function promise(executor) {
  70. executor(fn, undefined);
  71. executor(fn, fn);
  72. }
  73. Promise.allSettled.call(promise, []);
  74. }).toThrow(TypeError);
  75. expect(() => {
  76. function promise(executor) {
  77. executor(undefined, fn);
  78. executor(fn, fn);
  79. }
  80. Promise.allSettled.call(promise, []);
  81. }).toThrow(TypeError);
  82. });
  83. test("promise without resolve method", () => {
  84. expect(() => {
  85. function promise(executor) {}
  86. Promise.allSettled.call(promise, []);
  87. }).toThrow(TypeError);
  88. });
  89. test("no parameters", () => {
  90. let rejectionReason = null;
  91. Promise.allSettled().catch(reason => {
  92. rejectionReason = reason;
  93. });
  94. runQueuedPromiseJobs();
  95. expect(rejectionReason).toBeInstanceOf(TypeError);
  96. });
  97. test("non-iterable", () => {
  98. let rejectionReason = null;
  99. Promise.allSettled(1).catch(reason => {
  100. rejectionReason = reason;
  101. });
  102. runQueuedPromiseJobs();
  103. expect(rejectionReason).toBeInstanceOf(TypeError);
  104. });
  105. });