@pie-lib/editable-html-tip-tap 1.2.0-next.9 → 2.0.1

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.
Files changed (94) hide show
  1. package/CHANGELOG.md +176 -0
  2. package/lib/components/CharacterPicker.js +1 -0
  3. package/lib/components/CharacterPicker.js.map +1 -1
  4. package/lib/components/EditableHtml.js +84 -43
  5. package/lib/components/EditableHtml.js.map +1 -1
  6. package/lib/components/MenuBar.js +74 -43
  7. package/lib/components/MenuBar.js.map +1 -1
  8. package/lib/components/TiptapContainer.js +9 -8
  9. package/lib/components/TiptapContainer.js.map +1 -1
  10. package/lib/components/icons/TextAlign.js +2 -2
  11. package/lib/components/icons/TextAlign.js.map +1 -1
  12. package/lib/components/image/InsertImageHandler.js +10 -13
  13. package/lib/components/image/InsertImageHandler.js.map +1 -1
  14. package/lib/components/media/MediaDialog.js.map +1 -1
  15. package/lib/components/respArea/DragInTheBlank/DragInTheBlank.js +6 -1
  16. package/lib/components/respArea/DragInTheBlank/DragInTheBlank.js.map +1 -1
  17. package/lib/components/respArea/DragInTheBlank/choice.js +15 -7
  18. package/lib/components/respArea/DragInTheBlank/choice.js.map +1 -1
  19. package/lib/components/respArea/ExplicitConstructedResponse.js +29 -11
  20. package/lib/components/respArea/ExplicitConstructedResponse.js.map +1 -1
  21. package/lib/components/respArea/InlineDropdown.js +35 -6
  22. package/lib/components/respArea/InlineDropdown.js.map +1 -1
  23. package/lib/extensions/custom-toolbar-wrapper.js +3 -2
  24. package/lib/extensions/custom-toolbar-wrapper.js.map +1 -1
  25. package/lib/extensions/div-node.js +83 -0
  26. package/lib/extensions/div-node.js.map +1 -0
  27. package/lib/extensions/ensure-empty-root-div.js +48 -0
  28. package/lib/extensions/ensure-empty-root-div.js.map +1 -0
  29. package/lib/extensions/ensure-list-item-content-is-div.js +64 -0
  30. package/lib/extensions/ensure-list-item-content-is-div.js.map +1 -0
  31. package/lib/extensions/extended-list-item.js +15 -0
  32. package/lib/extensions/extended-list-item.js.map +1 -0
  33. package/lib/extensions/extended-table-cell.js +22 -0
  34. package/lib/extensions/extended-table-cell.js.map +1 -0
  35. package/lib/extensions/extended-table.js +50 -1
  36. package/lib/extensions/extended-table.js.map +1 -1
  37. package/lib/extensions/image-component.js +102 -51
  38. package/lib/extensions/image-component.js.map +1 -1
  39. package/lib/extensions/image.js +51 -2
  40. package/lib/extensions/image.js.map +1 -1
  41. package/lib/extensions/math.js +50 -9
  42. package/lib/extensions/math.js.map +1 -1
  43. package/lib/extensions/media.js +3 -1
  44. package/lib/extensions/media.js.map +1 -1
  45. package/lib/extensions/responseArea.js +12 -7
  46. package/lib/extensions/responseArea.js.map +1 -1
  47. package/lib/styles/editorContainerStyles.js +5 -4
  48. package/lib/styles/editorContainerStyles.js.map +1 -1
  49. package/lib/utils/helper.js +17 -0
  50. package/lib/utils/helper.js.map +1 -0
  51. package/package.json +8 -8
  52. package/src/__tests__/EditableHtml.test.jsx +90 -7
  53. package/src/__tests__/index.test.jsx +11 -3
  54. package/src/components/CharacterPicker.jsx +1 -0
  55. package/src/components/EditableHtml.jsx +91 -41
  56. package/src/components/MenuBar.jsx +57 -24
  57. package/src/components/TiptapContainer.jsx +10 -8
  58. package/src/components/__tests__/CharacterPicker.test.jsx +22 -0
  59. package/src/components/__tests__/ExplicitConstructedResponse.test.jsx +55 -12
  60. package/src/components/__tests__/InlineDropdown.test.jsx +203 -10
  61. package/src/components/__tests__/InsertImageHandler.test.js +28 -21
  62. package/src/components/__tests__/MenuBar.test.jsx +32 -0
  63. package/src/components/icons/TextAlign.jsx +1 -1
  64. package/src/components/image/InsertImageHandler.js +9 -13
  65. package/src/components/respArea/DragInTheBlank/DragInTheBlank.jsx +6 -1
  66. package/src/components/respArea/DragInTheBlank/choice.jsx +32 -4
  67. package/src/components/respArea/ExplicitConstructedResponse.jsx +33 -10
  68. package/src/components/respArea/InlineDropdown.jsx +45 -10
  69. package/src/extensions/__tests__/divNode.test.js +87 -0
  70. package/src/extensions/__tests__/ensure-empty-root-div.test.js +57 -0
  71. package/src/extensions/__tests__/ensure-list-item-content-is-div.test.js +44 -0
  72. package/src/extensions/__tests__/extended-list-item.test.js +13 -0
  73. package/src/extensions/__tests__/extended-table-cell.test.js +22 -0
  74. package/src/extensions/__tests__/extended-table.test.js +98 -1
  75. package/src/extensions/__tests__/image-component.test.jsx +105 -9
  76. package/src/extensions/__tests__/image.test.js +109 -8
  77. package/src/extensions/__tests__/math.test.js +348 -0
  78. package/src/extensions/__tests__/media-node-view.test.jsx +10 -8
  79. package/src/extensions/__tests__/responseArea.test.js +291 -0
  80. package/src/extensions/custom-toolbar-wrapper.jsx +2 -2
  81. package/src/extensions/div-node.js +86 -0
  82. package/src/extensions/ensure-empty-root-div.js +47 -0
  83. package/src/extensions/ensure-list-item-content-is-div.js +62 -0
  84. package/src/extensions/extended-list-item.js +10 -0
  85. package/src/extensions/extended-table-cell.js +19 -0
  86. package/src/extensions/extended-table.js +37 -1
  87. package/src/extensions/image-component.jsx +114 -69
  88. package/src/extensions/image.js +56 -1
  89. package/src/extensions/math.js +62 -10
  90. package/src/extensions/media.js +1 -1
  91. package/src/extensions/responseArea.js +13 -11
  92. package/src/styles/editorContainerStyles.js +5 -4
  93. package/src/utils/helper.js +17 -0
  94. /package/src/components/media/{MediaDialog.js → MediaDialog.jsx} +0 -0
