| 1 |
test_that("complex promise chains with mixed success/failure", { |
| 2 |
counter <- 0 |
| 3 |
promise_resolve(1) |> |
| 4 |
then(\(x) { |
| 5 |
counter <<- counter + 1 |
| 6 |
stop("error1") |
| 7 |
}) |> |
| 8 |
catch(\(err) { |
| 9 |
counter <<- counter + 1 |
| 10 |
"recovered" |
| 11 |
}) |> |
| 12 |
then(\(x) { |
| 13 |
counter <<- counter + 1 |
| 14 |
x |
| 15 |
}) |> |
| 16 |
catch(\(err) { |
| 17 |
counter <<- counter + 1 |
| 18 |
"should not reach" |
| 19 |
}) |> |
| 20 |
then(\(x) { |
| 21 |
counter <<- counter + 1 |
| 22 |
expect_equal(x, "recovered") |
| 23 |
}) |> |
| 24 |
wait_for_it() |
| 25 |
expect_equal(counter, 4) |
| 26 |
}) |
| 27 |
|
| 28 |
test_that("then() handles NULL callbacks correctly", { |
| 29 |
# NULL onFulfilled should pass through value |
| 30 |
expect_assertions(n = 1, { |
| 31 |
promise_resolve(42) |> |
| 32 |
then(NULL, \(err) "error") |> |
| 33 |
extract() |> |
| 34 |
expect_equal(42) |
| 35 |
}) |
| 36 |
}) |
| 37 |
|
| 38 |
test_that("catch() handles NULL callbacks correctly", { |
| 39 |
# NULL onRejected should pass through error |
| 40 |
expect_assertions(n = 1, { |
| 41 |
promise_resolve(stop("boom")) |> |
| 42 |
then(\(x) x, NULL) |> |
| 43 |
catch(\(err) expect_match(err$message, "boom")) |> |
| 44 |
wait_for_it() |
| 45 |
}) |
| 46 |
}) |
| 47 |
|
| 48 |
test_that("then() validates arguments properly", { |
| 49 |
expect_error( |
| 50 |
then(promise_resolve(1), onFulfilled = "not a function"), |
| 51 |
"function" |
| 52 |
) |
| 53 |
|
| 54 |
expect_error( |
| 55 |
catch(promise_resolve(1), "not a function"), |
| 56 |
"function" |
| 57 |
) |
| 58 |
}) |
| 59 |
|
| 60 |
test_that("tee types are handled correctly", { |
| 61 |
expect_silent({ |
| 62 |
p <- promise_resolve(1) |> then(tee = TRUE) |
| 63 |
p2 <- promise_resolve(1) |> catch(\(err) {}, tee = TRUE) |
| 64 |
}) |
| 65 |
|
| 66 |
expect_snapshot( |
| 67 |
then(promise_resolve(1), tee = 4), |
| 68 |
error = TRUE |
| 69 |
) |
| 70 |
|
| 71 |
expect_snapshot( |
| 72 |
promise_resolve(1) |> catch(\(err) {}, tee = "4"), |
| 73 |
error = TRUE |
| 74 |
) |
| 75 |
}) |
| 76 |
|
| 77 |
test_that("then(tee = FALSE) functions are normalized", { |
| 78 |
counter <- 0 |
| 79 |
promise_resolve(1) |> |
| 80 |
then( |
| 81 |
\() { |
| 82 |
counter <<- counter + 1 |
| 83 |
2 |
| 84 |
} |
| 85 |
) |> |
| 86 |
then( |
| 87 |
\(value) { |
| 88 |
expect_equal(value, 2) |
| 89 |
counter <<- counter + 1 |
| 90 |
invisible(3) |
| 91 |
} |
| 92 |
) |> |
| 93 |
then( |
| 94 |
\(value, .visible) { |
| 95 |
expect_equal(value, 3) |
| 96 |
expect_false(.visible) |
| 97 |
counter <<- counter + 1 |
| 98 |
stop("boom") |
| 99 |
} |
| 100 |
) |> |
| 101 |
then( |
| 102 |
onRejected = \() { |
| 103 |
counter <<- counter + 1 |
| 104 |
stop("blast") |
| 105 |
} |
| 106 |
) |> |
| 107 |
then( |
| 108 |
onRejected = \(err) { |
| 109 |
expect_true(grepl("blast", err$message)) |
| 110 |
counter <<- counter + 1 |
| 111 |
} |
| 112 |
) |
| 113 |
wait_for_it() |
| 114 |
expect_equal(counter, 5) |
| 115 |
}) |
| 116 |
test_that("catch(tee = FALSE) functions are normalized", { |
| 117 |
counter <- 0 |
| 118 |
promise_resolve(stop("boom")) |> |
| 119 |
catch( |
| 120 |
\() { |
| 121 |
counter <<- counter + 1 |
| 122 |
stop("blast") |
| 123 |
} |
| 124 |
) |> |
| 125 |
catch( |
| 126 |
\(err) { |
| 127 |
expect_true(grepl("blast", err$message)) |
| 128 |
counter <<- counter + 1 |
| 129 |
} |
| 130 |
) |> |
| 131 |
catch(\(err) {}) |
| 132 |
wait_for_it() |
| 133 |
expect_equal(counter, 2) |
| 134 |
}) |
| 135 |
|
| 136 |
test_that("then(tee=TRUE) functions are normalized", { |
| 137 |
then_counter <- 0 |
| 138 |
error_counter <- 0 |
| 139 |
promise_resolve(1) |> |
| 140 |
then( |
| 141 |
tee = TRUE, |
| 142 |
\() { |
| 143 |
then_counter <<- then_counter + 1 |
| 144 |
} |
| 145 |
) |> |
| 146 |
then( |
| 147 |
tee = TRUE, |
| 148 |
\(value) { |
| 149 |
expect_equal(value, 1) |
| 150 |
then_counter <<- then_counter + 1 |
| 151 |
3 |
| 152 |
} |
| 153 |
) |> |
| 154 |
then( |
| 155 |
tee = TRUE, |
| 156 |
\(value, .visible) { |
| 157 |
expect_equal(value, 1) |
| 158 |
expect_true(.visible) |
| 159 |
then_counter <<- then_counter + 1 |
| 160 |
|
| 161 |
stop("boom") |
| 162 |
} |
| 163 |
) |> |
| 164 |
then( |
| 165 |
tee = TRUE, |
| 166 |
onRejected = \() { |
| 167 |
error_counter <<- error_counter + 1 |
| 168 |
} |
| 169 |
) |> |
| 170 |
then( |
| 171 |
tee = TRUE, |
| 172 |
onRejected = \(err) { |
| 173 |
expect_true(grepl("boom", err$message)) |
| 174 |
error_counter <<- error_counter + 1 |
| 175 |
} |
| 176 |
) |> |
| 177 |
then(onRejected = \(err) { |
| 178 |
expect_true(grepl("boom", err$message)) |
| 179 |
error_counter <<- error_counter + 1 |
| 180 |
}) |> |
| 181 |
wait_for_it() |
| 182 |
|
| 183 |
expect_equal(then_counter, 3) |
| 184 |
expect_equal(error_counter, 3) |
| 185 |
}) |
| 186 |
|
| 187 |
test_that("catch(tee=TRUE) functions are normalized", { |
| 188 |
error_counter <- 0 |
| 189 |
promise_resolve(stop("boom")) |> |
| 190 |
catch( |
| 191 |
tee = TRUE, |
| 192 |
\() { |
| 193 |
error_counter <<- error_counter + 1 |
| 194 |
} |
| 195 |
) |> |
| 196 |
catch( |
| 197 |
tee = TRUE, |
| 198 |
\(err) { |
| 199 |
expect_true(grepl("boom", err$message)) |
| 200 |
error_counter <<- error_counter + 1 |
| 201 |
} |
| 202 |
) |> |
| 203 |
catch(\() { |
| 204 |
error_counter <<- error_counter + 1 |
| 205 |
}) |> |
| 206 |
wait_for_it() |
| 207 |
expect_equal(error_counter, 3) |
| 208 |
}) |