import { Assert, UnitTest } from '@ephox/bedrock-client'; import { Arr } from '@ephox/katamari'; import { Css, SugarElement } from '@ephox/sugar'; import * as CopySelected from 'ephox/snooker/api/CopySelected'; import { reducePrecision } from 'ephox/snooker/test/SizeUtils'; const SEL_CLASS = 'copy-selected'; interface TestCase { readonly label: string; readonly expectedWidth: string; readonly table: string; } const matchWithReducedPrecision = (label: string, expected: string, tableWidth: string) => { const roundedWidth = reducePrecision(tableWidth, 0); Assert.eq(label, expected, roundedWidth); }; const assertWidth = (testCase: TestCase) => () => { const table = SugarElement.fromHtml(testCase.table); // Table needs to be in the actual DOM to perform certain calculations document.body.appendChild(table.dom); const replica = CopySelected.extract(table, `.${SEL_CLASS}`); const tableWidth = Css.get(replica, 'width'); if (testCase.expectedWidth === '') { Assert.eq(testCase.label, testCase.expectedWidth, tableWidth); } else { matchWithReducedPrecision(testCase.label, testCase.expectedWidth, tableWidth); } document.body.removeChild(table.dom); }; const testCases: TestCase[] = [ { label: 'TINY-6664: Assert table width - pixel width single column', expectedWidth: '474px', table: ( `
     
     
` ), }, { label: 'TINY-6664: Assert table width - pixel width multiple columns', expectedWidth: '692px', table: ( `
     
     
` ), }, { label: 'TINY-6664: Assert table width - pixel width entire table', expectedWidth: '1036px', table: ( `
     
     
` ), }, { label: 'TINY-6664: Assert table width - relative width single column', expectedWidth: '21%', table: ( `
     
     
     
` ), }, { label: 'TINY-6664: Assert table width - relative width multiple columns', expectedWidth: '42%', table: ( `
     
     
     
` ), }, { label: 'TINY-6664: Assert table width - relative width entire table', expectedWidth: '63%', table: ( `
     
     
     
` ), }, { label: 'TINY-6664: Assert table width - responsive single column', expectedWidth: '', table: ( `
     
     
` ), }, { label: 'TINY-6664: Assert table width - responsive multiple columns', expectedWidth: '', table: ( `
     
     
` ), }, { label: 'TINY-6664: Assert table width - responsive entire table', expectedWidth: '', table: ( `
     
     
` ), }, { label: 'TINY-6664: Assert table width - colspans (colspan not selected)', expectedWidth: '25%', table: ( `
     
       
       
` ), }, { label: 'TINY-6664: Assert table width - colspans (colspan included in selection)', expectedWidth: '75%', table: ( `
     
       
       
` ), }, { label: 'TINY-6664: Assert table width - rowspans (rowspan not selected)', expectedWidth: '25%', table: ( `
       
     
       
` ), }, { label: 'TINY-6664: Assert table width - rowspans (rowspan included in selection)', expectedWidth: '50%', table: ( `
       
     
       
` ), }, { label: 'TINY-6664: Assert table width - colgroups', expectedWidth: '25%', table: ( `
       
       
       
` ), }, ]; Arr.each(testCases, (testCase) => { UnitTest.test(testCase.label, assertWidth(testCase)); });