File size: 3,542 Bytes
2daf8de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | import { act, render } from '@testing-library/react'
import { Editor } from '@tiptap/core'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { TextSelection } from '@tiptap/pm/state'
import React from 'react'
import { afterEach, describe, expect, it } from 'vitest'
import { useEditorState } from './useEditorState.js'
const createTestEditor = () => {
return new Editor({
extensions: [Document, Paragraph, Text],
content: '<p>Hello</p>',
})
}
const EditableIndicator = ({ editor }: { editor: Editor }) => {
const isEditable = useEditorState({
editor,
selector: ctx => ctx.editor.isEditable,
})
return React.createElement('div', { 'data-testid': 'editable-state' }, String(isEditable))
}
const ContentIndicator = ({ editor }: { editor: Editor }) => {
const text = useEditorState({
editor,
selector: ctx => ctx.editor.getText(),
})
return React.createElement('div', { 'data-testid': 'content-state' }, text)
}
const TransactionCountIndicator = ({ editor }: { editor: Editor }) => {
const transactionNumber = useEditorState({
editor,
selector: ctx => ctx.transactionNumber,
})
return React.createElement('div', { 'data-testid': 'tx-number' }, String(transactionNumber))
}
describe('useEditorState', () => {
afterEach(() => {
document.body.innerHTML = ''
})
it('re-renders when editor.setEditable() toggles the editable state', () => {
const editor = createTestEditor()
const { getByTestId } = render(React.createElement(EditableIndicator, { editor }))
expect(getByTestId('editable-state').textContent).toBe('true')
act(() => {
editor.setEditable(false)
})
expect(getByTestId('editable-state').textContent).toBe('false')
act(() => {
editor.setEditable(true)
})
expect(getByTestId('editable-state').textContent).toBe('true')
editor.destroy()
})
it('still re-renders on a normal content-changing transaction (regression)', () => {
const editor = createTestEditor()
const { getByTestId } = render(React.createElement(ContentIndicator, { editor }))
expect(getByTestId('content-state').textContent).toBe('Hello')
act(() => {
editor.commands.setContent('<p>World</p>')
})
expect(getByTestId('content-state').textContent).toBe('World')
editor.destroy()
})
it('notifies once per document change (transaction + update are deduped)', () => {
const editor = createTestEditor()
const { getByTestId } = render(React.createElement(TransactionCountIndicator, { editor }))
const before = Number(getByTestId('tx-number').textContent)
act(() => {
// A single doc-changing transaction emits both `transaction` and `update`.
editor.view.dispatch(editor.state.tr.insertText('!'))
})
const after = Number(getByTestId('tx-number').textContent)
expect(after - before).toBe(1)
editor.destroy()
})
it('still notifies on selection-only transactions (which emit no update event)', () => {
const editor = createTestEditor()
const { getByTestId } = render(React.createElement(TransactionCountIndicator, { editor }))
const before = Number(getByTestId('tx-number').textContent)
act(() => {
editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atStart(editor.state.doc)))
})
const after = Number(getByTestId('tx-number').textContent)
expect(after - before).toBe(1)
editor.destroy()
})
})
|