tor-browser

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

observable-first.any.js (2848B)


      1 promise_test(async () => {
      2  const results = [];
      3 
      4  const source = new Observable(subscriber => {
      5    subscriber.addTeardown(() => results.push('teardown'));
      6    subscriber.next(1);
      7    results.push(subscriber.active ? 'active' : 'inactive');
      8    results.push(subscriber.signal.aborted ? 'aborted' : 'not aborted')
      9 
     10    // Ignored.
     11    subscriber.next(2);
     12    subscriber.complete();
     13  });
     14 
     15  const value = await source.first();
     16 
     17  assert_array_equals(results, ['teardown', 'inactive', 'aborted']);
     18  assert_equals(value, 1,
     19      "Promise resolves with the first value from the source Observable");
     20 }, "first(): Promise resolves with the first value from the source Observable");
     21 
     22 promise_test(async (t) => {
     23  const error = new Error("error from source");
     24  const source = new Observable(subscriber => {
     25    subscriber.error(error);
     26  });
     27 
     28  return promise_rejects_exactly(t, error, source.first(), "Promise rejects with source Observable error");
     29 }, "first(): Promise rejects with the error emitted from the source Observable");
     30 
     31 promise_test(async (t) => {
     32  const source = new Observable(subscriber => {
     33    subscriber.complete();
     34  });
     35 
     36  return promise_rejects_js(t, RangeError, source.first(), "Upon complete(), first() Promise rejects with RangeError");
     37 }, "first(): Promise rejects with RangeError when source Observable " +
     38   "completes without emitting any values");
     39 
     40 promise_test(async (t) => {
     41  const source = new Observable(subscriber => {});
     42 
     43  const controller = new AbortController();
     44  const promise = source.first({ signal: controller.signal });
     45 
     46  controller.abort();
     47 
     48  return promise_rejects_dom(t, "AbortError", promise, "Promise rejects with a DOMException for abortion");
     49 }, "first(): Aborting a signal rejects the Promise with an AbortError DOMException");
     50 
     51 promise_test(async () => {
     52  const results = [];
     53 
     54  const source = new Observable(subscriber => {
     55    results.push("source subscribe");
     56    subscriber.addTeardown(() => results.push("source teardown"));
     57    subscriber.signal.addEventListener("abort", () => results.push("source abort"));
     58    results.push("before source next 1");
     59    subscriber.next(1);
     60    results.push("after source next 1");
     61  });
     62 
     63  results.push("calling first");
     64  const promise = source.first();
     65 
     66  assert_array_equals(results, [
     67    "calling first",
     68    "source subscribe",
     69    "before source next 1",
     70    "source abort",
     71    "source teardown",
     72    "after source next 1"
     73  ], "Array values after first() is called");
     74 
     75  const firstValue = await promise;
     76  results.push(`first resolved with: ${firstValue}`);
     77 
     78  assert_array_equals(results, [
     79    "calling first",
     80    "source subscribe",
     81    "before source next 1",
     82    "source abort",
     83    "source teardown",
     84    "after source next 1",
     85    "first resolved with: 1",
     86  ], "Array values after Promise is awaited");
     87 }, "first(): Lifecycle");