tor-browser

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

observable-drop.any.js (4062B)


      1 test(() => {
      2  const source = new Observable(subscriber => {
      3    subscriber.next(1);
      4    subscriber.next(2);
      5    subscriber.next(3);
      6    subscriber.next(4);
      7    subscriber.complete();
      8  });
      9 
     10  const results = [];
     11 
     12  source.drop(2).subscribe({
     13    next: v => results.push(v),
     14    error: e => results.push(e),
     15    complete: () => results.push("complete"),
     16  });
     17 
     18  assert_array_equals(results, [3, 4, "complete"]);
     19 }, "drop(): Observable should skip the first n values from the source " +
     20   "observable, then pass through the rest of the values and completion");
     21 
     22 test(() => {
     23  const error = new Error('source error');
     24  const source = new Observable(subscriber => {
     25    subscriber.next(1);
     26    subscriber.next(2);
     27    subscriber.next(3);
     28    subscriber.next(4);
     29    subscriber.error(error);
     30  });
     31 
     32  const results = [];
     33 
     34  source.drop(2).subscribe({
     35    next: v => results.push(v),
     36    error: e => results.push(e),
     37    complete: () => results.push("complete"),
     38  });
     39 
     40  assert_array_equals(results, [3, 4, error]);
     41 }, "drop(): Observable passes through errors from source Observable");
     42 
     43 test(() => {
     44  const error = new Error('source error');
     45  const source = new Observable(subscriber => {
     46    subscriber.error(error);
     47    subscriber.next(1);
     48  });
     49 
     50  const results = [];
     51 
     52  source.drop(2).subscribe({
     53    next: v => results.push(v),
     54    error: e => results.push(e),
     55    complete: () => results.push("complete"),
     56  });
     57 
     58  assert_array_equals(results, [error]);
     59 }, "drop(): Observable passes through errors from source observable even " +
     60   "before drop count is met");
     61 
     62 test(() => {
     63  const source = new Observable(subscriber => {
     64    subscriber.next(1);
     65    subscriber.complete();
     66  });
     67 
     68  const results = [];
     69 
     70  source.drop(2).subscribe({
     71    next: v => results.push(v),
     72    error: e => results.push(e),
     73    complete: () => results.push("complete"),
     74  });
     75 
     76  assert_array_equals(results, ["complete"]);
     77 }, "drop(): Observable passes through completions from source observable even " +
     78    "before drop count is met");
     79 
     80 test(() => {
     81  let sourceTeardownCalled = false;
     82  const source = new Observable(subscriber => {
     83    subscriber.addTeardown(() => sourceTeardownCalled = true);
     84    subscriber.next(1);
     85    subscriber.next(2);
     86    subscriber.next(3);
     87    subscriber.next(4);
     88    subscriber.next(5);
     89    subscriber.complete();
     90  });
     91 
     92  const results = [];
     93 
     94  const controller = new AbortController();
     95 
     96  source.drop(2).subscribe({
     97    next: v => {
     98      results.push(v);
     99      if (v === 3) {
    100        controller.abort();
    101      }
    102    },
    103    error: (e) => results.push(e),
    104    complete: () => results.push("complete"),
    105  }, {signal: controller.signal});
    106 
    107  assert_true(sourceTeardownCalled,
    108      "Aborting outer observable unsubscribes the source observable");
    109  assert_array_equals(results, [3]);
    110 }, "drop(): Unsubscribing from the Observable returned by drop() also " +
    111    "unsubscribes from the source Observable");
    112 
    113 test(() => {
    114  const source = new Observable(subscriber => {
    115    subscriber.next(1);
    116    subscriber.next(2);
    117    subscriber.next(3);
    118    subscriber.complete();
    119  });
    120 
    121  const results = [];
    122 
    123  source.drop(0).subscribe({
    124    next: v => results.push(v),
    125    error: e => results.push(e),
    126    complete: () => results.push("complete"),
    127  });
    128 
    129  assert_array_equals(results, [1, 2, 3, "complete"],
    130      "Source Observable is mirrored");
    131 }, "drop(): A drop amount of 0 simply mirrors the source Observable");
    132 
    133 test(() => {
    134  const source = new Observable(subscriber => {
    135    subscriber.next(1);
    136    subscriber.next(2);
    137    subscriber.next(3);
    138    subscriber.complete();
    139  });
    140 
    141  const results = [];
    142 
    143  // Passing `-1` here is subject to the Web IDL integer conversion semantics,
    144  // which converts the drop amount to the maximum of `18446744073709551615`.
    145  source.drop(-1).subscribe({
    146    next: v => results.push(v),
    147    error: e => results.push(e),
    148    complete: () => results.push("complete"),
    149  });
    150 
    151  assert_array_equals(results, ["complete"], "Source Observable is mirrored");
    152 }, "drop(): Passing negative value wraps to maximum value ");