-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(TABLE): add more table test (#527)
* test(table): add table utils has-common test * chore: add global test type * test(table): add table utils point test * test(table): add table utils util test * test(vdom): add tests for addVnodeStyle utility function * test(table): add tests for isCellInFirstRow helper function * test(table): refactor isCellInFirstRow function * test(table): add mergeChildren to parseTableHtml * test: fix mergeChildren property in parseTableHtml function
- Loading branch information
Showing
6 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import * as core from '@wangeditor-next/core' | ||
import * as slate from 'slate' | ||
|
||
import createEditor from '../../../tests/utils/create-editor' | ||
import { TableElement } from '../src/module/custom-types' | ||
import { isCellInFirstRow } from '../src/module/helpers' | ||
|
||
function setEditorSelection( | ||
editor: core.IDomEditor, | ||
selection: slate.Selection = { | ||
anchor: { path: [0, 0], offset: 0 }, | ||
focus: { path: [0, 0], offset: 0 }, | ||
}, | ||
) { | ||
editor.selection = selection | ||
} | ||
|
||
describe('isCellInFirstRow', () => { | ||
const content = [ | ||
{ | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
text: '', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'table', | ||
width: 'auto', | ||
children: [ | ||
{ | ||
type: 'table-row', | ||
children: [ | ||
{ | ||
type: 'table-cell', | ||
children: [ | ||
{ | ||
text: '', | ||
}, | ||
], | ||
isHeader: true, | ||
}, | ||
{ | ||
type: 'table-cell', | ||
children: [ | ||
{ | ||
text: '', | ||
}, | ||
], | ||
isHeader: true, | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'table-row', | ||
children: [ | ||
{ | ||
type: 'table-cell', | ||
children: [ | ||
{ | ||
text: '', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'table-cell', | ||
children: [ | ||
{ | ||
text: '', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
columnWidths: [ | ||
60, | ||
60, | ||
], | ||
scrollWidth: 120, | ||
height: 62, | ||
}, | ||
{ | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
text: '', | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
const editor = createEditor({ content }) | ||
|
||
setEditorSelection(editor) | ||
|
||
it('should correctly identify cells in the first row', () => { | ||
const result = isCellInFirstRow(editor, (editor.children[1] as TableElement).children[0].children[0]) | ||
|
||
expect(result).toBe(true) | ||
// Test cell in second row | ||
const secondRowCell = (editor.children[1] as TableElement).children[1].children[0] | ||
|
||
expect(isCellInFirstRow(editor, secondRowCell)).toBe(false) | ||
|
||
// Test non-cell element | ||
const nonCellElement = editor.children[0] | ||
|
||
expect(isCellInFirstRow(editor, nonCellElement as any)).toBe(false) | ||
|
||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Point } from '../../src/utils/point' | ||
|
||
describe('Point', () => { | ||
test('should create a point with given x and y', () => { | ||
const point = new Point(1, 2) | ||
|
||
expect(point.x).toBe(1) | ||
expect(point.y).toBe(2) | ||
}) | ||
|
||
test('should create a point using valueOf', () => { | ||
const point = Point.valueOf(3, 4) | ||
|
||
expect(point.x).toBe(3) | ||
expect(point.y).toBe(4) | ||
}) | ||
|
||
test('should return true for equal points', () => { | ||
const point1 = new Point(5, 6) | ||
const point2 = new Point(5, 6) | ||
|
||
expect(Point.equals(point1, point2)).toBe(true) | ||
}) | ||
|
||
test('should return false for different points', () => { | ||
const point1 = new Point(7, 8) | ||
const point2 = new Point(9, 10) | ||
|
||
expect(Point.equals(point1, point2)).toBe(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { genRandomStr } from '../../src/utils/util' | ||
|
||
describe('genRandomStr', () => { | ||
it('should generate a random string with default prefix', () => { | ||
const result = genRandomStr() | ||
|
||
expect(result).toMatch(/^r-/) | ||
}) | ||
|
||
it('should generate a random string with specified prefix', () => { | ||
const prefix = 'test' | ||
const result = genRandomStr(prefix) | ||
|
||
expect(result).toMatch(/^test-/) | ||
}) | ||
|
||
it('should generate unique strings', () => { | ||
const result1 = genRandomStr() | ||
const result2 = genRandomStr() | ||
|
||
expect(result1).not.toBe(result2) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { h, VNode } from 'snabbdom' | ||
|
||
import { addVnodeStyle } from '../../src/utils/vdom' | ||
|
||
describe('addVnodeStyle', () => { | ||
it('should add style to vnode', () => { | ||
const vnode: VNode = { data: {} } as VNode | ||
const newStyle = { color: 'red', fontSize: '16px' } | ||
|
||
addVnodeStyle(vnode, newStyle) | ||
|
||
expect(vnode.data?.style).toEqual(newStyle) | ||
}) | ||
|
||
it('should merge styles if vnode already has styles', () => { | ||
const vnode: VNode = h('div', { style: { color: 'blue' } }) | ||
const newStyle = { fontSize: '16px' } | ||
|
||
addVnodeStyle(vnode, newStyle) | ||
|
||
expect(vnode.data?.style).toEqual({ color: 'blue', fontSize: '16px' }) | ||
}) | ||
|
||
it('should initialize data and style if they are not present', () => { | ||
const vnode: VNode = {} as VNode | ||
const newStyle = { color: 'red' } | ||
|
||
addVnodeStyle(vnode, newStyle) | ||
|
||
expect(vnode.data?.style).toEqual(newStyle) | ||
}) | ||
}) |