-
Notifications
You must be signed in to change notification settings - Fork 995
Expand file tree
/
Copy pathonMouseDownTable.spec.tsx
More file actions
127 lines (110 loc) · 3.23 KB
/
Copy pathonMouseDownTable.spec.tsx
File metadata and controls
127 lines (110 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/** @jsx jsxt */
import { type SlateEditor, createSlateEditor } from 'platejs';
import { jsxt } from '@platejs/test-utils';
import { getTestTablePlugins } from '../lib/__tests__/getTestTablePlugins';
import { onMouseDownTable } from './onMouseDownTable';
jsxt;
const createTableEditor = (input: SlateEditor) =>
createSlateEditor({
nodeId: true,
plugins: getTestTablePlugins(),
selection: input.selection,
value: input.children,
});
const setRect = (element: HTMLElement, rect: Partial<DOMRect>) => {
element.getBoundingClientRect = () =>
({
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
x: 0,
y: 0,
toJSON: () => rect,
...rect,
}) as DOMRect;
};
describe('onMouseDownTable', () => {
it('selects a Slate point when a click lands on the editable root beside a table', () => {
const input = (
<editor>
<htable>
<htr>
<htd>
<hp>11</hp>
</htd>
</htr>
</htable>
<hp>after</hp>
</editor>
) as any as SlateEditor;
const editor = createTableEditor(input);
const editable = document.createElement('div');
const tableDom = document.createElement('table');
const paragraphDom = document.createElement('p');
const preventDefault = mock();
const stopPropagation = mock();
editable.setAttribute('data-slate-editor', 'true');
editable.setAttribute('data-slate-node', 'value');
editable.append(tableDom, paragraphDom);
setRect(tableDom, { bottom: 40, top: 0 });
setRect(paragraphDom, { bottom: 80, top: 41 });
spyOn(editor.api, 'toDOMNode').mockImplementation((node) =>
node === editor.children[0] ? tableDom : paragraphDom
);
onMouseDownTable({
editor,
event: {
button: 0,
clientY: 30,
currentTarget: editable,
defaultPrevented: false,
preventDefault,
stopPropagation,
target: editable,
},
} as any);
expect(preventDefault).toHaveBeenCalledTimes(1);
expect(stopPropagation).toHaveBeenCalledTimes(1);
expect(editor.selection).toEqual({
anchor: { offset: 2, path: [0, 0, 0, 0, 0] },
focus: { offset: 2, path: [0, 0, 0, 0, 0] },
});
});
it('ignores clicks that already target a mapped Slate node', () => {
const input = (
<editor>
<htable>
<htr>
<htd>
<hp>11</hp>
</htd>
</htr>
</htable>
</editor>
) as any as SlateEditor;
const editor = createTableEditor(input);
const editable = document.createElement('div');
const mappedNode = document.createElement('div');
const preventDefault = mock();
editable.setAttribute('data-slate-editor', 'true');
mappedNode.setAttribute('data-slate-node', 'element');
editable.append(mappedNode);
onMouseDownTable({
editor,
event: {
button: 0,
clientY: 20,
currentTarget: editable,
defaultPrevented: false,
preventDefault,
stopPropagation: mock(),
target: mappedNode,
},
} as any);
expect(preventDefault).not.toHaveBeenCalled();
expect(editor.selection).toEqual(input.selection);
});
});