import { describe, expect, test } from "@odoo/hoot"; import { press, queryAllTexts } from "@odoo/hoot-dom"; import { animationFrame } from "@odoo/hoot-mock"; import { setupEditor, testEditor } from "./_helpers/editor"; import { getContent } from "./_helpers/selection"; import { insertText, redo, undo } from "./_helpers/user_actions"; import { execCommand } from "./_helpers/userCommands"; function columnsContainer(contents) { return `
${contents}
`; } function column(size, contents) { return `
${contents}
`; } function columnize(numberOfColumns) { return (editor) => { execCommand(editor, "columnize", { numberOfColumns }); }; } describe("2 columns", () => { test("should display hint for focused empty column.", async () => { await testEditor({ /* eslint-disable */ contentBefore: columnsContainer( column(6, "

[]

") + column(6, "


") ), contentAfterEdit: columnsContainer( column(6, `

[]

`) + column(6, `


`) ), /* eslint-enable */ }); }); test("should display the normal hint when cursor is in an empty cell of an empty table in one of the columns", async () => { await testEditor({ /* eslint-disable */ contentBefore: columnsContainer( column(6, `

[]


`) + column(6, "


") ), contentAfterEdit: columnsContainer( column(6, `

[]


`) + column(6, `


`) ), /* eslint-enable */ }); }); test("should do nothing", async () => { await testEditor({ contentBefore: columnsContainer( column(6, "

abcd

") + column(6, "

[]ef

") ), stepFunction: columnize(2), contentAfter: columnsContainer( column(6, "

abcd

") + column(6, "

[]ef

") ), }); }); test("should turn text into 2 columns", async () => { await testEditor({ contentBefore: "

[]abcd

", stepFunction: columnize(2), contentAfterEdit: /* eslint-disable */ columnsContainer( column(6, "

[]abcd

") + column(6, `


`) ) + "


", contentAfter: columnsContainer( column(6, "

[]abcd

") + column(6, "


") ) + "


", /* eslint-enable */ }); }); test("should turn 3 columns into 2 columns", async () => { await testEditor({ contentBefore: columnsContainer( column(4, "

abcd

") + column(4, "

e[]f

") + column(4, "") ), stepFunction: columnize(2), contentAfter: columnsContainer( column(6, "

abcd

") + column(6, "

e[]f

") ), }); }); test("should turn 4 columns into 2 columns", async () => { await testEditor({ contentBefore: columnsContainer( column(3, "

abcd

") + column(3, "

ef

") + column(3, "") + column(3, "

i[]j

") ), stepFunction: columnize(2), contentAfter: columnsContainer( column(6, "

abcd

") + column(6, "

ef

i[]j

") ), }); }); test("apply '2 columns' powerbox command", async () => { const { el, editor } = await setupEditor("

ab[]cd

"); await insertText(editor, "/2columns"); await animationFrame(); expect(".active .o-we-command-name").toHaveText("2 columns"); await press("enter"); expect(getContent(el)).toBe( `

ab[]cd



` ); await insertText(editor, "/columns"); await animationFrame(); expect(queryAllTexts(".o-we-command-name")).toEqual([ "3 columns", "4 columns", "Remove columns", ]); }); }); describe("3 columns", () => { test("should do nothing", async () => { await testEditor({ contentBefore: columnsContainer( column(4, "

abcd

") + column(4, "


") + column(4, "

[]

") ), /* eslint-disable */ contentBeforeEdit: columnsContainer( column(4, "

abcd

") + column(4, `


`) + column(4, `

[]

`) ), /* eslint-enable */ stepFunction: columnize(3), contentAfter: columnsContainer( column(4, "

abcd

") + column(4, "


") + column(4, "

[]

") ), }); }); test("should turn text into 3 columns", async () => { await testEditor({ contentBefore: "

ab[]cd

", stepFunction: columnize(3), /* eslint-disable */ contentAfterEdit: columnsContainer( column(4, "

ab[]cd

") + column(4, `


`) + column(4, `


`) ) + "


", contentAfter: columnsContainer( column(4, "

ab[]cd

") + column(4, "


") + column(4, "


") ) + "


", /* eslint-enable */ }); }); test("should turn 2 columns into 3 columns", async () => { await testEditor({ contentBefore: columnsContainer( column(6, "

abcd

") + column(6, "

ef

") ), stepFunction: columnize(3), contentAfter: columnsContainer( column(4, "

abcd

") + column(4, "

ef

") + column(4, "


") ), }); }); test("should turn 4 columns into 3 columns", async () => { await testEditor({ contentBefore: columnsContainer( column(3, "

abcd

") + column(3, "

e[]f

") + column(3, "") + column(3, "

ij

") ), stepFunction: columnize(3), contentAfter: columnsContainer( column(4, "

abcd

") + column(4, "

e[]f

") + column(4, "

ij

") ), }); }); test("apply '3 columns' powerbox command", async () => { const { el, editor } = await setupEditor("

ab[]cd

"); await insertText(editor, "/3columns"); await animationFrame(); expect(".active .o-we-command-name").toHaveText("3 columns"); await press("enter"); expect(getContent(el)).toBe( `

ab[]cd




` ); await insertText(editor, "/columns"); await animationFrame(); expect(queryAllTexts(".o-we-command-name")).toEqual([ "2 columns", "4 columns", "Remove columns", ]); }); }); describe("4 columns", () => { test("should do nothing", async () => { await testEditor({ contentBefore: columnsContainer( column(3, "

abcd

") + column(3, "


") + column(3, "


") + column(3, "

[]

") ), stepFunction: columnize(4), contentAfter: columnsContainer( column(3, "

abcd

") + column(3, "


") + column(3, "


") + column(3, "

[]

") ), }); }); test("should turn text into 4 columns", async () => { await testEditor({ contentBefore: "

abcd[]

", stepFunction: columnize(4), contentAfter: columnsContainer( column(3, "

abcd[]

") + column(3, "


") + column(3, "


") + column(3, "


") ) + "


", }); }); test("should turn 2 columns into 4 columns", async () => { await testEditor({ contentBefore: columnsContainer( column(6, "

abcd

") + column(6, "

[]ef

") ), stepFunction: columnize(4), contentAfter: columnsContainer( column(3, "

abcd

") + column(3, "

[]ef

") + column(3, "


") + column(3, "


") ), }); }); test("should turn 3 columns into 4 columns", async () => { await testEditor({ contentBefore: columnsContainer( column(4, "

abcd

") + column(4, "

ef[]

") + column(4, "

ij

") ), stepFunction: columnize(4), contentAfter: columnsContainer( column(3, "

abcd

") + column(3, "

ef[]

") + column(3, "

ij

") + column(3, "


") ), }); }); test("apply '4 columns' powerbox command", async () => { const { el, editor } = await setupEditor("

ab[]cd

"); await insertText(editor, "/4columns"); await animationFrame(); expect(".active .o-we-command-name").toHaveText("4 columns"); await press("enter"); expect(getContent(el)).toBe( `

ab[]cd





` ); await insertText(editor, "/columns"); await animationFrame(); expect(queryAllTexts(".o-we-command-name")).toEqual([ "2 columns", "3 columns", "Remove columns", ]); }); }); describe("remove columns", () => { test("should do nothing", async () => { await testEditor({ contentBefore: "

ab[]cd

", stepFunction: columnize(0), contentAfter: "

ab[]cd

", }); }); test("should turn 2 columns into text", async () => { await testEditor({ contentBefore: columnsContainer( column(6, "

abcd

") + column(6, "

[]ef

") ), stepFunction: columnize(0), contentAfter: "

abcd

[]ef

", }); }); test("should turn 3 columns into text", async () => { await testEditor({ contentBefore: columnsContainer( column(4, "

abcd

") + column(4, "

ef[]

") + column(4, "

ij

") ), stepFunction: columnize(0), contentAfter: "

abcd

ef[]

ij

", }); }); test("should turn 4 columns into text", async () => { await testEditor({ contentBefore: columnsContainer( column(3, "

abcd

") + column(3, "

ef

") + column(3, "

ij

") + column(3, "

[]

") ), stepFunction: columnize(0), contentAfter: "

abcd

ef

ij

[]

", }); }); test("apply 'remove columns' powerbox command", async () => { const { el, editor } = await setupEditor("

ab[]cd

"); await insertText(editor, "/columns"); await animationFrame(); expect(queryAllTexts(".o-we-command-name")).toEqual([ "2 columns", "3 columns", "4 columns", ]); // add 2 columns await press("enter"); expect(getContent(el)).toBe( `

ab[]cd



` ); await insertText(editor, "/removecolumns"); await animationFrame(); expect(".active .o-we-command-name").toHaveText("Remove columns"); await press("enter"); expect(getContent(el)).toBe(`

ab[]cd



`); }); }); describe("complex", () => { test("should turn text into 2 columns, then 3, 4, 3, 2 and text again", async () => { await testEditor({ contentBefore: "

ab[]cd

", stepFunction: (editor) => { columnize(2)(editor); columnize(3)(editor); columnize(4)(editor); columnize(3)(editor); columnize(2)(editor); columnize(0)(editor); }, // A paragraph was created for each column + after them and // they were all kept. contentAfter: "

ab[]cd





", }); }); test("should not add a container when one already exists", async () => { await testEditor({ contentBefore: '
' + "

ab[]cd

" + "
", stepFunction: columnize(2), contentAfter: '
' + '
' + // no "container" class '
' + "

ab[]cd

" + "
" + '


' + "
" + "


" + "
", }); }); }); describe("undo", () => { test("should be able to write after undo", async () => { await testEditor({ contentBefore: "

[]

", stepFunction: async (editor) => { columnize(2)(editor); undo(editor); await insertText(editor, "x"); }, contentAfter: "

x[]

", }); }); test("should work properly after undo and then redo", async () => { await testEditor({ contentBefore: "

[]

", stepFunction: async (editor) => { columnize(2)(editor); undo(editor); redo(editor); await insertText(editor, "x"); }, contentAfter: columnsContainer(column(6, "

x[]

") + column(6, "


")) + "


", }); }); });