@pie-lib/editable-html 13.1.0-beta.2 → 13.1.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,401 +0,0 @@
1
- import EditTable from 'slate-edit-table';
2
- import TablePlugin, { parseStyleString, reactAttributes, serialization } from '../index';
3
- import { Block, Data, PathUtils } from 'slate';
4
- import React from 'react';
5
-
6
- jest.mock('slate-edit-table', () => {
7
- const mock = {
8
- default: jest.fn().mockReturnThis(),
9
- utils: {
10
- isSelectionInTable: jest.fn().mockReturnValue(true),
11
- createTable: jest.fn(function (c) {
12
- return {
13
- toJSON: jest.fn().mockReturnValue({ object: 'block', type: 'table' }),
14
- };
15
- }),
16
- },
17
- changes: {
18
- insertTable: jest.fn(function (c) {
19
- return c;
20
- }),
21
- },
22
- };
23
-
24
- return jest.fn().mockReturnValue(mock);
25
- });
26
-
27
- describe('table', () => {
28
- describe('toolbar', () => {
29
- describe('onClick', () => {
30
- let plugin;
31
- let onChange;
32
- let findDescendant;
33
- let insertBlock;
34
- let changeMock;
35
-
36
- beforeEach(() => {
37
- plugin = TablePlugin();
38
- onChange = jest.fn();
39
- insertBlock = jest.fn();
40
- findDescendant = jest.fn();
41
- changeMock = jest.fn().mockReturnValue({
42
- insertBlock,
43
- value: {
44
- document: {
45
- findDescendant,
46
- },
47
- },
48
- });
49
- });
50
-
51
- it('calls utils.createTable', () => {
52
- plugin.toolbar.onClick({ change: changeMock }, onChange);
53
-
54
- expect(EditTable().utils.createTable).toHaveBeenCalledWith(2, 2);
55
- });
56
-
57
- it('creates table with border: 1 as default', () => {
58
- plugin.toolbar.onClick({ change: changeMock }, onChange);
59
-
60
- expect(insertBlock).toHaveBeenCalledWith(
61
- Block.create({ key: '2', type: 'table', data: { border: '1', newTable: true } }),
62
- );
63
- });
64
-
65
- it('moves cursor to first cell of the table and removes the newTable property', () => {
66
- const dataRemoveFn = jest.fn().mockReturnValue({ border: '1' });
67
- const setNodeByKey = jest.fn();
68
- const newTableBlock = {
69
- key: '2',
70
- data: {
71
- remove: dataRemoveFn,
72
- },
73
- };
74
- const collapseToStartOf = jest.fn();
75
-
76
- changeMock = jest.fn().mockReturnValue({
77
- insertBlock,
78
- collapseToStartOf,
79
- setNodeByKey,
80
- value: {
81
- document: {
82
- findDescendant: jest.fn().mockReturnValue(newTableBlock),
83
- },
84
- },
85
- });
86
- plugin.toolbar.onClick({ change: changeMock }, onChange);
87
-
88
- expect(collapseToStartOf).toHaveBeenCalledWith(newTableBlock);
89
- expect(dataRemoveFn).toHaveBeenCalledWith('newTable');
90
- expect(setNodeByKey).toHaveBeenCalledWith('2', { data: { border: '1' } });
91
- });
92
- });
93
-
94
- describe('supports', () => {
95
- const assertSupports = (inTable, object, expected) => {
96
- it(`inTable: ${inTable}, type: ${object} => ${expected}`, () => {
97
- EditTable().utils.isSelectionInTable.mockReturnValue(inTable);
98
- const plugin = TablePlugin();
99
- const supports = plugin.toolbar.supports({ object });
100
- expect(supports).toEqual(expected);
101
- });
102
- };
103
-
104
- assertSupports(true, 'block', true);
105
- assertSupports(false, 'block', false);
106
- assertSupports(true, 'inline', false);
107
- assertSupports(false, 'inline', false);
108
- });
109
-
110
- describe('customToolbar', () => {
111
- describe('toggleBorder', () => {
112
- const assertToggle = (border, expectedBorder) => {
113
- describe(`with initial border of ${border}`, () => {
114
- let change;
115
- let done;
116
-
117
- beforeEach(() => {
118
- const plugin = TablePlugin();
119
- change = {
120
- setNodeByKey: jest.fn().mockReturnThis(),
121
- };
122
- plugin.utils.getTableBlock = jest.fn().mockReturnValue({
123
- key: 'tableKey',
124
- data: Data.create({ border }),
125
- });
126
- const node = { key: 'nodeKey' };
127
- const value = {
128
- change: jest.fn().mockReturnValue(change),
129
- };
130
- done = jest.fn();
131
- const Comp = plugin.toolbar.customToolbar(node, value, done);
132
- const c = Comp();
133
- c.props.onToggleBorder();
134
- });
135
-
136
- it(`calls setNodeByKey with ${expectedBorder}`, () => {
137
- expect(change.setNodeByKey).toHaveBeenCalledWith('tableKey', {
138
- data: Data.create({ border: expectedBorder }),
139
- });
140
- });
141
-
142
- it('calls onToolbarDone', () => {
143
- expect(done).toHaveBeenCalledWith(change, false);
144
- });
145
- });
146
- };
147
-
148
- assertToggle(null, '1');
149
- assertToggle(undefined, '1');
150
- assertToggle('0', '1');
151
- assertToggle('1', '0');
152
- assertToggle('2', '0');
153
- });
154
- });
155
-
156
- describe('normalizeNode', () => {
157
- it('should exit the function if the node is not of type document', () => {
158
- const tablePlugin = TablePlugin();
159
- const returnValue = tablePlugin.normalizeNode({ object: 'table' });
160
-
161
- expect(returnValue).toEqual(undefined);
162
- });
163
-
164
- it('should exit there are no changes needed', () => {
165
- const tablePlugin = TablePlugin();
166
- const nodes = {
167
- size: 3,
168
- findLastIndex: jest.fn().mockReturnValue(1),
169
- };
170
- const returnValue = tablePlugin.normalizeNode({
171
- object: 'document',
172
- nodes,
173
- findDescendant: jest.fn().mockReturnValue({ object: 'block', type: 'table' }),
174
- getParent: jest.fn().mockReturnValue(undefined),
175
- });
176
- expect(returnValue).toEqual(undefined);
177
- });
178
-
179
- it('should return a function if a table does not have a node before or after it', () => {
180
- const tablePlugin = TablePlugin();
181
- const nodes = [
182
- {
183
- object: 'block',
184
- type: 'table',
185
- key: '99',
186
- data: Data.create({
187
- newTable: true,
188
- }),
189
- },
190
- ];
191
- const findDescendant = jest.fn((callback) => {
192
- nodes.forEach((n) => callback(n));
193
- });
194
- const change = {
195
- insertNodeByPath: jest.fn(),
196
- value: {
197
- document: {
198
- getPath: jest.fn().mockReturnValue(PathUtils.create([0, 0])),
199
- },
200
- },
201
- };
202
-
203
- const returnValue = tablePlugin.normalizeNode({
204
- object: 'document',
205
- findDescendant,
206
- getPath: jest.fn().mockReturnValue(PathUtils.create([0, 0])),
207
- getPreviousNode: jest.fn().mockReturnValue(undefined),
208
- getNextNode: jest.fn().mockReturnValue(undefined),
209
- });
210
-
211
- expect(returnValue).toEqual(expect.any(Function));
212
-
213
- returnValue(change);
214
-
215
- expect(change.insertNodeByPath).toHaveBeenCalledWith([0], 0, {
216
- object: 'block',
217
- type: 'div',
218
- });
219
- });
220
- });
221
- });
222
- });
223
-
224
- describe('parseStyleString', () => {
225
- const parses = (s, expected) => {
226
- it(`parses ${s} -> ${JSON.stringify(expected)}`, () => {
227
- const result = parseStyleString(s);
228
- expect(result).toEqual(expected);
229
- });
230
- };
231
- parses(' width: 10px ', { width: '10px' });
232
- parses(' width: 10px; ', { width: '10px' });
233
- parses(' border-width: 10px; ', { 'border-width': '10px' });
234
- parses(' border: solid 1px red; height: 1px', {
235
- border: 'solid 1px red',
236
- height: '1px',
237
- });
238
- });
239
-
240
- describe('toStyleString', () => {
241
- const styleString = (o, expected) => {
242
- it(`${JSON.stringify(s)} -> ${expected}`, () => {
243
- const result = toStyleString(o);
244
- expect(result).toEqual(expected);
245
- });
246
- };
247
- });
248
-
249
- describe('reactAttributes', () => {
250
- const attributes = (o, expected) => {
251
- it(`${JSON.stringify(o)} -> ${JSON.stringify(expected)}`, () => {
252
- const result = reactAttributes(o);
253
- expect(result).toEqual(expected);
254
- });
255
- };
256
- attributes({ 'border-width': '10px', 'line-height': 1.56 }, { borderWidth: '10px', lineHeight: '1.56' });
257
- });
258
-
259
- describe('serialization', () => {
260
- describe('deserialize', () => {
261
- let next;
262
-
263
- beforeEach(() => {
264
- next = jest.fn().mockReturnValue([]);
265
- });
266
-
267
- describe('table', () => {
268
- let el;
269
- let out;
270
- beforeEach(() => {
271
- el = {
272
- tagName: 'table',
273
- children: [],
274
- getAttribute: jest.fn((name) => {
275
- switch (name) {
276
- case 'border':
277
- return '1';
278
- case 'cellspacing':
279
- return '2';
280
- case 'cellpadding':
281
- return '3';
282
- case 'class':
283
- return 'table-class';
284
- case 'style':
285
- return 'width: 10px';
286
- }
287
- }),
288
- };
289
- out = serialization.deserialize(el, next);
290
- });
291
-
292
- const assertData = (name, value) => {
293
- it(`data.${name} should eql ${value}`, () => {
294
- expect(out.data[name]).toEqual(value);
295
- });
296
- };
297
-
298
- assertData('border', '1');
299
- assertData('cellspacing', '2');
300
- assertData('cellpadding', '3');
301
- assertData('class', 'table-class');
302
- assertData('style', { width: '10px' });
303
- it('returns a table block', () => {
304
- expect(out).toMatchObject({
305
- object: 'block',
306
- type: 'table',
307
- nodes: [],
308
- });
309
- });
310
- });
311
-
312
- it('deserializes tr', () => {
313
- const el = {
314
- tagName: 'tr',
315
- children: [],
316
- };
317
-
318
- const out = serialization.deserialize(el, next);
319
-
320
- expect(out).toEqual({
321
- object: 'block',
322
- type: 'table_row',
323
- nodes: [],
324
- });
325
- });
326
-
327
- it('deserializes td', () => {
328
- const el = {
329
- tagName: 'td',
330
- childNodes: [],
331
- getAttribute: jest.fn(function (name) {
332
- const o = {
333
- class: 'class name',
334
- colspan: '1',
335
- rowspan: '1',
336
- };
337
- return o[name];
338
- }),
339
- };
340
-
341
- const out = serialization.deserialize(el, next);
342
-
343
- expect(out).toEqual({
344
- object: 'block',
345
- type: 'table_cell',
346
- nodes: [],
347
- data: {
348
- colspan: '1',
349
- rowspan: '1',
350
- class: 'class name',
351
- header: false,
352
- },
353
- });
354
- });
355
- });
356
-
357
- describe('serialize', () => {
358
- it('serializes table', () => {
359
- const el = serialization.serialize({
360
- object: 'block',
361
- type: 'table',
362
- nodes: [],
363
- data: Data.create({
364
- border: '1',
365
- cellpadding: '2',
366
- cellspacing: '3',
367
- }),
368
- });
369
-
370
- expect(el).toEqual(
371
- <table border="1" cellPadding="2" cellSpacing="3">
372
- <tbody />
373
- </table>,
374
- );
375
- });
376
-
377
- it('serializes table_row', () => {
378
- const el = serialization.serialize({
379
- object: 'block',
380
- type: 'table_row',
381
- nodes: [],
382
- });
383
-
384
- expect(el).toEqual(<tr />);
385
- });
386
- it('serializes table_cell', () => {
387
- const el = serialization.serialize({
388
- object: 'block',
389
- type: 'table_cell',
390
- nodes: [],
391
- data: Data.create({
392
- header: false,
393
- style: { width: '10px' },
394
- class: 'foo',
395
- }),
396
- });
397
-
398
- expect(el).toEqual(<td style={{ width: '10px' }} className={'foo'} />);
399
- });
400
- });
401
- });
@@ -1,75 +0,0 @@
1
- import React from 'react';
2
- import { render, screen } from '@testing-library/react';
3
- import { TableToolbar } from '../table-toolbar';
4
-
5
- describe('table-toolbar', () => {
6
- let onDone, onAddColumn, onAddRow, onRemoveColumn, onRemoveRow, onRemoveTable, onToggleBorder;
7
-
8
- beforeEach(() => {
9
- onAddColumn = jest.fn();
10
- onAddRow = jest.fn();
11
- onRemoveColumn = jest.fn();
12
- onRemoveRow = jest.fn();
13
- onRemoveTable = jest.fn();
14
- onDone = jest.fn();
15
- onToggleBorder = jest.fn();
16
- });
17
-
18
- const renderComponent = (extras = {}) => {
19
- const props = {
20
- onDone,
21
- onAddRow,
22
- onRemoveRow,
23
- onAddColumn,
24
- onRemoveColumn,
25
- onRemoveTable,
26
- hasBorder: true,
27
- onToggleBorder,
28
- classes: {
29
- tableToolbar: 'table-toolbar',
30
- },
31
- plugins: [],
32
- value: {},
33
- onChange: jest.fn(),
34
- ...extras,
35
- };
36
-
37
- return render(<TableToolbar {...props} />);
38
- };
39
-
40
- describe('rendering', () => {
41
- it('renders toolbar without crashing', () => {
42
- const { container } = renderComponent();
43
-
44
- // Check that the toolbar container is rendered
45
- expect(container.firstChild).toBeInTheDocument();
46
-
47
- // Check for Done button (only one with aria-label)
48
- expect(screen.getByLabelText('Done')).toBeInTheDocument();
49
-
50
- // Check that there are multiple buttons (table manipulation buttons)
51
- const buttons = container.querySelectorAll('button');
52
- expect(buttons.length).toBeGreaterThan(1);
53
- });
54
-
55
- it('renders with hasBorder prop', () => {
56
- const { container } = renderComponent({ hasBorder: false });
57
-
58
- expect(container.firstChild).toBeInTheDocument();
59
-
60
- // Verify hasBorder affects rendering - button with aria-pressed attribute
61
- const borderButton = container.querySelector('button[aria-pressed]');
62
- expect(borderButton).toBeInTheDocument();
63
- expect(borderButton).toHaveAttribute('aria-pressed', 'false');
64
- });
65
-
66
- it('renders with hasBorder true', () => {
67
- const { container } = renderComponent({ hasBorder: true });
68
-
69
- // Verify hasBorder affects rendering
70
- const borderButton = container.querySelector('button[aria-pressed]');
71
- expect(borderButton).toBeInTheDocument();
72
- expect(borderButton).toHaveAttribute('aria-pressed', 'true');
73
- });
74
- });
75
- });
@@ -1,123 +0,0 @@
1
- import React from 'react';
2
- import { fireEvent, render, screen } from '@testing-library/react';
3
- import { Value } from 'slate';
4
- import { DefaultToolbar } from '../default-toolbar';
5
-
6
- // Mock the DoneButton component directly to avoid styled component issues
7
- jest.mock('../done-button', () => ({
8
- DoneButton: (props) => (
9
- <button aria-label="Done" onClick={props.onClick} data-testid="done-button">
10
- Done
11
- </button>
12
- ),
13
- RawDoneButton: (props) => (
14
- <button aria-label="Done" onClick={props.onClick} className="RawDoneButton">
15
- Done
16
- </button>
17
- ),
18
- }));
19
-
20
- describe('default-toolbar', () => {
21
- let onDone;
22
- let onChange;
23
- let getFocusedValue;
24
- let defaultProps;
25
-
26
- beforeEach(() => {
27
- onDone = jest.fn();
28
- onChange = jest.fn();
29
- getFocusedValue = jest.fn();
30
-
31
- defaultProps = {
32
- classes: {},
33
- value: Value.fromJSON({}),
34
- plugins: [],
35
- className: 'className',
36
- onDone,
37
- onChange,
38
- getFocusedValue,
39
- deletable: false,
40
- showDone: true,
41
- };
42
- });
43
-
44
- describe('rendering', () => {
45
- it('renders toolbar', () => {
46
- const { container } = render(<DefaultToolbar {...defaultProps} deletable={false} />);
47
- expect(container.firstChild).toBeInTheDocument();
48
- });
49
-
50
- it('renders with 1 plugin', () => {
51
- const { container } = render(
52
- <DefaultToolbar {...defaultProps} plugins={[{ toolbar: { icon: <span>Icon1</span> }, name: 'plugin-one' }]} />,
53
- );
54
-
55
- expect(container.firstChild).toBeInTheDocument();
56
- // Should render plugin toolbar button
57
- });
58
-
59
- it('renders with 2 plugins', () => {
60
- const { container } = render(
61
- <DefaultToolbar
62
- {...defaultProps}
63
- plugins={[
64
- { toolbar: { icon: <span>Icon1</span> }, name: 'plugin-one' },
65
- { toolbar: { icon: <span>Icon2</span> }, name: 'plugin-two' },
66
- ]}
67
- />,
68
- );
69
-
70
- expect(container.firstChild).toBeInTheDocument();
71
- // Should render both plugin toolbar buttons
72
- });
73
-
74
- it('renders only enabled plugins when one is disabled', () => {
75
- const { container } = render(
76
- <DefaultToolbar
77
- {...defaultProps}
78
- pluginProps={{
79
- 'plugin-one': {
80
- disabled: true,
81
- },
82
- }}
83
- plugins={[
84
- { toolbar: { icon: <span>Icon1</span> }, name: 'plugin-one' },
85
- { toolbar: { icon: <span>Icon2</span> }, name: 'plugin-two' },
86
- ]}
87
- />,
88
- );
89
-
90
- expect(container.firstChild).toBeInTheDocument();
91
- // Only plugin-two should be rendered (plugin-one is disabled)
92
- });
93
-
94
- it('renders done button when not deletable', () => {
95
- render(<DefaultToolbar {...defaultProps} deletable={false} />);
96
-
97
- expect(screen.getByLabelText('Done')).toBeInTheDocument();
98
- });
99
-
100
- it('does not render done button when deletable', () => {
101
- render(<DefaultToolbar {...defaultProps} deletable={true} />);
102
-
103
- expect(screen.queryByLabelText('Done')).not.toBeInTheDocument();
104
- });
105
-
106
- it('does not render done button when showDone is false', () => {
107
- render(<DefaultToolbar {...defaultProps} showDone={false} deletable={false} />);
108
-
109
- expect(screen.queryByLabelText('Done')).not.toBeInTheDocument();
110
- });
111
- });
112
-
113
- describe('interactions', () => {
114
- it('calls onDone when done button is clicked', async () => {
115
- render(<DefaultToolbar {...defaultProps} deletable={false} />);
116
-
117
- const doneButton = screen.getByLabelText('Done');
118
- fireEvent.click(doneButton);
119
-
120
- expect(onDone).toHaveBeenCalled();
121
- });
122
- });
123
- });
@@ -1,40 +0,0 @@
1
- import { classObject, mockIconButton } from '../../../__tests__/utils';
2
-
3
- import { Value } from 'slate';
4
- import { EditorAndToolbar } from '../editor-and-toolbar';
5
- import React from 'react';
6
- import debug from 'debug';
7
- import { render, screen } from '@testing-library/react';
8
-
9
- jest.mock('../toolbar', () => () => <div>---- toolbar ------ </div>);
10
-
11
- mockIconButton();
12
-
13
- const log = debug('@pie-lib:editable-html:test:editor-and-toolbar');
14
-
15
- describe('toolbar', () => {
16
- let onDelete, classes;
17
-
18
- beforeEach(() => {
19
- onDelete = jest.fn();
20
- classes = classObject('root', 'editorHolder', 'editorInFocus');
21
- });
22
-
23
- it('renders EditorAndToolbar component', () => {
24
- const value = Value.fromJSON({});
25
- Object.defineProperty(value, 'isFocused', { get: jest.fn(() => true) });
26
-
27
- const { container } = render(
28
- <EditorAndToolbar classes={classes} value={value} plugins={[]} onDone={jest.fn()} onChange={jest.fn()}>
29
- children
30
- </EditorAndToolbar>,
31
- );
32
-
33
- // Verify component rendered
34
- expect(container.firstChild).toBeInTheDocument();
35
- // Verify toolbar is present
36
- expect(screen.getByText('---- toolbar ------')).toBeInTheDocument();
37
- // Verify children are rendered
38
- expect(screen.getByText('children')).toBeInTheDocument();
39
- });
40
- });