Skip to content

Commit

Permalink
test(table): add table utils has-common test (#526)
Browse files Browse the repository at this point in the history
* test(table): add table utils has-common test

* chore: add global test type
  • Loading branch information
cycleccc authored Jan 27, 2025
1 parent 93752fe commit 9b2ffc7
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
100 changes: 100 additions & 0 deletions packages/table-module/__tests__/utils/has-common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* @description has common test
*/

import * as core from '@wangeditor-next/core'
import * as slate from 'slate'

import createEditor from '../../../../tests/utils/create-editor'
import { hasCommon } from '../../src/utils/has-common'

function setEditorSelection(
editor: core.IDomEditor,
selection: slate.Selection = {
anchor: { path: [0, 0], offset: 0 },
focus: { path: [0, 0], offset: 0 },
},
) {
editor.selection = selection
}

describe('hasCommon', () => {
const editor = createEditor()

setEditorSelection(editor)

it('should return true if paths have a common ancestor of type table', () => {
const path1 = [1, 0, 0]
const path2 = [1, 0, 1]

const elem = {
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,
}

editor.insertNode(elem) // 插入 divider
const result = hasCommon(editor, [path1, path2], 'tr')

expect(result).toBe(true)
})

it('should return false if paths do not have a common ancestor of type table', () => {
const path1 = [0]
const path2 = [1, 0, 1]
const result = hasCommon(editor, [path1, path2], 'table')

expect(result).toBe(false)
})
})
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"packages/*/dist"
]
},
"types": [
"vitest/globals"
]
},
"exclude": [
"node_modules",
Expand All @@ -35,6 +38,7 @@
],
"include": [
"./tests/setup/index.ts",
"./packages/custom-types.d.ts"
"./packages/custom-types.d.ts",
"**/__tests__/**/*",
]
}

0 comments on commit 9b2ffc7

Please sign in to comment.