Debian Codegraph @

addons/web/static/lib/hoot/tests/mock/navigator.test.js

raw
1 /** @odoo-module */
2
3 import { describe, expect, mockSendBeacon, mockTouch, mockVibrate, test } from "@odoo/hoot";
4 import { parseUrl } from "../local_helpers";
5
6 /**
7 * @param {Promise<any>} promise
8 */
9 const ensureResolvesImmediatly = (promise) =>
10 Promise.race([
11 promise,
12 new Promise((resolve, reject) => reject("failed to resolve in a single micro tick")),
13 ]);
14
15 describe(parseUrl(import.meta.url), () => {
16 describe("clipboard", () => {
17 test.tags("secure");
18 test("read/write calls are resolved immediatly", async () => {
19 navigator.clipboard.write([
20 new ClipboardItem({
21 "text/plain": new Blob(["some text"], { type: "text/plain" }),
22 }),
23 ]);
24
25 const items = await ensureResolvesImmediatly(navigator.clipboard.read());
26
27 expect(items).toHaveLength(1);
28 expect(items[0]).toBeInstanceOf(ClipboardItem);
29
30 const blob = await ensureResolvesImmediatly(items[0].getType("text/plain"));
31
32 expect(blob).toBeInstanceOf(Blob);
33
34 const value = await ensureResolvesImmediatly(blob.text());
35
36 expect(value).toBe("some text");
37 });
38 });
39
40 test("maxTouchPoints", () => {
41 mockTouch(false);
42
43 expect(navigator.maxTouchPoints).toBe(0);
44
45 mockTouch(true);
46
47 expect(navigator.maxTouchPoints).toBe(1);
48 });
49
50 test("sendBeacon", () => {
51 expect(() => navigator.sendBeacon("/route", new Blob([]))).toThrow(/sendBeacon/);
52
53 mockSendBeacon(expect.step);
54
55 expect.verifySteps([]);
56
57 navigator.sendBeacon("/route", new Blob([]));
58
59 expect.verifySteps(["/route"]);
60 });
61
62 test("vibrate", () => {
63 expect(() => navigator.vibrate(100)).toThrow(/vibrate/);
64
65 mockVibrate(expect.step);
66
67 expect.verifySteps([]);
68
69 navigator.vibrate(100);
70
71 expect.verifySteps([100]);
72 });
73 });