summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/webappapis/microtask-queuing/queue-microtask.window.js
blob: 78cdcfc5d989a81882a10b7d70050408aadfe924 (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
"use strict";

// This does not work as you expect because mutation observer compound microtasks are confusing.
// Basically you can only use it once per test.
function queueMicrotaskViaMO(cb) {
  const observer = new MutationObserver(cb);
  const node = document.createTextNode("");
  observer.observe(node, { characterData: true });
  node.data = "foo";
}

// Need to use promise_test to get sequential ordering; otherwise the global mutation observer
// compound microtask business screws us over.

promise_test(() => {
  return new Promise(resolve => {
    const happenings = [];

    queueMicrotaskViaMO(() => happenings.push("x"));
    queueMicrotask(() => happenings.push("a"));

    queueMicrotask(() => {
      assert_array_equals(happenings, ["x", "a"]);
      resolve();
    });
  });
}, "It interleaves with MutationObservers as expected");

promise_test(() => {
  return new Promise(resolve => {
    const happenings = [];

    queueMicrotask(() => happenings.push("a"));
    Promise.reject().catch(() => happenings.push("x"));
    queueMicrotaskViaMO(() => happenings.push(1));
    Promise.resolve().then(() => happenings.push("y"));
    queueMicrotask(() => happenings.push("b"));
    queueMicrotask(() => happenings.push("c"));

    queueMicrotask(() => {
      assert_array_equals(happenings, ["a", "x", 1, "y", "b", "c"]);
      resolve();
    });
  });
}, "It interleaves with MutationObservers and promises together as expected");