import { UiFinder } from '@ephox/agar'; import { context, describe, it } from '@ephox/bedrock-client'; import { SelectorFind } from '@ephox/sugar'; import { TinyDom, TinyHooks } from '@ephox/wrap-mcagar'; import { assert } from 'chai'; import Editor from 'tinymce/core/api/Editor'; import * as TableTestUtils from '../../module/table/TableTestUtils'; interface Scenario { readonly html: string; } describe('browser.tinymce.models.dom.table.InsertRowTableResizeTest', () => { let objectResizedCounter = 0; const hook = TinyHooks.bddSetup({ width: 400, base_url: '/project/tinymce/js/tinymce', setup: (editor: Editor) => { editor.on('ObjectResized', () => objectResizedCounter++); } }, [], true); const emptyTable = { html: '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
' }; const contentsInSomeCells = { html: '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
a1b1
' }; const contentsInAllCells = { html: '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
a1b1
a2b2
a3b3
' }; const tableWithHeadings = { html: '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
a1b1
a1b1
a2b2
' }; const tableWithCaption = { html: '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
alphabet
a1b1
a2b2
' }; const nestedTables = { html: '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
a1' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
' + '
b1
a2b2
' }; const insertTable = (editor: Editor, table: string) => { editor.setContent(table); const bodyElem = TinyDom.fromDom(editor.getBody()); const tableElem = UiFinder.findIn(bodyElem, 'table').getOrDie(); SelectorFind.descendant(tableElem, 'td,th').each((cell) => { editor.selection.select(cell.dom, true); editor.selection.collapse(true); }); return tableElem; }; const insertRowMeasureWidth = (editor: Editor, scenario: Scenario) => { const table = insertTable(editor, scenario.html); const widthBefore = TableTestUtils.getWidths(editor, table.dom); const cellWidthBefore = TableTestUtils.getCellWidth(editor, table, 0, 0); TableTestUtils.insertRowBefore(editor); TableTestUtils.insertRowAfter(editor); TableTestUtils.deleteRow(editor); const widthAfter = TableTestUtils.getWidths(editor, table.dom); const cellWidthAfter = TableTestUtils.getCellWidth(editor, table, 0, 0); return { widthBefore, widthAfter, cellWidthBefore, cellWidthAfter }; }; const insertRowAssertWidth = (scenario: Scenario) => { const widths = insertRowMeasureWidth(hook.editor(), scenario); TableTestUtils.assertWidths(widths); assert.deepEqual(widths.cellWidthAfter, widths.cellWidthBefore, 'table cell widths should not change'); assert.equal(objectResizedCounter, 0, 'ObjectResized shouldn\'t have fired'); }; context('Insert rows, erase row and assert the table width and cell widths does not change', () => { it('table which is empty', () => insertRowAssertWidth(emptyTable)); it('table with contents in some cells', () => insertRowAssertWidth(contentsInSomeCells)); it('table with contents in all cells', () => insertRowAssertWidth(contentsInAllCells)); it('table with headings', () => insertRowAssertWidth(tableWithHeadings)); it('table with caption', () => insertRowAssertWidth(tableWithCaption)); it('table with nested tables', () => insertRowAssertWidth(nestedTables)); }); });