|
| 1 | +import {describe, expect, it, jest} from '@jest/globals' |
| 2 | +import {render, waitFor} from '@testing-library/react' |
| 3 | +import {createRef, type RefObject} from 'react' |
| 4 | + |
| 5 | +import {PortableTextEditorTester, schemaType} from '../../__tests__/PortableTextEditorTester' |
| 6 | +import {PortableTextEditor} from '../../PortableTextEditor' |
| 7 | + |
| 8 | +const initialValue = [ |
| 9 | + { |
| 10 | + _key: 'a', |
| 11 | + _type: 'myTestBlockType', |
| 12 | + children: [ |
| 13 | + { |
| 14 | + _key: 'a1', |
| 15 | + _type: 'span', |
| 16 | + marks: [], |
| 17 | + text: 'Block A', |
| 18 | + }, |
| 19 | + ], |
| 20 | + markDefs: [], |
| 21 | + style: 'normal', |
| 22 | + }, |
| 23 | + { |
| 24 | + _key: 'b', |
| 25 | + _type: 'myTestBlockType', |
| 26 | + children: [ |
| 27 | + { |
| 28 | + _key: 'b1', |
| 29 | + _type: 'span', |
| 30 | + marks: [], |
| 31 | + text: 'Block B', |
| 32 | + }, |
| 33 | + ], |
| 34 | + markDefs: [], |
| 35 | + style: 'normal', |
| 36 | + }, |
| 37 | +] |
| 38 | + |
| 39 | +const initialSelection = { |
| 40 | + focus: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 7}, |
| 41 | + anchor: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 7}, |
| 42 | +} |
| 43 | + |
| 44 | +describe('plugin:withUndoRedo', () => { |
| 45 | + it('preserves the keys when undoing ', async () => { |
| 46 | + const editorRef: RefObject<PortableTextEditor> = createRef() |
| 47 | + const onChange = jest.fn() |
| 48 | + render( |
| 49 | + <PortableTextEditorTester |
| 50 | + onChange={onChange} |
| 51 | + ref={editorRef} |
| 52 | + schemaType={schemaType} |
| 53 | + value={initialValue} |
| 54 | + />, |
| 55 | + ) |
| 56 | + await waitFor(() => { |
| 57 | + if (editorRef.current) { |
| 58 | + PortableTextEditor.focus(editorRef.current) |
| 59 | + PortableTextEditor.select(editorRef.current, initialSelection) |
| 60 | + PortableTextEditor.delete( |
| 61 | + editorRef.current, |
| 62 | + PortableTextEditor.getSelection(editorRef.current), |
| 63 | + {mode: 'blocks'}, |
| 64 | + ) |
| 65 | + expect(PortableTextEditor.getValue(editorRef.current)).toMatchInlineSnapshot(` |
| 66 | + Array [ |
| 67 | + Object { |
| 68 | + "_key": "a", |
| 69 | + "_type": "myTestBlockType", |
| 70 | + "children": Array [ |
| 71 | + Object { |
| 72 | + "_key": "a1", |
| 73 | + "_type": "span", |
| 74 | + "marks": Array [], |
| 75 | + "text": "Block A", |
| 76 | + }, |
| 77 | + ], |
| 78 | + "markDefs": Array [], |
| 79 | + "style": "normal", |
| 80 | + }, |
| 81 | + ] |
| 82 | + `) |
| 83 | + PortableTextEditor.undo(editorRef.current) |
| 84 | + expect(PortableTextEditor.getValue(editorRef.current)).toEqual(initialValue) |
| 85 | + } |
| 86 | + }) |
| 87 | + }) |
| 88 | + it('preserves the keys when redoing ', async () => { |
| 89 | + const editorRef: RefObject<PortableTextEditor> = createRef() |
| 90 | + const onChange = jest.fn() |
| 91 | + render( |
| 92 | + <PortableTextEditorTester |
| 93 | + onChange={onChange} |
| 94 | + ref={editorRef} |
| 95 | + schemaType={schemaType} |
| 96 | + value={initialValue} |
| 97 | + />, |
| 98 | + ) |
| 99 | + await waitFor(() => { |
| 100 | + if (editorRef.current) { |
| 101 | + PortableTextEditor.focus(editorRef.current) |
| 102 | + PortableTextEditor.select(editorRef.current, initialSelection) |
| 103 | + PortableTextEditor.insertBlock(editorRef.current, editorRef.current.schemaTypes.block, { |
| 104 | + children: [{_key: 'c1', _type: 'span', marks: [], text: 'Block C'}], |
| 105 | + }) |
| 106 | + const producedKey = PortableTextEditor.getValue(editorRef.current)?.slice(-1)[0]?._key |
| 107 | + PortableTextEditor.undo(editorRef.current) |
| 108 | + PortableTextEditor.redo(editorRef.current) |
| 109 | + expect(PortableTextEditor.getValue(editorRef.current)?.slice(-1)[0]?._key).toEqual( |
| 110 | + producedKey, |
| 111 | + ) |
| 112 | + } |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
0 commit comments