| 1 |
library(testthat) |
| 2 |
|
| 3 |
# 2.3. The Promise Resolution Procedure |
| 4 |
|
| 5 |
describe("2.3.1. If promise and x refer to the same object, reject promise with a TypeError as the reason.", { |
| 6 |
it(" ", { |
| 7 |
p <- ext_promise() |
| 8 |
p$resolve(p$promise) |
| 9 |
expect_error(extract(p$promise), "^Chaining cycle detected for promise$") |
| 10 |
}) |
| 11 |
}) |
| 12 |
|
| 13 |
describe("2.3.2. If x is a promise, adopt its state [3.4]:", { |
| 14 |
it("2.3.2.1. If x is pending, promise must remain pending until x is fulfilled or rejected.", { |
| 15 |
p <- ext_promise() |
| 16 |
x <- ext_promise() |
| 17 |
p$resolve(x$promise) |
| 18 |
wait_for_it() |
| 19 |
expect_identical(x$status(), "pending") |
| 20 |
expect_identical(p$status(), "pending") |
| 21 |
}) |
| 22 |
it("2.3.2.2. If/when x is fulfilled, fulfill promise with the same value.", { |
| 23 |
p <- ext_promise() |
| 24 |
x <- ext_promise() |
| 25 |
p$resolve(x$promise) |
| 26 |
wait_for_it() |
| 27 |
expect_identical(x$status(), "pending") |
| 28 |
expect_identical(p$status(), "pending") |
| 29 |
x$resolve(100) |
| 30 |
expect_identical(extract(x$promise), 100) |
| 31 |
expect_identical(extract(p$promise), 100) |
| 32 |
}) |
| 33 |
it("2.3.2.3. If/when x is rejected, reject promise with the same reason.", { |
| 34 |
p <- ext_promise() |
| 35 |
x <- ext_promise() |
| 36 |
p$resolve(x$promise) |
| 37 |
wait_for_it() |
| 38 |
expect_identical(x$status(), "pending") |
| 39 |
expect_identical(p$status(), "pending") |
| 40 |
squelch_unhandled_promise_error(p$promise) |
| 41 |
x$reject("bad") |
| 42 |
expect_error(extract(x$promise), "^bad$") |
| 43 |
expect_error(extract(p$promise), "^bad$") |
| 44 |
}) |
| 45 |
}) |
| 46 |
# 2.3.3. Otherwise, if x is an object or function, |
| 47 |
# 2.3.3.1. Let then be x.then. [3.5] |
| 48 |
# 2.3.3.2. If retrieving the property x.then results in a thrown exception e, reject promise with e as the reason. |
| 49 |
# 2.3.3.3. If then is a function, call it with x as this, first argument resolvePromise, and second argument rejectPromise, where: |
| 50 |
# 2.3.3.3.1. If/when resolvePromise is called with a value y, run [[Resolve]](promise, y). |
| 51 |
# 2.3.3.3.2. If/when rejectPromise is called with a reason r, reject promise with r. |
| 52 |
# 2.3.3.3.3. If both resolvePromise and rejectPromise are called, or multiple calls to the same argument are made, the first call takes precedence, and any further calls are ignored. |
| 53 |
# 2.3.3.3.4. If calling then throws an exception e, |
| 54 |
# 2.3.3.3.4.1. If resolvePromise or rejectPromise have been called, ignore it. |
| 55 |
# 2.3.3.3.4.2. Otherwise, reject promise with e as the reason. |
| 56 |
# 2.3.3.4. If then is not a function, fulfill promise with x. |
| 57 |
describe("2.3.4. If x is not an object or function, fulfill promise with x.", { |
| 58 |
it(" ", { |
| 59 |
p <- ext_promise() |
| 60 |
p$resolve(10) |
| 61 |
expect_identical(extract(p$promise), 10) |
| 62 |
}) |
| 63 |
}) |