Skip to content

Commit cc3bcbe

Browse files
authored
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
1 parent 57493b0 commit cc3bcbe

File tree

6 files changed

+254
-0
lines changed

6 files changed

+254
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import * as core from '@wangeditor-next/core'
2+
import * as slate from 'slate'
3+
4+
import createEditor from '../../../tests/utils/create-editor'
5+
import { TableElement } from '../src/module/custom-types'
6+
import { isCellInFirstRow } from '../src/module/helpers'
7+
8+
function setEditorSelection(
9+
editor: core.IDomEditor,
10+
selection: slate.Selection = {
11+
anchor: { path: [0, 0], offset: 0 },
12+
focus: { path: [0, 0], offset: 0 },
13+
},
14+
) {
15+
editor.selection = selection
16+
}
17+
18+
describe('isCellInFirstRow', () => {
19+
const content = [
20+
{
21+
type: 'paragraph',
22+
children: [
23+
{
24+
text: '',
25+
},
26+
],
27+
},
28+
{
29+
type: 'table',
30+
width: 'auto',
31+
children: [
32+
{
33+
type: 'table-row',
34+
children: [
35+
{
36+
type: 'table-cell',
37+
children: [
38+
{
39+
text: '',
40+
},
41+
],
42+
isHeader: true,
43+
},
44+
{
45+
type: 'table-cell',
46+
children: [
47+
{
48+
text: '',
49+
},
50+
],
51+
isHeader: true,
52+
},
53+
],
54+
},
55+
{
56+
type: 'table-row',
57+
children: [
58+
{
59+
type: 'table-cell',
60+
children: [
61+
{
62+
text: '',
63+
},
64+
],
65+
},
66+
{
67+
type: 'table-cell',
68+
children: [
69+
{
70+
text: '',
71+
},
72+
],
73+
},
74+
],
75+
},
76+
],
77+
columnWidths: [
78+
60,
79+
60,
80+
],
81+
scrollWidth: 120,
82+
height: 62,
83+
},
84+
{
85+
type: 'paragraph',
86+
children: [
87+
{
88+
text: '',
89+
},
90+
],
91+
},
92+
]
93+
94+
const editor = createEditor({ content })
95+
96+
setEditorSelection(editor)
97+
98+
it('should correctly identify cells in the first row', () => {
99+
const result = isCellInFirstRow(editor, (editor.children[1] as TableElement).children[0].children[0])
100+
101+
expect(result).toBe(true)
102+
// Test cell in second row
103+
const secondRowCell = (editor.children[1] as TableElement).children[1].children[0]
104+
105+
expect(isCellInFirstRow(editor, secondRowCell)).toBe(false)
106+
107+
// Test non-cell element
108+
const nonCellElement = editor.children[0]
109+
110+
expect(isCellInFirstRow(editor, nonCellElement as any)).toBe(false)
111+
112+
})
113+
})

packages/table-module/__tests__/parse-html.test.ts

+55
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,54 @@ describe('table - parse html', () => {
9797
children: [{ type: 'table-cell', children: [{ text: 'hello world' }] }],
9898
},
9999
]
100+
const mergeChildren = [
101+
{
102+
type: 'table-row',
103+
children: [
104+
{
105+
type: 'table-cell',
106+
isHeader: false,
107+
colSpan: 2,
108+
rowSpan: 1,
109+
width: 'auto',
110+
children: [
111+
{
112+
text: '',
113+
},
114+
],
115+
hidden: false,
116+
borderWidth: '1',
117+
borderStyle: 'solid',
118+
borderColor: '#ccc',
119+
},
120+
{
121+
type: 'table-cell',
122+
children: [
123+
{
124+
text: '',
125+
},
126+
],
127+
hidden: true,
128+
},
129+
{
130+
type: 'table-cell',
131+
isHeader: false,
132+
colSpan: 1,
133+
rowSpan: 1,
134+
width: 'auto',
135+
children: [
136+
{
137+
text: '',
138+
},
139+
],
140+
hidden: true,
141+
borderWidth: '1',
142+
borderStyle: 'solid',
143+
borderColor: '#ccc',
144+
},
145+
],
146+
},
147+
]
100148

101149
expect($table[0].matches(parseTableHtmlConf.selector)).toBeTruthy()
102150

@@ -106,5 +154,12 @@ describe('table - parse html', () => {
106154
children,
107155
height: 0,
108156
})
157+
158+
expect(parseTableHtmlConf.parseElemHtml($table[0], mergeChildren, editor)).toEqual({
159+
type: 'table',
160+
width: '100%',
161+
children: mergeChildren,
162+
height: 0,
163+
})
109164
})
110165
})

packages/table-module/__tests__/utils/matrices.test.ts

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Point } from '../../src/utils/point'
2+
3+
describe('Point', () => {
4+
test('should create a point with given x and y', () => {
5+
const point = new Point(1, 2)
6+
7+
expect(point.x).toBe(1)
8+
expect(point.y).toBe(2)
9+
})
10+
11+
test('should create a point using valueOf', () => {
12+
const point = Point.valueOf(3, 4)
13+
14+
expect(point.x).toBe(3)
15+
expect(point.y).toBe(4)
16+
})
17+
18+
test('should return true for equal points', () => {
19+
const point1 = new Point(5, 6)
20+
const point2 = new Point(5, 6)
21+
22+
expect(Point.equals(point1, point2)).toBe(true)
23+
})
24+
25+
test('should return false for different points', () => {
26+
const point1 = new Point(7, 8)
27+
const point2 = new Point(9, 10)
28+
29+
expect(Point.equals(point1, point2)).toBe(false)
30+
})
31+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { genRandomStr } from '../../src/utils/util'
2+
3+
describe('genRandomStr', () => {
4+
it('should generate a random string with default prefix', () => {
5+
const result = genRandomStr()
6+
7+
expect(result).toMatch(/^r-/)
8+
})
9+
10+
it('should generate a random string with specified prefix', () => {
11+
const prefix = 'test'
12+
const result = genRandomStr(prefix)
13+
14+
expect(result).toMatch(/^test-/)
15+
})
16+
17+
it('should generate unique strings', () => {
18+
const result1 = genRandomStr()
19+
const result2 = genRandomStr()
20+
21+
expect(result1).not.toBe(result2)
22+
})
23+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { h, VNode } from 'snabbdom'
2+
3+
import { addVnodeStyle } from '../../src/utils/vdom'
4+
5+
describe('addVnodeStyle', () => {
6+
it('should add style to vnode', () => {
7+
const vnode: VNode = { data: {} } as VNode
8+
const newStyle = { color: 'red', fontSize: '16px' }
9+
10+
addVnodeStyle(vnode, newStyle)
11+
12+
expect(vnode.data?.style).toEqual(newStyle)
13+
})
14+
15+
it('should merge styles if vnode already has styles', () => {
16+
const vnode: VNode = h('div', { style: { color: 'blue' } })
17+
const newStyle = { fontSize: '16px' }
18+
19+
addVnodeStyle(vnode, newStyle)
20+
21+
expect(vnode.data?.style).toEqual({ color: 'blue', fontSize: '16px' })
22+
})
23+
24+
it('should initialize data and style if they are not present', () => {
25+
const vnode: VNode = {} as VNode
26+
const newStyle = { color: 'red' }
27+
28+
addVnodeStyle(vnode, newStyle)
29+
30+
expect(vnode.data?.style).toEqual(newStyle)
31+
})
32+
})

0 commit comments

Comments
 (0)