Skip to content

Commit

Permalink
test(portable-text-editor): add test for empty array value (#6764)
Browse files Browse the repository at this point in the history
  • Loading branch information
skogsmaskin authored May 24, 2024
1 parent 9f7ab8b commit a13e22c
Showing 1 changed file with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {describe, expect, it, jest} from '@jest/globals'
import {type PortableTextBlock} from '@sanity/types'
import {render, waitFor} from '@testing-library/react'
import {createRef, type RefObject} from 'react'

Expand Down Expand Up @@ -179,4 +180,93 @@ describe('when PTE would display warnings, instead it self solves', () => {
}
})
})
it('allows missing .markDefs', async () => {
const editorRef: RefObject<PortableTextEditor> = createRef()
const initialValue = [
{
_key: 'abc',
_type: 'myTestBlockType',
children: [
{
_key: 'def',
_type: 'span',
marks: [],
text: 'No markDefs',
},
],
style: 'normal',
},
]

const onChange = jest.fn()
render(
<PortableTextEditorTester
onChange={onChange}
ref={editorRef}
schemaType={schemaType}
value={initialValue}
/>,
)
await waitFor(() => {
expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
})
await waitFor(() => {
if (editorRef.current) {
PortableTextEditor.focus(editorRef.current)
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
{
_key: 'abc',
_type: 'myTestBlockType',
children: [
{
_key: 'def',
_type: 'span',
text: 'No markDefs',
marks: [],
},
],
style: 'normal',
},
])
}
})
})

it('allows empty array of blocks', async () => {
const editorRef: RefObject<PortableTextEditor> = createRef()
const initialValue = [] as PortableTextBlock[]

const onChange = jest.fn()
render(
<PortableTextEditorTester
onChange={onChange}
ref={editorRef}
schemaType={schemaType}
value={initialValue}
/>,
)
await waitFor(() => {
expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
})
await waitFor(() => {
if (editorRef.current) {
PortableTextEditor.focus(editorRef.current)
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
{
_key: '5',
_type: 'myTestBlockType',
children: [{_key: '4', _type: 'span', marks: [], text: ''}],
markDefs: [],
style: 'normal',
},
])
}
})
await waitFor(() => {
expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
})
})
})

0 comments on commit a13e22c

Please sign in to comment.