import { Plugin } from "@html_editor/plugin"; import { MAIN_PLUGINS } from "@html_editor/plugin_sets"; import { expect, test } from "@odoo/hoot"; import { click } from "@odoo/hoot-dom"; import { setupEditor, testEditor } from "./_helpers/editor"; import { getContent, setContent } from "./_helpers/selection"; import { withSequence } from "@html_editor/utils/resource"; import { execCommand } from "./_helpers/userCommands"; test("can instantiate a Editor", async () => { const { el, editor } = await setupEditor("

hel[lo] world

", {}); expect(el.innerHTML).toBe(`

hello world

`); expect(getContent(el)).toBe(`

hel[lo] world

`); setContent(el, "
a[dddb]
"); execCommand(editor, "formatBold"); expect(getContent(el)).toBe(`
a[dddb]
`); }); test("cannot reattach an editor", async () => { const { el, editor } = await setupEditor("

[]

", {}); expect(getContent(el)).toBe(`

[]

`); expect(() => editor.attachTo(el)).toThrow("Cannot re-attach an editor"); }); test("cannot reattach a destroyed editor", async () => { const { el, editor } = await setupEditor("

[]

", {}); expect(getContent(el)).toBe(`

[]

`); editor.destroy(); expect(getContent(el)).toBe(`

[]

`); expect(() => editor.attachTo(el)).toThrow("Cannot re-attach an editor"); }); test.tags("iframe"); test("can instantiate a Editor in an iframe", async () => { const { el, editor } = await setupEditor("

hel[lo] world

", { props: { iframe: true } }); expect("iframe").toHaveCount(1); expect(el.innerHTML).toBe(`

hello world

`); expect(getContent(el)).toBe(`

hel[lo] world

`); setContent(el, "
a[dddb]
"); execCommand(editor, "formatBold"); expect(getContent(el)).toBe(`
a[dddb]
`); }); test("with an empty selector", async () => { const { el } = await setupEditor("
[]
", {}); expect(el.innerHTML).toBe( `
` ); expect(getContent(el)).toBe( `
[]
` ); }); test("with a part of the selector in an empty HTMLElement", async () => { const { el } = await setupEditor("
a[bc
]
", {}); expect(el.innerHTML).toBe(`
abc
`); expect(getContent(el)).toBe(`
a[bc
]
`); }); test("inverse selection", async () => { const { el } = await setupEditor("
a]bc
[
", {}); expect(el.innerHTML).toBe(`
abc
`); expect(getContent(el)).toBe(`
a]bc
[
`); }); test("with an empty selector and a
", async () => { const { el } = await setupEditor("

[]

", {}); expect(getContent(el)).toBe( `

[]

` ); }); test("no arrow key press or mouse click should keep selection near a contenteditable='false'", async () => { await testEditor({ contentBefore: '[]
', contentAfter: "[]
", }); await testEditor({ contentBefore: '
[]', contentAfter: "
[]", }); }); test("event handlers are properly cleaned up after destruction", async () => { let count = 0; class TestHandlerPlugin extends Plugin { static id = "testHandler"; setup() { this.addDomListener(document.body, "click", () => count++); } } const { editor } = await setupEditor("

", { config: { Plugins: [...MAIN_PLUGINS, TestHandlerPlugin] }, }); expect(count).toBe(0); await click(document.body); expect(count).toBe(1); editor.destroy(); await click(document.body); expect(count).toBe(1); }); test("can give resources in config", async () => { expect.assertions(1); class TestPlugin extends Plugin { static id = "test"; setup() { expect(this.getResource("some")).toEqual(["value"]); } } await setupEditor("

", { config: { Plugins: [...MAIN_PLUGINS, TestPlugin], resources: { some: ["value"] }, }, }); }); test("resource can have sequence", async () => { expect.assertions(1); class TestPlugin extends Plugin { static id = "test"; resources = { test_resource: [ { value: 2 }, withSequence(20, { value: 4 }), withSequence(5, { value: 1 }), { value: 3 }, ], }; setup() { expect(this.getResource("test_resource")).toEqual([ { value: 1 }, { value: 2 }, { value: 3 }, { value: 4 }, ]); } } await setupEditor("

", { config: { Plugins: [...MAIN_PLUGINS, TestPlugin], }, }); });