Skip to content

Commit

Permalink
Add tests for self.queueMicrotask()
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Jun 23, 2017
1 parent b994916 commit d2d10d0
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

setup({
allow_uncaught_exception: true
});

async_test(t => {
const error = new Error("boo");
self.addEventListener("error", ev => {
assert_equals(ev.error, error);
t.done();
});

queueMicrotask(() => { throw error; });
}, "It rethrows exceptions");
41 changes: 41 additions & 0 deletions html/webappapis/microtask-queuing/queue-microtask.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";

test(() => {
assert_equals(typeof queueMicrotask, "function");
}, "It exists and is a function");

test(() => {
assert_throws(new TypeError(), () => queueMicrotask(), "no argument");
assert_throws(new TypeError(), () => queueMicrotask(undefined), "undefined");
assert_throws(new TypeError(), () => queueMicrotask(null), "null");
assert_throws(new TypeError(), () => queueMicrotask(0), "0");
assert_throws(new TypeError(), () => queueMicrotask({ handleEvent() { } }), "an event handler object");
assert_throws(new TypeError(), () => queueMicrotask("window.x = 5;"), "a string");
}, "It throws when given non-functions");

async_test(t => {
let called = false;
queueMicrotask(() => {
called = true;
t.done();
});
assert_false(called);
}, "It calls the callback asynchronously");

async_test(t => {
queueMicrotask(function () {
assert_array_equals(arguments, []);
t.done();
}, "x", "y");
}, "It does not pass any arguments");

async_test(t => {
const happenings = [];
Promise.resolve().then(() => happenings.push("a"));
queueMicrotask(() => happenings.push("b"));
Promise.reject().catch(() => happenings.push("c"));
queueMicrotask(() => {
assert_array_equals(happenings, ["a", "b", "c"]);
t.done();
});
}, "It interleaves with promises as expected");
45 changes: 45 additions & 0 deletions html/webappapis/microtask-queuing/queue-microtask.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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");

0 comments on commit d2d10d0

Please sign in to comment.