1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
promise_test(async () => {
const source = new Observable(subscriber => {
subscriber.next("good");
subscriber.next("good");
subscriber.next("good");
subscriber.complete();
});
const result = await source.every((value) => value === "good");
assert_true(result, "Promise resolves with true if all values pass the predicate");
}, "every(): Promise resolves to true if all values pass the predicate");
promise_test(async () => {
const source = new Observable(subscriber => {
subscriber.next("good");
subscriber.next("good");
subscriber.next("bad");
subscriber.complete();
});
const result = await source.every((value) => value === "good");
assert_false(result, "Promise resolves with false if any value fails the predicate");
}, "every(): Promise resolves to false if any value fails the predicate");
promise_test(async () => {
let tornDown = false;
let subscriberActiveAfterFailingPredicate = true;
const source = new Observable(subscriber => {
subscriber.addTeardown(() => tornDown = true);
subscriber.next("good");
subscriber.next("good");
subscriber.next("bad");
subscriberActiveAfterFailingPredicate = subscriber.active;
subscriber.next("good");
subscriber.complete();
});
const result = await source.every((value) => value === "good");
assert_false(result, "Promise resolves with false if any value fails the predicate");
assert_false(subscriberActiveAfterFailingPredicate,
"Subscriber becomes inactive because every() unsubscribed");
}, "every(): Abort the subscription to the source if the predicate does not pass");
promise_test(async () => {
const logs = [];
const source = createTestSubject({
onSubscribe: () => logs.push("subscribed to source"),
onTeardown: () => logs.push("teardown"),
});
const resultPromise = source.every((value, index) => {
logs.push(`Predicate called with ${value}, ${index}`);
return true;
});
let promiseResolved = false;
resultPromise.then(() => promiseResolved = true);
assert_array_equals(logs, ["subscribed to source"],
"calling every() subscribes to the source immediately");
source.next("a");
assert_array_equals(logs, [
"subscribed to source",
"Predicate called with a, 0"
], "Predicate called with the value and the index");
source.next("b");
assert_array_equals(logs, [
"subscribed to source",
"Predicate called with a, 0",
"Predicate called with b, 1",
], "Predicate called with the value and the index");
// wait a tick, just to prove that you have to wait for complete to be called.
await Promise.resolve();
assert_false(promiseResolved,
"Promise should not resolve until after the source completes");
source.complete();
assert_array_equals(logs, [
"subscribed to source",
"Predicate called with a, 0",
"Predicate called with b, 1",
"teardown",
], "Teardown function called immediately after the source completes");
const result = await resultPromise;
assert_true(result,
"Promise resolves with true if all values pass the predicate");
}, "every(): Lifecycle checks when all values pass the predicate");
promise_test(async () => {
const logs = [];
const source = createTestSubject({
onSubscribe: () => logs.push("subscribed to source"),
onTeardown: () => logs.push("teardown"),
});
const resultPromise = source.every((value, index) => {
logs.push(`Predicate called with ${value}, ${index}`);
return value === "good";
});
let promiseResolved = false;
resultPromise.then(() => promiseResolved = true);
assert_array_equals(logs, ["subscribed to source"],
"calling every() subscribes to the source immediately");
source.next("good");
source.next("good");
assert_array_equals(logs, [
"subscribed to source",
"Predicate called with good, 0",
"Predicate called with good, 1",
], "Predicate called with the value and the index");
assert_false(promiseResolved, "Promise should not resolve until after the predicate fails");
source.next("bad");
assert_array_equals(logs, [
"subscribed to source",
"Predicate called with good, 0",
"Predicate called with good, 1",
"Predicate called with bad, 2",
"teardown",
], "Predicate called with the value and the index, failing predicate immediately aborts subscription to source");
const result = await resultPromise;
assert_false(result, "Promise resolves with false if any value fails the predicate");
}, "every(): Lifecycle checks when any value fails the predicate");
promise_test(async () => {
const source = new Observable(subscriber => {
subscriber.complete();
});
const result = await source.every(() => true);
assert_true(result,
"Promise resolves with true if the observable completes without " +
"emitting a value");
}, "every(): Resolves with true if the observable completes without " +
"emitting a value");
promise_test(async t => {
const error = new Error("error from source");
const source = new Observable(subscriber => {
subscriber.error(error);
});
promise_rejects_exactly(t, error, source.every(() => true),
"Promise rejects with the error emitted from the source observable");
}, "every(): Rejects with any error emitted from the source observable");
promise_test(async t => {
const source = new Observable(subscriber => {
subscriber.next(1);
subscriber.next(2);
subscriber.next(3);
subscriber.complete();
});
const error = new Error("bad value");
const promise = source.every(value => {
if (value <= 2) return true;
throw error;
});
promise_rejects_exactly(t, error, promise, "Promise rejects with the " +
"error thrown from the predicate");
}, "every(): Rejects with any error thrown from the predicate");
promise_test(async () => {
const indices = [];
const source = new Observable(subscriber => {
subscriber.next("a");
subscriber.next("b");
subscriber.next("c");
subscriber.complete();
});
const value = await source.every((value, index) => {
indices.push(index);
return true;
});
assert_array_equals(indices, [0, 1, 2]);
assert_true(value,
"Promise resolves with true if all values pass the predicate");
}, "every(): Index is passed into the predicate");
promise_test(async t => {
const source = new Observable(subscriber => {});
const controller = new AbortController();
const promise = source.every(() => true, { signal: controller.signal });
controller.abort();
promise_rejects_dom(t, 'AbortError', promise, "Promise rejects with a " +
"DOMException if the source Observable is aborted");
}, "every(): Rejects with a DOMException if the source Observable is aborted");
function createTestSubject(options) {
const onTeardown = options?.onTeardown;
const subscribers = new Set();
const subject = new Observable(subscriber => {
options?.onSubscribe?.();
subscribers.add(subscriber);
subscriber.addTeardown(() => subscribers.delete(subscriber));
if (onTeardown) {
subscriber.addTeardown(onTeardown);
}
});
subject.next = (value) => {
for (const subscriber of Array.from(subscribers)) {
subscriber.next(value);
}
};
subject.error = (error) => {
for (const subscriber of Array.from(subscribers)) {
subscriber.error(error);
}
};
subject.complete = () => {
for (const subscriber of Array.from(subscribers)) {
subscriber.complete();
}
};
subject.subscriberCount = () => {
return subscribers.size;
};
return subject;
}
|