tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

observable-find.any.js (2876B)


      1 promise_test(async () => {
      2  let inactiveAfterB = false;
      3  const source = new Observable(subscriber => {
      4    subscriber.next("a");
      5    subscriber.next("b");
      6    inactiveAfterB = !subscriber.active;
      7    subscriber.next("c");
      8    subscriber.complete();
      9  });
     10 
     11  const value = await source.find((value) => value === "b");
     12 
     13  assert_equals(value, "b", "Promise resolves with the first value that passes the predicate");
     14 
     15  assert_true(inactiveAfterB, "subscriber is inactive after the first value that passes the predicate");
     16 }, "find(): Promise resolves with the first value that passes the predicate");
     17 
     18 promise_test(async () => {
     19  const source = new Observable(subscriber => {
     20    subscriber.next("a");
     21    subscriber.next("b");
     22    subscriber.next("c");
     23    subscriber.complete();
     24  });
     25 
     26  const value = await source.find(() => false);
     27 
     28  assert_equals(value, undefined, "Promise resolves with undefined if no value passes the predicate");
     29 }, "find(): Promise resolves with undefined if no value passes the predicate");
     30 
     31 promise_test(async t => {
     32  const error = new Error("error from source");
     33  const source = new Observable(subscriber => {
     34    subscriber.error(error);
     35  });
     36 
     37  promise_rejects_exactly(t, error, source.find(() => true), "Promise " +
     38      "rejects with the error emitted from the source Observable");
     39 }, "find(): Promise rejects with the error emitted from the source Observable");
     40 
     41 promise_test(async t => {
     42  const source = new Observable(subscriber => {
     43    subscriber.next("ignored");
     44  });
     45 
     46  const error = new Error("thrown from predicate");
     47  promise_rejects_exactly(t, error, source.find(() => {throw error}),
     48      "Promise rejects with any error thrown from the predicate");
     49 }, "find(): Promise rejects with any error thrown from the predicate");
     50 
     51 promise_test(async () => {
     52  let indices = [];
     53 
     54  const source = new Observable(subscriber => {
     55    subscriber.next("a");
     56    subscriber.next("b");
     57    subscriber.next("c");
     58    subscriber.complete();
     59  });
     60 
     61  const value = await source.find((value, index) => {
     62    indices.push(index);
     63    return false;
     64  });
     65 
     66  assert_equals(value, undefined, "Promise resolves with undefined if no value passes the predicate");
     67 
     68  assert_array_equals(indices, [0, 1, 2], "find(): Passes the index of the value to the predicate");
     69 }, "find(): Passes the index of the value to the predicate");
     70 
     71 promise_test(async t => {
     72  const controller = new AbortController();
     73  const source = new Observable(subscriber => {
     74    subscriber.next("a");
     75    subscriber.next("b");
     76    subscriber.next("c");
     77    subscriber.complete();
     78  });
     79 
     80  controller.abort();
     81  const promise = source.find(() => true, { signal: controller.signal });
     82 
     83  promise_rejects_dom(t, 'AbortError', promise, "Promise rejects with " +
     84      "DOMException when the signal is aborted");
     85 }, "find(): Rejects with AbortError when the signal is aborted");