/** @odoo-module **/
import { setSelection } from "@web_editor/js/editor/odoo-editor/src/utils/utils";
import { makeView, setupViewRegistries } from "@web/../tests/views/helpers";
import { patchWithCleanup, nextTick } from "@web/../tests/helpers/utils";
import { Wysiwyg } from "@web_editor/js/wysiwyg/wysiwyg";
import {
triggerEvent,
insertText,
} from "@web_editor/js/editor/odoo-editor/test/utils";
function onMount() {;
const editor = wysiwyg.odooEditor;
const editable = editor.editable;
editor.testMode = true;
return { editor, editable };
}
let wysiwyg;
QUnit.module(
"web_editor",
{
beforeEach: async function () {
const serverData = {
models: {
note: {
fields: {
body: {
string: "Editor",
type: "html",
},
},
records: [
{
id: 1,
display_name: "first record",
body: "
",
},
],
},
},
};
setupViewRegistries();
patchWithCleanup(Wysiwyg.prototype, {
init() {
super.init(...arguments);
wysiwyg = this;
},
});
await makeView({
type: "form",
serverData,
resModel: "note",
arch:
"",
resId: 1,
});
},
},
function () {
QUnit.module("insert a new banner");
QUnit.test("should insert a banner followed by a paragraph", async function (assert) {
const { editor, editable } = onMount();
const node = editable.querySelector("p");
setSelection(node, 0);
insertText(editor, "Test");
setSelection(node.firstChild, 3, node.firstChild, 3);
insertText(editor, '/banner');
triggerEvent(editor.editable, "keydown", { key: "Enter" });
await nextTick();
assert.strictEqual(
editable.innerHTML,
`Test
`,
);
});
QUnit.test("should have focus inside banner when new banner is created", async function (assert) {
const { editor, editable } = onMount();
const node = editable.querySelector("p");
setSelection(node, 0);
insertText(editor, "Test");
setSelection(node.firstChild, 3, node.firstChild, 3);
insertText(editor, '/banner');
triggerEvent(editor.editable, "keydown", { key: "Enter" });
await nextTick();
assert.strictEqual(
editable.innerHTML,
`Test
`,
);
});
QUnit.module("banner selection and backspace");
QUnit.test("preserves the first paragraph tag inside the banner", async function (assert) {
const { editor, editable } = onMount();
const node = editable.querySelector("p");
setSelection(node, 0);
insertText(editor, "Test");
setSelection(node.firstChild, 3, node.firstChild, 3);
insertText(editor, '/banner');
triggerEvent(editor.editable, "keydown", { key: "Enter" });
await nextTick();
insertText(editor, 'Test1');
triggerEvent(editor.editable, "input", { inputType: "insertParagraph" });
insertText(editor, 'Test2');
triggerEvent(editor.editable, "input", { inputType: "insertParagraph" });
triggerEvent(editor.editable, "keydown", { key: "a", ctrlKey: true });
await nextTick();
triggerEvent(editor.editable, "input", { inputType: "deleteContentBackward" });
assert.strictEqual(
editable.innerHTML,
`Test
`,
);
});
QUnit.test("First element of o_editable is not editable", async function (assert) {
const { editor, editable } = onMount();
const node = editable.querySelector("p");
setSelection(node, 0);
insertText(editor, '/banner');
triggerEvent(editor.editable, "keydown", { key: "Enter" });
await nextTick();
const p = editable.querySelectorAll('p')[1];
setSelection(p, 0);
insertText(editor, 'Test1');
triggerEvent(editor.editable, "input", { inputType: "insertParagraph" });
insertText(editor, 'Test2');
triggerEvent(editor.editable, "keydown", { key: "a", ctrlKey: true });
await nextTick();
triggerEvent(editor.editable, "input", { inputType: "deleteContentBackward" });
assert.strictEqual(
editable.innerHTML,
`
`,
"should not remove banner when ctrl+a and backspace are performed",
);
});
}
);