From e5d7affed516f2116ebd26c7858ebc1fbb944b6c Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Thu, 8 Jun 2023 10:54:28 +0200 Subject: [PATCH] [FIX] web_editor: ensure standard selection before running tests Some editor tests may be sensitive to the existence or not of a selection in the DOM before initializing the editor. The first test to be run won't have a selection before the editable is initialized, but every subsequent test will (removing the test node add the end moves the selection to its parent). One particularly problematic consequence concerns tests that would fail with an initial selection but succeed otherwise. Such a test would only fail when run after another test, but not when run alone. This commit prevents this confusing debugging situation (and potential false negatives) by setting a selection at the beginning of the testNode by default before running each test, ensuring that each test has the same initial conditions. Part-of: odoo/odoo#141303 --- .../static/src/js/editor/odoo-editor/test/utils.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/utils.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/utils.js index 97cd5c09a20..464a038a209 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/utils.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/utils.js @@ -307,6 +307,12 @@ export async function testEditor(Editor = OdooEditor, spec, options = {}) { // the editor as otherwise those would genererate mutations the editor would // consider and the tests would make no sense. testNode.innerHTML = spec.contentBefore; + // Setting a selection in the DOM before initializing the editor to ensure + // every test is run with the same preconditions. + setTestSelection({ + anchorNode: testNode.parentElement, anchorOffset: 0, + focusNode: testNode.parentElement, focusOffset: 0, + }); const selection = parseTextualSelection(testNode); const editor = new Editor(testNode, Object.assign({ toSanitize: false }, options));