| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { after, describe, expect, test, watchListeners } from "@odoo/hoot"; |
| 4 |
import { queryOne } from "@odoo/hoot-dom"; |
| 5 |
import { EventBus } from "@odoo/owl"; |
| 6 |
import { mountForTest, parseUrl } from "../local_helpers"; |
| 7 |
|
| 8 |
describe(parseUrl(import.meta.url), () => { |
| 9 |
class TestBus extends EventBus { |
| 10 |
addEventListener(type) { |
| 11 |
expect.step(`addEventListener:${type}`); |
| 12 |
return super.addEventListener(...arguments); |
| 13 |
} |
| 14 |
|
| 15 |
removeEventListener() { |
| 16 |
throw new Error("Cannot remove event listeners"); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
let testBus; |
| 21 |
|
| 22 |
test("elementFromPoint and elementsFromPoint should be mocked", async () => { |
| 23 |
await mountForTest(/* xml */ ` |
| 24 |
<div class="oui" style="position: absolute; left: 10px; top: 10px; width: 250px; height: 250px;"> |
| 25 |
Oui |
| 26 |
</div> |
| 27 |
`); |
| 28 |
|
| 29 |
expect(".oui").toHaveRect({ |
| 30 |
x: 10, |
| 31 |
y: 10, |
| 32 |
width: 250, |
| 33 |
height: 250, |
| 34 |
}); |
| 35 |
|
| 36 |
const div = queryOne(".oui"); |
| 37 |
expect(document.elementFromPoint(11, 11)).toBe(div); |
| 38 |
expect(document.elementsFromPoint(11, 11)).toEqual([ |
| 39 |
div, |
| 40 |
document.body, |
| 41 |
document.documentElement, |
| 42 |
]); |
| 43 |
|
| 44 |
expect(document.elementFromPoint(9, 9)).toBe(document.body); |
| 45 |
expect(document.elementsFromPoint(9, 9)).toEqual([document.body, document.documentElement]); |
| 46 |
}); |
| 47 |
|
| 48 |
// ! WARNING: the following 2 tests need to be run sequentially to work, as they |
| 49 |
// ! attempt to test the in-between-tests event listeners cleanup. |
| 50 |
test("event listeners are properly removed: setup", async () => { |
| 51 |
const callback = () => expect.step("callback"); |
| 52 |
|
| 53 |
testBus = new TestBus(); |
| 54 |
|
| 55 |
expect.verifySteps([]); |
| 56 |
|
| 57 |
after(watchListeners()); |
| 58 |
|
| 59 |
testBus.addEventListener("some-event", callback); |
| 60 |
testBus.trigger("some-event"); |
| 61 |
|
| 62 |
expect.verifySteps(["addEventListener:some-event", "callback"]); |
| 63 |
}); |
| 64 |
test("event listeners are properly removed: check", async () => { |
| 65 |
testBus.trigger("some-event"); |
| 66 |
|
| 67 |
expect.verifySteps([]); |
| 68 |
}); |
| 69 |
}); |