summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/observable/tentative/observable-some.any.js
blob: b692610df329c601d38b3407eac1b242b62f9b05 (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
promise_test(async () => {
  let inactiveAfterFirstGood = true;

  const source = new Observable(subscriber => {
    subscriber.next("good");
    inactiveAfterFirstGood = !subscriber.active;
    subscriber.next("good");
    subscriber.next("good");
    subscriber.complete();
  });

  const result = await source.some((value) => value === "good");

  assert_true(result, "Promise resolves with true if any value passes the predicate");

  assert_true(inactiveAfterFirstGood,
      "subscriber is inactive after the first value that passes the " +
      "predicate, because the source was unsubscribed from");
}, "some(): subscriber is inactive after the first value that passes the predicate, because the source was unsubscribed from");

promise_test(async () => {
  const source = new Observable(subscriber => {
    subscriber.next("bad");
    subscriber.next("bad");
    subscriber.next("bad");
    subscriber.complete();
  });

  const result = await source.some((value) => value === "good");

  assert_false(result, "some(): Promise resolves with false if no value passes the predicate");
});

promise_test(async () => {
  const source = new Observable(subscriber => {
    subscriber.next("bad");
    subscriber.next("bad");
    subscriber.next("good");
    subscriber.complete();
  });

  const result = await source.some((value) => value === "good");

  assert_true(result, "some(): Promise resolves with true if any value passes the predicate");
});

promise_test(async t => {
  const source = new Observable(subscriber => {
    subscriber.next("not used");
  });

  const error = new Error("thrown from predicate");
  promise_rejects_exactly(t, error, source.some(() => {throw error}),
      "The returned promise rejects with an error if the predicate errors");
}, "some(): The returned promise rejects with an error if the predicate errors");

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.some(() => true),
      "The returned promise rejects with an error if the source observable errors");
}, "some(): The returned promise rejects with an error if the source observable errors");

promise_test(async () => {
  const source = new Observable(subscriber => {
    subscriber.complete();
  });

  const result = await source.some(() => true);

  assert_false(result,
      "The returned promise resolves as false if the source observable " +
      "completes without emitting a value");
}, "some(): The returned promise resolves as false if the source observable " +
   "completes without emitting a value");

promise_test(async t => {
  let teardownCalled = false;
  const source = new Observable(subscriber => {
    subscriber.addTeardown(() => {
      teardownCalled = true;
    });
  });

  const controller = new AbortController();
  const promise = source.some(() => true, { signal: controller.signal });

  controller.abort();

  promise_rejects_dom(t, 'AbortError', promise);
  assert_true(teardownCalled,
      "The teardown function is called when the signal is aborted");
}, "some(): The return promise rejects with a DOMException if the signal is aborted");