-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(portable-text-editor): add tests for getFragment
- Loading branch information
1 parent
111043d
commit b294421
Showing
1 changed file
with
142 additions
and
0 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
...ity/portable-text-editor/src/editor/plugins/__tests__/withEditableAPIGetFragment.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,142 @@ | ||
import {describe, expect, it, jest} from '@jest/globals' | ||
import {isPortableTextTextBlock} from '@sanity/types' | ||
import {render, waitFor} from '@testing-library/react' | ||
import {createRef, type RefObject} from 'react' | ||
|
||
import {PortableTextEditorTester, schemaType} from '../../__tests__/PortableTextEditorTester' | ||
import {PortableTextEditor} from '../../PortableTextEditor' | ||
|
||
const initialValue = [ | ||
{ | ||
_key: 'a', | ||
_type: 'myTestBlockType', | ||
children: [ | ||
{ | ||
_key: 'a1', | ||
_type: 'span', | ||
marks: [], | ||
text: 'Block A', | ||
}, | ||
], | ||
markDefs: [], | ||
style: 'normal', | ||
}, | ||
{ | ||
_key: 'b', | ||
_type: 'myTestBlockType', | ||
children: [ | ||
{ | ||
_key: 'b1', | ||
_type: 'span', | ||
marks: [], | ||
text: 'Block B ', | ||
}, | ||
{ | ||
_key: 'b2', | ||
_type: 'someObject', | ||
}, | ||
{ | ||
_key: 'b3', | ||
_type: 'span', | ||
marks: [], | ||
text: ' contains a inline object', | ||
}, | ||
], | ||
markDefs: [], | ||
style: 'normal', | ||
}, | ||
] | ||
|
||
describe('plugin:withEditableAPI: .getFragment()', () => { | ||
it('can get a Portable Text fragment of the current selection in a single block', async () => { | ||
const editorRef: RefObject<PortableTextEditor> = createRef() | ||
const onChange = jest.fn() | ||
render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/>, | ||
) | ||
const initialSelection = { | ||
focus: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 6}, | ||
anchor: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 7}, | ||
} | ||
await waitFor(() => { | ||
if (editorRef.current) { | ||
PortableTextEditor.focus(editorRef.current) | ||
PortableTextEditor.select(editorRef.current, initialSelection) | ||
const fragment = PortableTextEditor.getFragment(editorRef.current) | ||
expect( | ||
fragment && isPortableTextTextBlock(fragment[0]) && fragment[0]?.children[0]?.text, | ||
).toBe('A') | ||
} | ||
}) | ||
}) | ||
it('can get a Portable Text fragment of the current selection in multiple blocks', async () => { | ||
const editorRef: RefObject<PortableTextEditor> = createRef() | ||
const onChange = jest.fn() | ||
render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/>, | ||
) | ||
const initialSelection = { | ||
anchor: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 6}, | ||
focus: {path: [{_key: 'b'}, 'children', {_key: 'b3'}], offset: 9}, | ||
} | ||
await waitFor(() => { | ||
if (editorRef.current) { | ||
PortableTextEditor.focus(editorRef.current) | ||
PortableTextEditor.select(editorRef.current, initialSelection) | ||
const fragment = PortableTextEditor.getFragment(editorRef.current) | ||
expect(fragment).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"_key": "a", | ||
"_type": "myTestBlockType", | ||
"children": Array [ | ||
Object { | ||
"_key": "a1", | ||
"_type": "span", | ||
"marks": Array [], | ||
"text": "A", | ||
}, | ||
], | ||
"markDefs": Array [], | ||
"style": "normal", | ||
}, | ||
Object { | ||
"_key": "b", | ||
"_type": "myTestBlockType", | ||
"children": Array [ | ||
Object { | ||
"_key": "b1", | ||
"_type": "span", | ||
"marks": Array [], | ||
"text": "Block B ", | ||
}, | ||
Object { | ||
"_key": "b2", | ||
"_type": "someObject", | ||
}, | ||
Object { | ||
"_key": "b3", | ||
"_type": "span", | ||
"marks": Array [], | ||
"text": " contains", | ||
}, | ||
], | ||
"markDefs": Array [], | ||
"style": "normal", | ||
}, | ||
] | ||
`) | ||
} | ||
}) | ||
}) | ||
}) |