|
| 1 | +/** |
| 2 | + * @description has common test |
| 3 | + */ |
| 4 | + |
| 5 | +import * as core from '@wangeditor-next/core' |
| 6 | +import * as slate from 'slate' |
| 7 | + |
| 8 | +import createEditor from '../../../../tests/utils/create-editor' |
| 9 | +import { hasCommon } from '../../src/utils/has-common' |
| 10 | + |
| 11 | +function setEditorSelection( |
| 12 | + editor: core.IDomEditor, |
| 13 | + selection: slate.Selection = { |
| 14 | + anchor: { path: [0, 0], offset: 0 }, |
| 15 | + focus: { path: [0, 0], offset: 0 }, |
| 16 | + }, |
| 17 | +) { |
| 18 | + editor.selection = selection |
| 19 | +} |
| 20 | + |
| 21 | +describe('hasCommon', () => { |
| 22 | + const editor = createEditor() |
| 23 | + |
| 24 | + setEditorSelection(editor) |
| 25 | + |
| 26 | + it('should return true if paths have a common ancestor of type table', () => { |
| 27 | + const path1 = [1, 0, 0] |
| 28 | + const path2 = [1, 0, 1] |
| 29 | + |
| 30 | + const elem = { |
| 31 | + type: 'table', |
| 32 | + width: 'auto', |
| 33 | + children: [ |
| 34 | + { |
| 35 | + type: 'table-row', |
| 36 | + children: [ |
| 37 | + { |
| 38 | + type: 'table-cell', |
| 39 | + children: [ |
| 40 | + { |
| 41 | + text: '', |
| 42 | + }, |
| 43 | + ], |
| 44 | + isHeader: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + type: 'table-cell', |
| 48 | + children: [ |
| 49 | + { |
| 50 | + text: '', |
| 51 | + }, |
| 52 | + ], |
| 53 | + isHeader: true, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + { |
| 58 | + type: 'table-row', |
| 59 | + children: [ |
| 60 | + { |
| 61 | + type: 'table-cell', |
| 62 | + children: [ |
| 63 | + { |
| 64 | + text: '', |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + { |
| 69 | + type: 'table-cell', |
| 70 | + children: [ |
| 71 | + { |
| 72 | + text: '', |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + ], |
| 79 | + columnWidths: [ |
| 80 | + 60, |
| 81 | + 60, |
| 82 | + ], |
| 83 | + scrollWidth: 120, |
| 84 | + height: 62, |
| 85 | + } |
| 86 | + |
| 87 | + editor.insertNode(elem) // 插入 divider |
| 88 | + const result = hasCommon(editor, [path1, path2], 'tr') |
| 89 | + |
| 90 | + expect(result).toBe(true) |
| 91 | + }) |
| 92 | + |
| 93 | + it('should return false if paths do not have a common ancestor of type table', () => { |
| 94 | + const path1 = [0] |
| 95 | + const path2 = [1, 0, 1] |
| 96 | + const result = hasCommon(editor, [path1, path2], 'table') |
| 97 | + |
| 98 | + expect(result).toBe(false) |
| 99 | + }) |
| 100 | +}) |
0 commit comments