import { setSelection } from "@html_editor/../tests/_helpers/selection"; import { insertText } from "@html_editor/../tests/_helpers/user_actions"; import { HtmlMailField } from "@mail/views/web/fields/html_mail_field/html_mail_field"; import { after, before, beforeEach, expect, test } from "@odoo/hoot"; import { press, queryOne } from "@odoo/hoot-dom"; import { contains, defineModels, fields, models, mountView, onRpc, patchWithCleanup, } from "@web/../tests/web_test_helpers"; import { mailModels } from "../mail_test_helpers"; import { animationFrame } from "@odoo/hoot-mock"; function setSelectionInHtmlField(selector = "p", fieldName = "body") { const anchorNode = queryOne(`[name='${fieldName}'] .odoo-editor-editable ${selector}`); setSelection({ anchorNode, anchorOffset: 0 }); return anchorNode; } function useCustomStyleRules(rules = "") { let style; before(() => { style = document.createElement("STYLE"); style.type = "text/css"; style.append(document.createTextNode(rules)); document.head.append(style); }); after(() => { style.remove(); }); } class CustomMessage extends models.Model { _name = "custom.message"; title = fields.Char(); body = fields.Html(); _records = [ { id: 1, title: "first", body: "

first

" }, { id: 2, title: "second", body: "

second

" }, ]; _onChanges = { title(record) { record.body = `

${record.title}

`; }, }; } defineModels({ ...mailModels, CustomMessage }); let htmlEditor; beforeEach(() => { patchWithCleanup(HtmlMailField.prototype, { onEditorLoad(editor) { htmlEditor = editor; return super.onEditorLoad(...arguments); }, }); }); test("HtmlMail save inline html", async function () { useCustomStyleRules(`.test-h1-inline .note-editable h1 { color: #111827 !important; }`); onRpc("web_save", ({ args }) => { expect(args[1].body.replace(/font-size: ?(\d+(\.\d+)?)px/, "font-size: []px")).toBe( `

first

` ); expect.step("web_save"); }); await mountView({ type: "form", resId: 1, resModel: "custom.message", arch: `
`, }); setSelectionInHtmlField(); await insertText(htmlEditor, "/heading1"); await press("enter"); expect(".odoo-editor-editable").toHaveInnerHTML("

first

"); await contains(".o_form_button_save").click(); expect.verifySteps(["web_save"]); }); test("HtmlMail don't have access to column commands", async function () { await mountView({ type: "form", resId: 1, resModel: "custom.message", arch: `
`, }); setSelectionInHtmlField(); await insertText(htmlEditor, "/"); await animationFrame(); expect(".o-we-powerbox").toHaveCount(1); await insertText(htmlEditor, "column"); await animationFrame(); expect(".o-we-powerbox").toHaveCount(0); }); test("HtmlMail add icon and save inline html", async function () { useCustomStyleRules( `.test-icon-inline .note-editable .fa { color: rgb(55,65,81) !important; background-color: rgb(249,250,251) !important; }` ); onRpc("web_save", ({ args }) => { expect(args[1].body).toBe( `

first

` ); expect.step("web_save"); }); await mountView({ type: "form", resId: 1, resModel: "custom.message", arch: `
`, }); setSelectionInHtmlField(); await insertText(htmlEditor, "/image"); await press("enter"); await contains("a.nav-link:contains('Icons')").click(); await contains("span.fa-glass").click(); await contains(".o_form_button_save").click(); expect.verifySteps(["web_save"]); });