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 153 154 155 156 157 158 159 160 161 162 163
|
<!doctype html>
<meta charset="utf-8" />
<meta name="author" title="Keith Cirkel" href="mailto:wpt@keithcirkel.co.uk" />
<link rel="help" href="https://open-ui.org/components/invokers.explainer/" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/invoker-utils.js"></script>
<div id="div"></div>
<button id="button"></button>
<script>
test(function () {
const event = new CommandEvent("test");
assert_equals(event.command, "");
assert_readonly(event, "command", "readonly attribute value");
}, "command is a readonly defaulting to ''");
test(function () {
const event = new CommandEvent("test");
assert_equals(event.source, null);
assert_readonly(event, "source", "readonly attribute value");
}, "source is readonly defaulting to null");
test(function () {
const event = new CommandEvent("test", { command: "sAmPle" });
assert_equals(event.command, "sAmPle");
}, "command reflects initialized attribute");
test(function () {
const event = new CommandEvent("test", { command: undefined });
assert_equals(event.command, "");
}, "command set to undefined");
test(function () {
const event = new CommandEvent("test", { command: null });
assert_equals(event.command, "null");
}, "command set to null");
test(function () {
const event = new CommandEvent("test", { command: false });
assert_equals(event.command, "false");
}, "command set to false");
test(function () {
const event = new CommandEvent("test", { command: "" });
assert_equals(event.command, "");
}, "command explicitly set to empty string");
test(function () {
const event = new CommandEvent("test", { command: true });
assert_equals(event.command, "true");
}, "command set to true");
test(function () {
const event = new CommandEvent("test", { command: 0.5 });
assert_equals(event.command, "0.5");
}, "command set to a number");
test(function () {
const event = new CommandEvent("test", { command: [] });
assert_equals(event.command, "");
}, "command set to []");
test(function () {
const event = new CommandEvent("test", { command: [1, 2, 3] });
assert_equals(event.command, "1,2,3");
}, "command set to [1, 2, 3]");
test(function () {
const event = new CommandEvent("test", { command: { sample: 0.5 } });
assert_equals(event.command, "[object Object]");
}, "command set to an object");
test(function () {
const event = new CommandEvent("test", {
command: {
toString() {
return "sample";
},
},
});
assert_equals(event.command, "sample");
}, "command set to an object with a toString function");
test(function () {
const eventInit = { command: "sample", source: document.body };
const event = new CommandEvent("test", eventInit);
assert_equals(event.command, "sample");
assert_equals(event.source, document.body);
}, "CommandEventInit properties set value");
test(function () {
const eventInit = {
command: "open",
source: document.getElementById("div"),
};
const event = new CommandEvent("beforetoggle", eventInit);
assert_equals(event.command, "open");
assert_equals(event.source, document.getElementById("div"));
}, "CommandEventInit properties set value 2");
test(function () {
const eventInit = {
command: "closed",
source: document.getElementById("button"),
};
const event = new CommandEvent("toggle", eventInit);
assert_equals(event.command, "closed");
assert_equals(event.source, document.getElementById("button"));
}, "CommandEventInit properties set value 3");
test(function () {
const event = new CommandEvent("test", { source: undefined });
assert_equals(event.source, null);
}, "source set to undefined");
test(function () {
const event = new CommandEvent("test", { source: null });
assert_equals(event.source, null);
}, "source set to null");
test(function () {
assert_throws_js(
TypeError,
function () {
new CommandEvent("test", { source: false });
},
"source is not an object",
);
}, "source set to false");
test(function () {
assert_throws_js(
TypeError,
function () {
const event = new CommandEvent("test", { source: true });
},
"source is not an object",
);
}, "source set to true");
test(function () {
assert_throws_js(
TypeError,
function () {
const event = new CommandEvent("test", { source: {} });
},
"source is not an object",
);
}, "source set to {}");
test(function () {
assert_throws_js(
TypeError,
function () {
const eventInit = { command: "closed", source: new XMLHttpRequest() };
const event = new CommandEvent("toggle", eventInit);
},
"source is not an Element",
);
}, "source set to non-Element EventTarget");
</script>
|