@@ -35,21 +35,36 @@ jest.mock('../custom-toolbar-wrapper', () => ({
35
35
  }));
36
36
 
37
37
  describe('ImageComponent', () => {
38
- const mockEditor = {
38
+ const MOCK_NODE_POS = 7;
39
+
40
+ const createMockEditor = (selection = { from: 0, to: 1 }) => ({
41
+ _tiptapContainerEl: document.body,
39
42
  commands: {
40
43
  updateAttributes: jest.fn(),
41
44
  focus: jest.fn(),
42
45
  },
43
46
  state: {
44
- selection: {
45
- from: 0,
46
- to: 1,
47
+ selection,
48
+ doc: {
49
+ descendants: jest.fn(),
50
+ nodeAt: jest.fn(),
51
+ },
52
+ tr: {
53
+ setNodeMarkup: jest.fn().mockReturnThis(),
54
+ delete: jest.fn().mockReturnThis(),
47
55
  },
48
56
  },
49
- };
57
+ view: {
58
+ dispatch: jest.fn(),
59
+ },
60
+ });
61
+
62
+ let mockEditor = createMockEditor();
50
63
 
51
64
  const mockNode = {
65
+ type: { name: 'imageUploadNode' },
52
66
  attrs: {
67
+ nodeKey: 'test-key-123',
53
68
  src: 'test.jpg',
54
69
  width: 100,
55
70
  height: 100,
@@ -78,10 +93,16 @@ describe('ImageComponent', () => {
78
93
  options: mockOptions,
79
94
  attributes: {},
80
95
  onFocus: jest.fn(),
96
+ getPos: jest.fn(() => MOCK_NODE_POS),
81
97
  };
82
98
 
83
99
  beforeEach(() => {
84
100
  jest.clearAllMocks();
101
+ mockEditor = createMockEditor();
102
+ defaultProps.editor = mockEditor;
103
+ defaultProps.getPos = jest.fn(() => MOCK_NODE_POS);
104
+ mockEditor.state.doc.descendants.mockImplementation((cb) => cb(mockNode, 0));
105
+ mockEditor.state.doc.nodeAt.mockReturnValue(mockNode);
85
106
  });
86
107
 
87
108
  it('renders without crashing', () => {
@@ -184,9 +205,67 @@ describe('ImageComponent', () => {
184
205
  expect(root).toHaveStyle({ justifyContent: 'flex-end' });
185
206
  });
186
207
 
187
- it('calls insertImageRequested on mount', () => {
188
- render(<ImageComponent {...defaultProps} />);
189
- expect(mockOptions.imageHandling.insertImageRequested).toHaveBeenCalled();
208
+ it('does not call insertImageRequested when image already has src', () => {
209
+ render(<ImageComponent {...defaultProps} selected={true} />);
210
+ expect(mockOptions.imageHandling.insertImageRequested).not.toHaveBeenCalled();
211
+ });
212
+
213
+ it('does not call insertImageRequested for empty placeholder on mount when not selected', () => {
214
+ const placeholderNode = {
215
+ ...mockNode,
216
+ attrs: {
217
+ ...mockNode.attrs,
218
+ src: null,
219
+ loaded: false,
220
+ percent: null,
221
+ },
222
+ };
223
+ render(<ImageComponent {...defaultProps} node={placeholderNode} selected={false} />);
224
+ expect(mockOptions.imageHandling.insertImageRequested).not.toHaveBeenCalled();
225
+ });
226
+
227
+ it('calls insertImageRequested with editor and [node, pos] when empty placeholder is solely selected', async () => {
228
+ const placeholderNode = {
229
+ ...mockNode,
230
+ nodeSize: 1,
231
+ attrs: {
232
+ ...mockNode.attrs,
233
+ src: null,
234
+ loaded: false,
235
+ percent: null,
236
+ },
237
+ };
238
+ mockEditor = createMockEditor({ from: 0, to: 1 });
239
+
240
+ render(<ImageComponent {...defaultProps} editor={mockEditor} node={placeholderNode} selected={true} />);
241
+
242
+ await waitFor(() => {
243
+ expect(mockOptions.imageHandling.insertImageRequested).toHaveBeenCalled();
244
+ });
245
+
246
+ expect(mockOptions.imageHandling.insertImageRequested).toHaveBeenCalledWith(
247
+ mockEditor,
248
+ [placeholderNode, MOCK_NODE_POS],
249
+ expect.any(Function),
250
+ );
251
+ expect(defaultProps.getPos).toHaveBeenCalled();
252
+ });
253
+
254
+ it('does not call insertImageRequested when selection spans beyond the image node', () => {
255
+ const placeholderNode = {
256
+ ...mockNode,
257
+ nodeSize: 1,
258
+ attrs: {
259
+ ...mockNode.attrs,
260
+ src: null,
261
+ loaded: false,
262
+ percent: null,
263
+ },
264
+ };
265
+ mockEditor = createMockEditor({ from: 0, to: 5 });
266
+
267
+ render(<ImageComponent {...defaultProps} editor={mockEditor} node={placeholderNode} selected={true} />);
268
+ expect(mockOptions.imageHandling.insertImageRequested).not.toHaveBeenCalled();
190
269
  });
191
270
 
192
271
  it('updates attributes through toolbar onChange', async () => {
@@ -197,7 +276,12 @@ describe('ImageComponent', () => {
197
276
  fireEvent.click(centerButton);
198
277
  });
199
278
 
200
- expect(mockEditor.commands.updateAttributes).toHaveBeenCalledWith('imageUploadNode', { alignment: 'center' });
279
+ expect(mockEditor.state.tr.setNodeMarkup).toHaveBeenCalledWith(
280
+ 0,
281
+ undefined,
282
+ expect.objectContaining({ alignment: 'center' }),
283
+ );
284
+ expect(mockEditor.view.dispatch).toHaveBeenCalled();
201
285
  });
202
286
 
203
287
  it('toolbar is shown when selected', async () => {
@@ -246,4 +330,16 @@ describe('ImageComponent', () => {
246
330
  const img = container.querySelector('img');
247
331
  expect(img).toBeInTheDocument();
248
332
  });
333
+
334
+ it('passes editor to imageHandling onDone when Done is clicked', async () => {
335
+ const { getByTestId } = render(<ImageComponent {...defaultProps} selected={true} />);
336
+
337
+ await waitFor(() => {
338
+ expect(getByTestId('done-button')).toBeInTheDocument();
339
+ });
340
+
341
+ fireEvent.click(getByTestId('done-button'));
342
+
343
+ expect(mockOptions.imageHandling.onDone).toHaveBeenCalledWith(mockEditor);
344
+ });
249
345
  });
@@ -1,10 +1,15 @@
1
- import { ImageUploadNode } from '../image';
2
-
3
1
  jest.mock('@tiptap/core', () => ({
4
2
  Node: { create: jest.fn((config) => config) },
5
3
  mergeAttributes: jest.fn((...args) => Object.assign({}, ...args)),
6
4
  }));
7
5
 
6
+ jest.mock('@tiptap/pm/state', () => ({
7
+ Plugin: jest.fn(function MockPlugin(spec) {
8
+ this.spec = spec;
9
+ return { spec };
10
+ }),
11
+ }));
12
+
8
13
  jest.mock('@tiptap/react', () => ({
9
14
  ReactNodeViewRenderer: jest.fn((component) => component),
10
15
  }));
@@ -14,6 +19,19 @@ jest.mock('../image-component', () => ({
14
19
  default: jest.fn(() => <div data-testid="image-component" />),
15
20
  }));
16
21
 
22
+ import { Plugin } from '@tiptap/pm/state';
23
+ import { ImageUploadNode } from '../image';
24
+
25
+ function setupPastePlugin() {
26
+ const insertContent = jest.fn();
27
+ const editor = { commands: { insertContent } };
28
+ Plugin.mockClear();
29
+ ImageUploadNode.addProseMirrorPlugins.call({ editor });
30
+ expect(Plugin).toHaveBeenCalledTimes(1);
31
+ const handlePaste = Plugin.mock.calls[0][0].props.handlePaste;
32
+ return { handlePaste, insertContent, editor };
33
+ }
34
+
17
35
  describe('ImageUploadNode', () => {
18
36
  describe('configuration', () => {
19
37
  it('has correct name', () => {
@@ -67,7 +85,7 @@ describe('ImageUploadNode', () => {
67
85
 
68
86
  expect(Array.isArray(rules)).toBe(true);
69
87
  expect(rules).toHaveLength(1);
70
- expect(rules[0]).toHaveProperty('tag', 'div[data-type="image-upload-node"]');
88
+ expect(rules[0]).toHaveProperty('tag', 'img[data-type="image-upload-node"]');
71
89
  });
72
90
  });
73
91
 
@@ -112,7 +130,8 @@ describe('ImageUploadNode', () => {
112
130
 
113
131
  describe('addCommands', () => {
114
132
  it('returns setImageUploadNode command', () => {
115
- const commands = ImageUploadNode.addCommands();
133
+ const context = { name: 'imageUploadNode' };
134
+ const commands = ImageUploadNode.addCommands.call(context);
116
135
 
117
136
  expect(commands).toHaveProperty('setImageUploadNode');
118
137
  expect(typeof commands.setImageUploadNode).toBe('function');
@@ -124,13 +143,95 @@ describe('ImageUploadNode', () => {
124
143
  const mockCommands = {
125
144
  insertContent: jest.fn(() => true),
126
145
  };
127
-
128
146
  const result = commands.setImageUploadNode()({ commands: mockCommands });
147
+ expect(mockCommands.insertContent).toHaveBeenCalledWith(
148
+ expect.objectContaining({
149
+ type: 'imageUploadNode',
150
+ attrs: expect.objectContaining({ nodeKey: expect.any(String) }),
151
+ }),
152
+ );
153
+ expect(result).toBe(true);
154
+ });
155
+ });
129
156
 
130
- expect(mockCommands.insertContent).toHaveBeenCalledWith({
131
- type: 'imageUploadNode',
157
+ describe('addProseMirrorPlugins', () => {
158
+ const mockView = {};
159
+
160
+ beforeEach(() => {
161
+ jest.spyOn(global, 'FileReader').mockImplementation(function MockFileReader() {
162
+ this.readAsDataURL = function readAsDataURL() {
163
+ this.result = 'data:image/png;base64,Zm9v';
164
+ queueMicrotask(() => {
165
+ if (this.onload) {
166
+ this.onload();
167
+ }
168
+ });
169
+ };
132
170
  });
133
- expect(result).toBe(true);
171
+ });
172
+
173
+ afterEach(() => {
174
+ global.FileReader.mockRestore();
175
+ });
176
+
177
+ it('registers one paste plugin', () => {
178
+ const insertContent = jest.fn();
179
+ const editor = { commands: { insertContent } };
180
+ Plugin.mockClear();
181
+ const plugins = ImageUploadNode.addProseMirrorPlugins.call({ editor });
182
+ expect(plugins).toHaveLength(1);
183
+ expect(Plugin).toHaveBeenCalledTimes(1);
184
+ });
185
+
186
+ it('handlePaste returns false when clipboard has no image file', () => {
187
+ const { handlePaste } = setupPastePlugin();
188
+ const event = {
189
+ clipboardData: {
190
+ items: [{ kind: 'string', type: 'text/plain', getAsFile: () => null }],
191
+ },
192
+ };
193
+ expect(handlePaste(mockView, event)).toBe(false);
194
+ });
195
+
196
+ it('handlePaste returns false when clipboardData is missing', () => {
197
+ const { handlePaste } = setupPastePlugin();
198
+ const event = {};
199
+ expect(handlePaste(mockView, event)).toBe(false);
200
+ });
201
+
202
+ it('handlePaste returns false when the file item has no file', () => {
203
+ const { handlePaste } = setupPastePlugin();
204
+ const event = {
205
+ clipboardData: {
206
+ items: [{ kind: 'file', type: 'image/png', getAsFile: () => null }],
207
+ },
208
+ };
209
+ expect(handlePaste(mockView, event)).toBe(false);
210
+ });
211
+
212
+ it('handlePaste returns true and inserts imageUploadNode with data URL after read', async () => {
213
+ const { handlePaste, insertContent } = setupPastePlugin();
214
+ const file = new File([new Uint8Array([1, 2, 3])], 'p.png', { type: 'image/png' });
215
+ const event = {
216
+ clipboardData: {
217
+ items: [{ kind: 'file', type: 'image/png', getAsFile: () => file }],
218
+ },
219
+ };
220
+
221
+ expect(handlePaste(mockView, event)).toBe(true);
222
+ expect(insertContent).not.toHaveBeenCalled();
223
+
224
+ await new Promise((resolve) => queueMicrotask(resolve));
225
+
226
+ expect(insertContent).toHaveBeenCalledWith(
227
+ expect.objectContaining({
228
+ type: 'imageUploadNode',
229
+ attrs: expect.objectContaining({
230
+ src: 'data:image/png;base64,Zm9v',
231
+ loaded: true,
232
+ }),
233
+ }),
234
+ );
134
235
  });
135
236
  });
136
237
  });
@@ -0,0 +1,348 @@
1
+ import React from 'react';
2
+ import { render, waitFor, fireEvent } from '@testing-library/react';
3
+ import { MathNode, MathNodeView } from '../math';
4
+
5
+ jest.mock('@tiptap/react', () => ({
6
+ NodeViewWrapper: ({ children, ...props }) => (
7
+ <div data-testid="node-view-wrapper" {...props}>
8
+ {children}
9
+ </div>
10
+ ),
11
+ ReactNodeViewRenderer: jest.fn((component) => component),
12
+ }));
13
+
14
+ jest.mock('react-dom', () => ({
15
+ ...jest.requireActual('react-dom'),
16
+ createPortal: (node) => node,
17
+ }));
18
+
19
+ jest.mock('@pie-lib/math-toolbar', () => {
20
+ const React = require('react');
21
+ return {
22
+ MathPreview: ({ latex }) => <div data-testid="math-preview">{latex}</div>,
23
+ MathToolbar: ({ latex, onChange, onDone }) => {
24
+ const [localLatex, setLocalLatex] = React.useState(latex);
25
+ return (
26
+ <div data-testid="math-toolbar">
27
+ <input
28
+ data-testid="math-input"
29
+ value={localLatex}
30
+ onChange={(e) => {
31
+ setLocalLatex(e.target.value);
32
+ onChange(e.target.value);
33
+ }}
34
+ />
35
+ <button data-testid="done-button" onClick={() => onDone(localLatex)}>
36
+ Done
37
+ </button>
38
+ </div>
39
+ );
40
+ },
41
+ };
42
+ });
43
+
44
+ jest.mock('@pie-lib/math-rendering', () => ({
45
+ wrapMath: (latex, wrapper) => latex,
46
+ }));
47
+
48
+ jest.mock('@tiptap/core', () => ({
49
+ Node: {
50
+ create: jest.fn((config) => config),
51
+ },
52
+ }));
53
+
54
+ jest.mock('prosemirror-state', () => ({
55
+ Plugin: jest.fn(function (config) {
56
+ return config;
57
+ }),
58
+ PluginKey: jest.fn(function (key) {
59
+ this.key = key;
60
+ }),
61
+ TextSelection: {
62
+ create: jest.fn((doc, pos) => ({ type: 'text', pos })),
63
+ },
64
+ NodeSelection: {
65
+ create: jest.fn((doc, pos) => ({ type: 'node', pos })),
66
+ },
67
+ }));
68
+
69
+ describe('MathNode', () => {
70
+ describe('configuration', () => {
71
+ it('has correct name', () => {
72
+ expect(MathNode.name).toBe('math');
73
+ });
74
+
75
+ it('is inline', () => {
76
+ expect(MathNode.inline).toBe(true);
77
+ });
78
+
79
+ it('is in inline group', () => {
80
+ expect(MathNode.group).toBe('inline');
81
+ });
82
+
83
+ it('is atomic', () => {
84
+ expect(MathNode.atom).toBe(true);
85
+ });
86
+ });
87
+
88
+ describe('addAttributes', () => {
89
+ it('returns required attributes', () => {
90
+ const attributes = MathNode.addAttributes();
91
+
92
+ expect(attributes).toHaveProperty('latex');
93
+ expect(attributes).toHaveProperty('wrapper');
94
+ expect(attributes).toHaveProperty('html');
95
+
96
+ expect(attributes.latex).toEqual({ default: '' });
97
+ expect(attributes.wrapper).toEqual({ default: null });
98
+ expect(attributes.html).toEqual({ default: null });
99
+ });
100
+ });
101
+
102
+ describe('parseHTML', () => {
103
+ it('returns parsing rules for latex', () => {
104
+ const rules = MathNode.parseHTML();
105
+
106
+ expect(Array.isArray(rules)).toBe(true);
107
+ expect(rules).toHaveLength(2);
108
+ expect(rules[0]).toHaveProperty('tag', 'span[data-latex]');
109
+ });
110
+
111
+ it('returns parsing rules for mathml', () => {
112
+ const rules = MathNode.parseHTML();
113
+ expect(rules[1]).toHaveProperty('tag', 'span[data-type="mathml"]');
114
+ });
115
+ });
116
+
117
+ describe('renderHTML', () => {
118
+ it('renders mathml when html attribute is present', () => {
119
+ const result = MathNode.renderHTML({
120
+ HTMLAttributes: {
121
+ html: '<math><mi>x</mi></math>',
122
+ },
123
+ });
124
+
125
+ expect(result[0]).toBe('span');
126
+ expect(result[1]).toHaveProperty('data-type', 'mathml');
127
+ });
128
+
129
+ it('renders latex when html attribute is not present', () => {
130
+ const result = MathNode.renderHTML({
131
+ HTMLAttributes: {
132
+ latex: 'x^2',
133
+ },
134
+ });
135
+
136
+ expect(result[0]).toBe('span');
137
+ expect(result[1]).toHaveProperty('data-latex', '');
138
+ expect(result[1]).toHaveProperty('data-raw', 'x^2');
139
+ });
140
+ });
141
+
142
+ describe('addCommands', () => {
143
+ it('returns insertMath command', () => {
144
+ const commands = MathNode.addCommands();
145
+
146
+ expect(commands).toHaveProperty('insertMath');
147
+ expect(typeof commands.insertMath).toBe('function');
148
+ });
149
+ });
150
+
151
+ describe('addNodeView', () => {
152
+ it('returns ReactNodeViewRenderer result', () => {
153
+ const result = MathNode.addNodeView();
154
+
155
+ expect(result).toBeDefined();
156
+ });
157
+ });
158
+ });
159
+
160
+ describe('MathNodeView', () => {
161
+ const createMockEditor = () => ({
162
+ state: {
163
+ selection: {
164
+ from: 0,
165
+ to: 1,
166
+ },
167
+ tr: {
168
+ setSelection: jest.fn().mockReturnThis(),
169
+ },
170
+ doc: {},
171
+ },
172
+ view: {
173
+ coordsAtPos: jest.fn(() => ({ top: 100, left: 50 })),
174
+ dispatch: jest.fn(),
175
+ },
176
+ commands: {
177
+ focus: jest.fn(),
178
+ },
179
+ instanceId: 'editor-123',
180
+ _toolbarOpened: false,
181
+ });
182
+
183
+ const mockNode = {
184
+ attrs: {
185
+ latex: 'x^2',
186
+ },
187
+ };
188
+
189
+ let defaultProps;
190
+
191
+ beforeAll(() => {
192
+ Object.defineProperty(document.body, 'getBoundingClientRect', {
193
+ value: jest.fn(() => ({ top: 0, left: 0 })),
194
+ configurable: true,
195
+ });
196
+ });
197
+
198
+ beforeEach(() => {
199
+ jest.clearAllMocks();
200
+ defaultProps = {
201
+ node: mockNode,
202
+ updateAttributes: jest.fn(),
203
+ editor: createMockEditor(),
204
+ selected: false,
205
+ options: {},
206
+ };
207
+ });
208
+
209
+ it('renders without crashing', () => {
210
+ const { container } = render(<MathNodeView {...defaultProps} />);
211
+ expect(container).toBeInTheDocument();
212
+ });
213
+
214
+ it('renders NodeViewWrapper', () => {
215
+ const { getByTestId } = render(<MathNodeView {...defaultProps} />);
216
+ expect(getByTestId('node-view-wrapper')).toBeInTheDocument();
217
+ });
218
+
219
+ it('displays math preview', () => {
220
+ const { getByTestId } = render(<MathNodeView {...defaultProps} />);
221
+ expect(getByTestId('math-preview')).toBeInTheDocument();
222
+ });
223
+
224
+ it('shows toolbar when selected', async () => {
225
+ const { getByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
226
+ await waitFor(() => {
227
+ expect(getByTestId('math-toolbar')).toBeInTheDocument();
228
+ });
229
+ });
230
+
231
+ it('does not show toolbar when not selected', () => {
232
+ const { queryByTestId } = render(<MathNodeView {...defaultProps} selected={false} />);
233
+ expect(queryByTestId('math-toolbar')).not.toBeInTheDocument();
234
+ });
235
+
236
+ it('adds data-toolbar-for attribute with editor instanceId', async () => {
237
+ const { container } = render(<MathNodeView {...defaultProps} selected={true} />);
238
+ await waitFor(() => {
239
+ const toolbar = container.querySelector('[data-toolbar-for]');
240
+ expect(toolbar).toHaveAttribute('data-toolbar-for', 'editor-123');
241
+ });
242
+ });
243
+
244
+ it('renders toolbar with correct position', async () => {
245
+ const { container } = render(<MathNodeView {...defaultProps} selected={true} />);
246
+ await waitFor(() => {
247
+ const toolbar = container.querySelector('[data-toolbar-for]');
248
+ expect(toolbar).toBeInTheDocument();
249
+ });
250
+ });
251
+
252
+ it('calls updateAttributes when latex changes', async () => {
253
+ const { getByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
254
+ await waitFor(() => {
255
+ const input = getByTestId('math-input');
256
+ fireEvent.change(input, { target: { value: 'y^2' } });
257
+ });
258
+ expect(defaultProps.updateAttributes).toHaveBeenCalledWith({ latex: 'y^2' });
259
+ });
260
+
261
+ it('closes toolbar and updates attributes when done', async () => {
262
+ const updateAttributes = jest.fn();
263
+ const { getByTestId } = render(
264
+ <MathNodeView {...defaultProps} updateAttributes={updateAttributes} selected={true} />,
265
+ );
266
+
267
+ await waitFor(() => {
268
+ expect(getByTestId('done-button')).toBeInTheDocument();
269
+ });
270
+
271
+ const doneButton = getByTestId('done-button');
272
+ fireEvent.click(doneButton);
273
+
274
+ await waitFor(() => {
275
+ expect(updateAttributes).toHaveBeenCalledWith({ latex: 'x^2' });
276
+ });
277
+ });
278
+
279
+ it('sets editor._toolbarOpened when toolbar is shown', async () => {
280
+ const { getByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
281
+ await waitFor(() => {
282
+ expect(getByTestId('math-toolbar')).toBeInTheDocument();
283
+ expect(defaultProps.editor._toolbarOpened).toBe(true);
284
+ });
285
+ });
286
+
287
+ it('unsets editor._toolbarOpened when toolbar is closed', async () => {
288
+ const { getByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
289
+
290
+ await waitFor(() => {
291
+ expect(getByTestId('done-button')).toBeInTheDocument();
292
+ });
293
+
294
+ const doneButton = getByTestId('done-button');
295
+ fireEvent.click(doneButton);
296
+
297
+ await waitFor(() => {
298
+ expect(defaultProps.editor._toolbarOpened).toBe(false);
299
+ });
300
+ });
301
+
302
+ it('closes toolbar on outside click', async () => {
303
+ const { queryByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
304
+
305
+ await waitFor(() => {
306
+ expect(queryByTestId('math-toolbar')).toBeInTheDocument();
307
+ });
308
+
309
+ fireEvent.click(document.body);
310
+
311
+ await waitFor(() => {
312
+ expect(queryByTestId('math-toolbar')).not.toBeInTheDocument();
313
+ });
314
+ });
315
+
316
+ it('does not close toolbar when clicking equation editor dropdown', async () => {
317
+ const { queryByTestId } = render(<MathNodeView {...defaultProps} selected={true} />);
318
+
319
+ await waitFor(() => {
320
+ expect(queryByTestId('math-toolbar')).toBeInTheDocument();
321
+ });
322
+
323
+ // Simulate MUI Select's portal dropdown container.
324
+ const dropdown = document.createElement('div');
325
+ dropdown.id = 'equation-editor-select-listbox';
326
+ document.body.appendChild(dropdown);
327
+
328
+ fireEvent.click(dropdown);
329
+
330
+ await waitFor(() => {
331
+ expect(queryByTestId('math-toolbar')).toBeInTheDocument();
332
+ });
333
+
334
+ document.body.removeChild(dropdown);
335
+ });
336
+
337
+ it('renders with empty latex', () => {
338
+ const nodeWithEmptyLatex = { attrs: { latex: '' } };
339
+ const { getByTestId } = render(<MathNodeView {...defaultProps} node={nodeWithEmptyLatex} />);
340
+ expect(getByTestId('math-preview')).toBeInTheDocument();
341
+ });
342
+
343
+ it('has correct styling on NodeViewWrapper', () => {
344
+ const { getByTestId } = render(<MathNodeView {...defaultProps} />);
345
+ const wrapper = getByTestId('node-view-wrapper');
346
+ expect(wrapper).toHaveStyle({ display: 'inline-flex', cursor: 'pointer' });
347
+ });
348
+ });