Skip to content

Commit

Permalink
test: add insert.block test to ensure custom keys are preserved
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhg committed Jan 27, 2025
1 parent 9c3bf07 commit 47d48ab
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 3 deletions.
179 changes: 179 additions & 0 deletions packages/editor/tests/event.insert.block.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import * as React from 'react'
import {describe, expect, test} from 'vitest'
import {render} from 'vitest-browser-react'
import type {Editor} from '../src/editor/create-editor'
import {defineSchema} from '../src/editor/define-schema'
import {PortableTextEditable} from '../src/editor/Editable'
import {EditorProvider, useEditor} from '../src/editor/editor-provider'
import {createTestKeyGenerator} from '../src/internal-utils/test-key-generator'

describe('event.insert.block', () => {
test('Scenario: Inserting block with custom _key', () => {
const editorRef = React.createRef<Editor>()

render(
<EditorProvider
initialConfig={{
keyGenerator: createTestKeyGenerator(),
schemaDefinition: defineSchema({}),
}}
>
<EditorRefPlugin ref={editorRef} />
<PortableTextEditable />
</EditorProvider>,
)

expect(editorRef.current?.getSnapshot().context.value).toEqual([
{
_key: 'k2',
_type: 'block',
children: [
{
_key: 'k3',
_type: 'span',
text: '',
marks: [],
},
],
markDefs: [],
style: 'normal',
},
])

editorRef.current?.send({
type: 'insert.block',
block: {
_type: 'block',
_key: 'custom key',
children: [
{
_type: 'span',
text: 'foo',
},
],
},
placement: 'auto',
})

expect(editorRef.current?.getSnapshot().context.value).toEqual([
{
_key: 'custom key',
_type: 'block',
children: [
{
_key: 'k4',
_type: 'span',
text: 'foo',
marks: [],
},
],
markDefs: [],
style: 'normal',
},
])
})

test('Scenario: Inserting two blocks with same custom _key', () => {
const editorRef = React.createRef<Editor>()

render(
<EditorProvider
initialConfig={{
keyGenerator: createTestKeyGenerator(),
schemaDefinition: defineSchema({}),
}}
>
<EditorRefPlugin ref={editorRef} />
<PortableTextEditable />
</EditorProvider>,
)

expect(editorRef.current?.getSnapshot().context.value).toEqual([
{
_key: 'k2',
_type: 'block',
children: [
{
_key: 'k3',
_type: 'span',
text: '',
marks: [],
},
],
markDefs: [],
style: 'normal',
},
])

editorRef.current?.send({
type: 'insert.block',
block: {
_type: 'block',
_key: 'custom key',
children: [
{
_type: 'span',
text: 'foo',
},
],
},
placement: 'auto',
})

editorRef.current?.send({
type: 'insert.block',
block: {
_type: 'block',
_key: 'custom key',
children: [
{
_type: 'span',
text: 'bar',
},
],
},
placement: 'auto',
})

expect(editorRef.current?.getSnapshot().context.value).toEqual([
{
_key: 'custom key',
_type: 'block',
children: [
{
_key: 'k4',
_type: 'span',
text: 'foo',
marks: [],
},
],
markDefs: [],
style: 'normal',
},
{
_key: 'k5',
_type: 'block',
children: [
{
_key: 'k6',
_type: 'span',
text: 'bar',
marks: [],
},
],
markDefs: [],
style: 'normal',
},
])
})
})

const EditorRefPlugin = React.forwardRef<Editor | null>((_, ref) => {
const editor = useEditor()

const portableTextEditorRef = React.useRef(editor)

React.useImperativeHandle(ref, () => portableTextEditorRef.current, [])

return null
})
2 changes: 1 addition & 1 deletion packages/editor/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./tsconfig.base.json",
"include": ["./src", "./e2e-tests", "./gherkin-tests"],
"include": ["./src", "./e2e-tests", "./gherkin-tests", "./tests"],
"compilerOptions": {
"rootDir": ".",
"outDir": "./lib",
Expand Down
9 changes: 7 additions & 2 deletions packages/editor/vitest.workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export default defineWorkspace([
],
test: {
name: 'browser',
include: ['gherkin-tests/**/*.test.ts', 'gherkin-tests/**/*.test.tsx'],
include: [
'gherkin-tests/**/*.test.ts',
'gherkin-tests/**/*.test.tsx',
'tests/**/*.test.ts',
'tests/**/*.test.tsx',
],
browser: {
enabled: true,
headless: true,
Expand Down Expand Up @@ -39,7 +44,7 @@ export default defineWorkspace([
],
test: {
name: 'unit',
exclude: ['node_modules', 'e2e-tests', 'gherkin-tests'],
exclude: ['node_modules', 'e2e-tests', 'gherkin-tests', 'tests'],
environment: 'jsdom',
setupFiles: ['./vitest.setup.ts'],
},
Expand Down

0 comments on commit 47d48ab

Please sign in to comment.