| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
import QUnit from "qunit-tests"; |
| 4 |
|
| 5 |
QUnit.test("event dispatch", function (assert) { |
| 6 |
const obj = { }; |
| 7 |
cockpit.event_target(obj); |
| 8 |
|
| 9 |
let count = 0; |
| 10 |
function handler(ev) { |
| 11 |
if (count === 0) { |
| 12 |
assert.equal(typeof ev, "object", "event is object"); |
| 13 |
assert.equal(ev.type, "action", "event.type is 'action'"); |
| 14 |
assert.equal(ev.data, "Data", "event.data is set"); |
| 15 |
} |
| 16 |
count += 1; |
| 17 |
} |
| 18 |
|
| 19 |
const ev = new Event("action", { bubbles: false, cancelable: false }); |
| 20 |
ev.data = "Data"; |
| 21 |
|
| 22 |
obj.dispatchEvent(ev); |
| 23 |
assert.strictEqual(count, 0, "count is zero"); |
| 24 |
|
| 25 |
obj.onaction = handler; |
| 26 |
obj.dispatchEvent(ev); |
| 27 |
assert.strictEqual(count, 1, "count is one"); |
| 28 |
|
| 29 |
obj.addEventListener("action", handler, true); |
| 30 |
obj.dispatchEvent(ev); |
| 31 |
assert.strictEqual(count, 3, "count is three"); |
| 32 |
|
| 33 |
obj.addEventListener("action", handler, true); |
| 34 |
obj.dispatchEvent(ev); |
| 35 |
assert.strictEqual(count, 6, "count is six"); |
| 36 |
|
| 37 |
obj.removeEventListener("action", handler, true); |
| 38 |
obj.dispatchEvent(ev); |
| 39 |
assert.strictEqual(count, 8, "count is eight"); |
| 40 |
|
| 41 |
obj.onaction = null; |
| 42 |
obj.dispatchEvent(ev); |
| 43 |
assert.strictEqual(count, 9, "count is nine"); |
| 44 |
|
| 45 |
obj.removeEventListener("action", handler, true); |
| 46 |
obj.dispatchEvent(ev); |
| 47 |
assert.strictEqual(count, 9, "count is still nine"); |
| 48 |
}); |
| 49 |
|
| 50 |
QUnit.start(); |