|
| 1 | +/** @jsx jsxt */ |
| 2 | + |
| 3 | +import { jsxt } from '@platejs/test-utils'; |
| 4 | + |
| 5 | +jsxt; |
| 6 | + |
| 7 | +import { createBaseEditor } from '../../lib/editor'; |
| 8 | +import { createBasePlugin } from '../../lib/plugin'; |
| 9 | + |
| 10 | +describe('plate change handlers', () => { |
| 11 | + it('dispatches node change handlers from Plite node change events', () => { |
| 12 | + const onNodeChange = mock(); |
| 13 | + const NodeObserverPlugin = createBasePlugin({ |
| 14 | + handlers: { onNodeChange }, |
| 15 | + key: 'nodeObserver', |
| 16 | + }); |
| 17 | + const editor = createBaseEditor({ |
| 18 | + plugins: [NodeObserverPlugin], |
| 19 | + value: [{ children: [{ text: 'hello' }], type: 'p' }], |
| 20 | + }); |
| 21 | + |
| 22 | + onNodeChange.mockClear(); |
| 23 | + |
| 24 | + editor.update.nodes.set({ variant: 'lead' } as any, { at: [0] }); |
| 25 | + |
| 26 | + expect(onNodeChange).toHaveBeenCalledTimes(1); |
| 27 | + expect(onNodeChange.mock.calls[0]?.[0]).toMatchObject({ |
| 28 | + node: { |
| 29 | + children: [{ text: 'hello' }], |
| 30 | + type: 'p', |
| 31 | + variant: 'lead', |
| 32 | + }, |
| 33 | + operation: { |
| 34 | + path: [0], |
| 35 | + type: 'set_node', |
| 36 | + }, |
| 37 | + prevNode: { |
| 38 | + children: [{ text: 'hello' }], |
| 39 | + type: 'p', |
| 40 | + }, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('dispatches text change handlers from Plite text change events', () => { |
| 45 | + const onTextChange = mock(); |
| 46 | + const TextObserverPlugin = createBasePlugin({ |
| 47 | + handlers: { onTextChange }, |
| 48 | + key: 'textObserver', |
| 49 | + }); |
| 50 | + const editor = createBaseEditor({ |
| 51 | + plugins: [TextObserverPlugin], |
| 52 | + selection: { |
| 53 | + anchor: { offset: 5, path: [0, 0] }, |
| 54 | + focus: { offset: 5, path: [0, 0] }, |
| 55 | + }, |
| 56 | + value: [{ children: [{ text: 'hello' }], type: 'p' }], |
| 57 | + }); |
| 58 | + |
| 59 | + onTextChange.mockClear(); |
| 60 | + |
| 61 | + editor.update.text.insert('!'); |
| 62 | + |
| 63 | + expect(onTextChange).toHaveBeenCalledTimes(1); |
| 64 | + expect(onTextChange.mock.calls[0]?.[0]).toMatchObject({ |
| 65 | + node: { |
| 66 | + children: [{ text: 'hello!' }], |
| 67 | + type: 'p', |
| 68 | + }, |
| 69 | + operation: { |
| 70 | + offset: 5, |
| 71 | + path: [0, 0], |
| 72 | + text: '!', |
| 73 | + type: 'insert_text', |
| 74 | + }, |
| 75 | + prevText: 'hello', |
| 76 | + text: 'hello!', |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
0 commit comments