summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/observable/tentative/observable-drop.any.js
blob: 4b15fedfd33b36090929dd6704285fc3fc6d90ed (plain)
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
test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.next(4);
    subscriber.complete();
  });

  const results = [];

  source.drop(2).subscribe({
    next: v => results.push(v),
    error: e => results.push(e),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, [3, 4, "complete"]);
}, "drop(): Observable should skip the first n values from the source " +
   "observable, then pass through the rest of the values and completion");

test(() => {
  const error = new Error('source error');
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.next(4);
    subscriber.error(error);
  });

  const results = [];

  source.drop(2).subscribe({
    next: v => results.push(v),
    error: e => results.push(e),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, [3, 4, error]);
}, "drop(): Observable passes through errors from source Observable");

test(() => {
  const error = new Error('source error');
  const source = new Observable(subscriber => {
    subscriber.error(error);
    subscriber.next(1);
  });

  const results = [];

  source.drop(2).subscribe({
    next: v => results.push(v),
    error: e => results.push(e),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, [error]);
}, "drop(): Observable passes through errors from source observable even " +
   "before drop count is met");

test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.complete();
  });

  const results = [];

  source.drop(2).subscribe({
    next: v => results.push(v),
    error: e => results.push(e),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, ["complete"]);
}, "drop(): Observable passes through completions from source observable even " +
    "before drop count is met");

test(() => {
  let sourceTeardownCalled = false;
  const source = new Observable(subscriber => {
    subscriber.addTeardown(() => sourceTeardownCalled = true);
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.next(4);
    subscriber.next(5);
    subscriber.complete();
  });

  const results = [];

  const controller = new AbortController();

  source.drop(2).subscribe({
    next: v => {
      results.push(v);
      if (v === 3) {
        controller.abort();
      }
    },
    error: (e) => results.push(e),
    complete: () => results.push("complete"),
  }, {signal: controller.signal});

  assert_true(sourceTeardownCalled,
      "Aborting outer observable unsubscribes the source observable");
  assert_array_equals(results, [3]);
}, "drop(): Unsubscribing from the Observable returned by drop() also " +
    "unsubscribes from the source Observable");

test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.complete();
  });

  const results = [];

  source.drop(0).subscribe({
    next: v => results.push(v),
    error: e => results.push(e),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, [1, 2, 3, "complete"],
      "Source Observable is mirrored");
}, "drop(): A drop amount of 0 simply mirrors the source Observable");

test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.complete();
  });

  const results = [];

  // Passing `-1` here is subject to the Web IDL integer conversion semantics,
  // which converts the drop amount to the maximum of `18446744073709551615`.
  source.drop(-1).subscribe({
    next: v => results.push(v),
    error: e => results.push(e),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, ["complete"], "Source Observable is mirrored");
}, "drop(): Passing negative value wraps to maximum value ");