Debian Codegraph @

addons/web/static/tests/legacy/helpers/cleanup.js

raw symbols SCIP
1 /** @odoo-module alias=@web/../tests/helpers/cleanup default=false */
2
3 // -----------------------------------------------------------------------------
4 // Cleanup
5 // -----------------------------------------------------------------------------
6
7 const cleanups = [];
8
9 /**
10 * Register a cleanup callback that will be executed whenever the current test
11 * is done.
12 *
13 * - the cleanups will be executed in reverse order
14 * - they will be executed even if the test fails/crashes
15 *
16 * @param {Function} callback
17 */
18 export function registerCleanup(callback) {
19 cleanups.push(callback);
20 }
21
22 if (window.QUnit) {
23 QUnit.on("OdooAfterTestHook", (info) => {
24 if (QUnit.config.debug) {
25 return;
26 }
27 let cleanup;
28 // note that this calls the cleanup callbacks in reverse order!
29 while ((cleanup = cleanups.pop())) {
30 try {
31 cleanup(info);
32 } catch (error) {
33 console.error(error);
34 }
35 }
36 });
37
38 // -----------------------------------------------------------------------------
39 // Check leftovers
40 // -----------------------------------------------------------------------------
41
42 /**
43 * List of elements tolerated in the body after a test. The property "keep"
44 * prevents the element from being removed (typically: qunit suite elements).
45 */
46 const validElements = [
47 // always in the body:
48 { tagName: "DIV", attr: "id", value: "qunit", keep: true },
49 { tagName: "DIV", attr: "id", value: "qunit-fixture", keep: true },
50 // shouldn't be in the body after a test but are tolerated:
51 { tagName: "SCRIPT", attr: "id", value: "" },
52 { tagName: "DIV", attr: "class", value: "o_notification_manager" },
53 { tagName: "DIV", attr: "class", value: "tooltip fade bs-tooltip-auto" },
54 { tagName: "DIV", attr: "class", value: "tooltip fade bs-tooltip-auto show" },
55 { tagName: "DIV", attr: "class", value: "tooltip tooltip-field-info fade bs-tooltip-auto" },
56 {
57 tagName: "DIV",
58 attr: "class",
59 value: "tooltip tooltip-field-info fade bs-tooltip-auto show",
60 },
61
62 // Due to a Document Kanban bug (already present in 12.0)
63 { tagName: "DIV", attr: "class", value: "ui-helper-hidden-accessible" },
64 {
65 tagName: "UL",
66 attr: "class",
67 value: "ui-menu ui-widget ui-widget-content ui-autocomplete ui-front",
68 },
69 {
70 tagName: "UL",
71 attr: "class",
72 value: "ui-menu ui-widget ui-widget-content ui-autocomplete dropdown-menu ui-front", // many2ones
73 },
74 ];
75
76 /**
77 * After each test, we check that there is no leftover in the DOM.
78 *
79 * Note: this event is not QUnit standard, we added it for this specific use case.
80 * As a payload, an object with keys 'moduleName' and 'testName' is provided. It
81 * is used to indicate the test that left elements in the DOM, when it happens.
82 */
83 QUnit.on("OdooAfterTestHook", function (info) {
84 if (QUnit.config.debug) {
85 return;
86 }
87 const failed = info.testReport.getStatus() === "failed";
88 const toRemove = [];
89 // check for leftover elements in the body
90 for (const bodyChild of document.body.children) {
91 const tolerated = validElements.find(
92 (e) => e.tagName === bodyChild.tagName && bodyChild.getAttribute(e.attr) === e.value
93 );
94 if (!failed && !tolerated) {
95 QUnit.pushFailure(
96 `Body still contains undesirable elements:\n${bodyChild.outerHTML}`
97 );
98 }
99 if (!tolerated || !tolerated.keep) {
100 toRemove.push(bodyChild);
101 }
102 }
103 // cleanup leftovers in #qunit-fixture
104 const qunitFixture = document.getElementById("qunit-fixture");
105 if (qunitFixture.children.length) {
106 toRemove.push(...qunitFixture.children);
107 }
108 // remove unwanted elements if not in debug
109 for (const el of toRemove) {
110 el.remove();
111 }
112 document.body.classList.remove("modal-open");
113 });
114 }