-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(portable-text-editor): fix sync issue with text spans (#4467)
* fix(portable-text-editor): fix sync issue with text spans When a span text is changed, it can't be updated only using setNodes on the node itself. Use Slate delete and insertText for this. * test(portable-text-editor): add some syncValue tests
- Loading branch information
1 parent
97b923e
commit 036e68a
Showing
2 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
packages/@sanity/portable-text-editor/src/editor/hooks/useSyncValue.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* \@jest-environment ./test/setup/jsdom.jest.env.ts | ||
*/ | ||
/* eslint-disable no-irregular-whitespace */ | ||
// eslint-disable-next-line import/no-unassigned-import | ||
import '@testing-library/jest-dom/extend-expect' | ||
import {render, waitFor} from '@testing-library/react' | ||
|
||
import React from 'react' | ||
import {PortableTextEditor} from '../PortableTextEditor' | ||
import {PortableTextEditorTester, schemaType} from '../__tests__/PortableTextEditorTester' | ||
|
||
const initialValue = [ | ||
{ | ||
_key: '77071c3af231', | ||
_type: 'myTestBlockType', | ||
children: [ | ||
{ | ||
_key: 'c001f0e92c1f0', | ||
_type: 'span', | ||
marks: [], | ||
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', | ||
}, | ||
], | ||
markDefs: [], | ||
style: 'normal', | ||
}, | ||
] | ||
|
||
describe('useSyncValue', () => { | ||
it('updates span text', async () => { | ||
const editorRef: React.RefObject<PortableTextEditor> = React.createRef() | ||
const onChange = jest.fn() | ||
const syncedValue = [ | ||
{ | ||
_key: '77071c3af231', | ||
_type: 'myTestBlockType', | ||
children: [ | ||
{ | ||
_key: 'c001f0e92c1f0', | ||
_type: 'span', | ||
marks: [], | ||
text: 'Lorem my ipsum!', | ||
}, | ||
], | ||
markDefs: [], | ||
style: 'normal', | ||
}, | ||
] | ||
const {rerender} = render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/> | ||
) | ||
rerender( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={syncedValue} | ||
/> | ||
) | ||
await waitFor(() => { | ||
if (editorRef.current) { | ||
expect(PortableTextEditor.getValue(editorRef.current)).toEqual(syncedValue) | ||
} | ||
}) | ||
}) | ||
it('replaces span nodes with different keys inside the same children array', async () => { | ||
const editorRef: React.RefObject<PortableTextEditor> = React.createRef() | ||
const onChange = jest.fn() | ||
const syncedValue = [ | ||
{ | ||
_key: '77071c3af231', | ||
_type: 'myTestBlockType', | ||
children: [ | ||
{ | ||
_key: 'c001f0e92c1f0__NEW_KEY_YA!', | ||
_type: 'span', | ||
marks: [], | ||
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', | ||
}, | ||
], | ||
markDefs: [], | ||
style: 'normal', | ||
}, | ||
] | ||
const {rerender} = render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/> | ||
) | ||
rerender( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={syncedValue} | ||
/> | ||
) | ||
await waitFor(() => { | ||
if (editorRef.current) { | ||
expect(PortableTextEditor.getValue(editorRef.current)).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"_key": "77071c3af231", | ||
"_type": "myTestBlockType", | ||
"children": Array [ | ||
Object { | ||
"_key": "c001f0e92c1f0__NEW_KEY_YA!", | ||
"_type": "span", | ||
"marks": Array [], | ||
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ", | ||
}, | ||
], | ||
"markDefs": Array [], | ||
"style": "normal", | ||
}, | ||
] | ||
`) | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
036e68a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
performance-studio – ./
performance-studio.sanity.build
performance-studio-git-next.sanity.build
036e68a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
test-studio – ./
test-studio.sanity.build
test-studio-git-next.sanity.build