-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pte): create new text blocks if needed (#6560)
* feat(pte): create new text blocks if needed * chore(pte): add tests
- Loading branch information
1 parent
3b3125c
commit cadd496
Showing
6 changed files
with
554 additions
and
3 deletions.
There are no files selected for viewing
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
218 changes: 218 additions & 0 deletions
218
packages/@sanity/portable-text-editor/src/editor/__tests__/handleClick.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,218 @@ | ||
import {describe, expect, it, jest} from '@jest/globals' | ||
import {fireEvent, render, waitFor} from '@testing-library/react' | ||
import {createRef, type RefObject} from 'react' | ||
|
||
import {PortableTextEditor} from '../PortableTextEditor' | ||
import {PortableTextEditorTester, schemaType} from './PortableTextEditorTester' | ||
import {getEditableElement} from './utils' | ||
|
||
describe('adds empty text block if its needed', () => { | ||
const newBlock = { | ||
_type: 'myTestBlockType', | ||
_key: '3', | ||
style: 'normal', | ||
markDefs: [], | ||
children: [ | ||
{ | ||
_type: 'span', | ||
_key: '2', | ||
text: '', | ||
marks: [], | ||
}, | ||
], | ||
} | ||
it('adds a new block at the bottom, when clicking on the portable text editor, because the only block is void and user is focused on that one', async () => { | ||
const initialValue = [ | ||
{ | ||
_key: 'b', | ||
_type: 'someObject', | ||
}, | ||
] | ||
|
||
const initialSelection = { | ||
focus: {path: [{_key: 'b'}], offset: 0}, | ||
anchor: {path: [{_key: 'b'}], offset: 0}, | ||
} | ||
|
||
const editorRef: RefObject<PortableTextEditor> = createRef() | ||
const onChange = jest.fn() | ||
const component = render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/>, | ||
) | ||
const element = await getEditableElement(component) | ||
|
||
const editor = editorRef.current | ||
const inlineType = editor?.schemaTypes.inlineObjects.find((t) => t.name === 'someObject') | ||
await waitFor(async () => { | ||
if (editor && inlineType && element) { | ||
PortableTextEditor.focus(editor) | ||
PortableTextEditor.select(editor, initialSelection) | ||
fireEvent.click(element) | ||
expect(PortableTextEditor.getValue(editor)).toEqual([initialValue[0], newBlock]) | ||
} | ||
}) | ||
}) | ||
it('should not add blocks if the last element is a text block', async () => { | ||
const initialValue = [ | ||
{ | ||
_key: 'b', | ||
_type: 'someObject', | ||
}, | ||
{ | ||
_type: 'myTestBlockType', | ||
_key: '3', | ||
style: 'normal', | ||
markDefs: [], | ||
children: [ | ||
{ | ||
_type: 'span', | ||
_key: '2', | ||
text: '', | ||
marks: [], | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
const initialSelection = { | ||
focus: {path: [{_key: 'b'}], offset: 0}, | ||
anchor: {path: [{_key: 'b'}], offset: 0}, | ||
} | ||
|
||
const editorRef: RefObject<PortableTextEditor> = createRef() | ||
const onChange = jest.fn() | ||
const component = render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/>, | ||
) | ||
const element = await getEditableElement(component) | ||
|
||
const editor = editorRef.current | ||
const inlineType = editor?.schemaTypes.inlineObjects.find((t) => t.name === 'someObject') | ||
await waitFor(async () => { | ||
if (editor && inlineType && element) { | ||
PortableTextEditor.focus(editor) | ||
PortableTextEditor.select(editor, initialSelection) | ||
fireEvent.click(element) | ||
expect(PortableTextEditor.getValue(editor)).toEqual(initialValue) | ||
} | ||
}) | ||
}) | ||
it('should not add blocks if the last element is void, but its not focused on that one', async () => { | ||
const initialValue = [ | ||
{ | ||
_key: 'a', | ||
_type: 'someObject', | ||
}, | ||
{ | ||
_type: 'myTestBlockType', | ||
_key: 'b', | ||
style: 'normal', | ||
markDefs: [], | ||
children: [ | ||
{ | ||
_type: 'span', | ||
_key: 'b1', | ||
text: '', | ||
marks: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
_key: 'c', | ||
_type: 'someObject', | ||
}, | ||
] | ||
|
||
const initialSelection = { | ||
focus: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 2}, | ||
anchor: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 2}, | ||
} | ||
|
||
const editorRef: RefObject<PortableTextEditor> = createRef() | ||
const onChange = jest.fn() | ||
const component = render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/>, | ||
) | ||
const element = await getEditableElement(component) | ||
|
||
const editor = editorRef.current | ||
const inlineType = editor?.schemaTypes.inlineObjects.find((t) => t.name === 'someObject') | ||
await waitFor(async () => { | ||
if (editor && inlineType && element) { | ||
PortableTextEditor.focus(editor) | ||
PortableTextEditor.select(editor, initialSelection) | ||
fireEvent.click(element) | ||
expect(PortableTextEditor.getValue(editor)).toEqual(initialValue) | ||
} | ||
}) | ||
}) | ||
it('should not add blocks if the last element is void, and its focused on that one when clicking', async () => { | ||
const initialValue = [ | ||
{ | ||
_key: 'a', | ||
_type: 'someObject', | ||
}, | ||
{ | ||
_type: 'myTestBlockType', | ||
_key: 'b', | ||
style: 'normal', | ||
markDefs: [], | ||
children: [ | ||
{ | ||
_type: 'span', | ||
_key: 'b1', | ||
text: '', | ||
marks: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
_key: 'c', | ||
_type: 'someObject', | ||
}, | ||
] | ||
|
||
const initialSelection = { | ||
focus: {path: [{_key: 'c'}], offset: 0}, | ||
anchor: {path: [{_key: 'c'}], offset: 0}, | ||
} | ||
|
||
const editorRef: RefObject<PortableTextEditor> = createRef() | ||
const onChange = jest.fn() | ||
const component = render( | ||
<PortableTextEditorTester | ||
onChange={onChange} | ||
ref={editorRef} | ||
schemaType={schemaType} | ||
value={initialValue} | ||
/>, | ||
) | ||
const element = await getEditableElement(component) | ||
|
||
const editor = editorRef.current | ||
const inlineType = editor?.schemaTypes.inlineObjects.find((t) => t.name === 'someObject') | ||
await waitFor(async () => { | ||
if (editor && inlineType && element) { | ||
PortableTextEditor.focus(editor) | ||
PortableTextEditor.select(editor, initialSelection) | ||
fireEvent.click(element) | ||
expect(PortableTextEditor.getValue(editor)).toEqual(initialValue.concat(newBlock)) | ||
} | ||
}) | ||
}) | ||
}) |
39 changes: 39 additions & 0 deletions
39
packages/@sanity/portable-text-editor/src/editor/__tests__/utils.ts
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,39 @@ | ||
// This utils are inspired from https://github.dev/mwood23/slate-test-utils/blob/master/src/buildTestHarness.tsx | ||
import {act, fireEvent, type render} from '@testing-library/react' | ||
import {parseHotkey} from 'is-hotkey-esm' | ||
|
||
export async function triggerKeyboardEvent(hotkey: string, element: Element): Promise<void> { | ||
return act(async () => { | ||
const eventProps = parseHotkey(hotkey) | ||
const values = hotkey.split('+') | ||
|
||
fireEvent( | ||
element, | ||
new window.KeyboardEvent('keydown', { | ||
key: values[values.length - 1], | ||
code: `${eventProps.which}`, | ||
keyCode: eventProps.which, | ||
bubbles: true, | ||
...eventProps, | ||
}), | ||
) | ||
}) | ||
} | ||
|
||
export async function getEditableElement(component: ReturnType<typeof render>): Promise<Element> { | ||
await act(async () => component) | ||
const element = component.container.querySelector('[data-slate-editor="true"]') | ||
if (!element) { | ||
throw new Error('Could not find element') | ||
} | ||
/** | ||
* Manually add this because JSDom doesn't implement this and Slate checks for it | ||
* internally before doing stuff. | ||
* | ||
* https://github.com/jsdom/jsdom/issues/1670 | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
element.isContentEditable = true | ||
return element | ||
} |
Oops, something went wrong.