Skip to content

Commit

Permalink
test(portable-text-editor): test the value normalization of style prop
Browse files Browse the repository at this point in the history
Test that when editing a block without a style prop, the result will have
a default style prop.
  • Loading branch information
skogsmaskin committed Feb 22, 2022
1 parent ad91bf2 commit 03b206c
Showing 1 changed file with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @jest-environment ./test/setup/jsdom.jest.env.ts
*/
// eslint-disable-next-line import/no-unassigned-import
import '@testing-library/jest-dom/extend-expect'
import {act} from 'react-dom/test-utils'
import {render} from '@testing-library/react'

import React from 'react'
import {PortableTextEditor} from '../../editor/PortableTextEditor'
import {PortableTextEditorTester, type} from '../../editor/__tests__/PortableTextEditorTester'

describe('values: normalization', () => {
it("accepts incoming value with blocks without a style or markDefs prop, but doesn't leave them without them when editing them", () => {
const editorRef: React.RefObject<PortableTextEditor> = React.createRef()
const initialValue = [
{
_key: '5fc57af23597',
_type: 'myTestBlockType',
children: [
{
_key: 'be1c67c6971a',
_type: 'span',
marks: [],
text: 'Hello',
},
],
markDefs: [],
},
]
const onChange = jest.fn()
act(() => {
render(
<PortableTextEditorTester
onChange={onChange}
ref={editorRef}
type={type}
value={initialValue}
/>
)
})
if (!editorRef.current) {
throw new Error('No editor')
}
act(() => {
if (editorRef.current) {
PortableTextEditor.focus(editorRef.current)
PortableTextEditor.select(editorRef.current, {
focus: {path: [{_key: '5fc57af23597'}, 'children', {_key: 'be1c67c6971a'}], offset: 0},
anchor: {path: [{_key: '5fc57af23597'}, 'children', {_key: 'be1c67c6971a'}], offset: 5},
})
}
})
act(() => {
if (editorRef.current) {
PortableTextEditor.toggleMark(editorRef.current, 'strong')
}
})
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
{
_key: '5fc57af23597',
_type: 'myTestBlockType',
children: [
{
_key: 'be1c67c6971a',
_type: 'span',
marks: ['strong'],
text: 'Hello',
},
],
markDefs: [],
style: 'normal',
},
])
})
})

3 comments on commit 03b206c

@vercel
Copy link

@vercel vercel bot commented on 03b206c Feb 22, 2022

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:

studio-workshop – ./dev/workshop

studio-workshop.sanity.build
studio-workshop-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 03b206c Feb 22, 2022

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

@vercel
Copy link

@vercel vercel bot commented on 03b206c Feb 22, 2022

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:

perf-studio – ./

perf-studio-git-next.sanity.build
perf-studio.sanity.build

Please sign in to comment.