Files
genoffice/apps/docs/tests/spellcheck-toggle.test.ts
T
merrick-2002andGenOffice 69b4ce0560 Sync snapshot (2026-09-06) (#243)
- Docs: Word-style dark page, phased fast open for long documents, fonts embedded in the DOCX, spellcheck toggle, hyperlink editing, and ~80 Word fidelity fixes (CJK/JP metrics, header/footer, tables, anchored pictures)
- Sheets: hold formula recalculation while file data streams in, large-workbook scroll/load performance, combo charts, duplicate/unique conditional formatting by display text, RTL frozen-pane fix, workbooks whose ZIP entries carry a leading slash
- Slides: MTX-compressed embedded font support, CJK/Latin face selection and PowerPoint script fallbacks, theme colors kept on paragraph defaults and bullets, more reliable AI edits with a layout audit after each batch
- PDF: AI page ops, text block move/align, notes and markup editing, watermark and header/footer, form check boxes, metadata; the cloud Export as Word fallback is removed, conversion is always on-device
- AI: Gemini tool schema translation, OpenCode Zen/Go providers, markdown tables and code blocks in chat, current date in the system prompt
- Shell: Office-style ribbon collapse across all editors, native Windows ARM64 installer, BUILD_DIR override for packaging

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-09-07 00:18:14 +08:00

124 lines
4.2 KiB
TypeScript

// Review → Spelling toggles the native check-as-you-type spellchecker (r168):
// the ribbon button flips the persisted pref, and the editorProps route (App's
// setOptions call) must actually move the spellcheck attribute on the editable
// DOM — a direct DOM write would be reverted by ProseMirror's attribute deco.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { Editor } from '@tiptap/core'
import { editorExtensions } from '../src/renderer/editor/extensions'
import { computeFormatState } from '../src/renderer/components/ribbon-format-state'
import { Ribbon } from '../src/renderer/components/Ribbon'
import { SPELLCHECK_KEY, spellcheckEnabled } from '../src/renderer/spellcheck-pref'
import { t } from '../src/renderer/i18n/locale'
import { ribbonProps } from './helpers/ribbon-props'
function makeEditor(): Editor {
return new Editor({
element: document.createElement('div'),
extensions: editorExtensions,
content: {
type: 'doc',
content: [{ type: 'docParagraph', content: [{ type: 'text', text: 'hello world' }] }],
},
})
}
describe('spellcheck preference', () => {
afterEach(() => localStorage.removeItem(SPELLCHECK_KEY))
it('defaults to enabled and follows the stored flag', () => {
expect(spellcheckEnabled()).toBe(true)
localStorage.setItem(SPELLCHECK_KEY, '0')
expect(spellcheckEnabled()).toBe(false)
localStorage.setItem(SPELLCHECK_KEY, '1')
expect(spellcheckEnabled()).toBe(true)
})
})
describe('editorProps route', () => {
it('moves the spellcheck attribute on the editable DOM via setOptions', () => {
const editor = new Editor({
element: document.createElement('div'),
extensions: editorExtensions,
editorProps: { attributes: { class: 'doc-page', spellcheck: 'true' } },
content: { type: 'doc', content: [{ type: 'docParagraph' }] },
})
expect(editor.view.dom.getAttribute('spellcheck')).toBe('true')
editor.setOptions({
editorProps: {
...editor.options.editorProps,
attributes: {
...(editor.options.editorProps.attributes as Record<string, string>),
spellcheck: 'false',
},
},
})
expect(editor.view.dom.getAttribute('spellcheck')).toBe('false')
editor.destroy()
})
})
describe('Review → Spelling toggle', () => {
let editor: Editor
let root: Root
let container: HTMLElement
const onSpellcheck = vi.fn()
function renderRibbon(spellcheck: boolean) {
act(() =>
root.render(
createElement(Ribbon, {
...ribbonProps(editor, computeFormatState(editor)),
spellcheck,
onSpellcheck,
}),
),
)
}
const spellingButton = () =>
[...container.querySelectorAll<HTMLButtonElement>('.rb-big')].find(
// the icon's SVG text glyph ("abc") is part of textContent, so match the label span
(b) => b.querySelector('span:last-child')?.textContent === t('ribbonSpellcheckBtn'),
)
beforeEach(() => {
editor = makeEditor()
container = document.createElement('div')
document.body.appendChild(container)
root = createRoot(container)
})
afterEach(() => {
act(() => root.unmount())
container.remove()
editor.destroy()
onSpellcheck.mockClear()
})
it('shows an active toggle that reports the flipped state on click', () => {
renderRibbon(true)
const reviewTab = [...container.querySelectorAll<HTMLButtonElement>('.ribbon-tab')].find(
(b) => b.textContent === t('ribbonTabReview'),
)!
act(() => reviewTab.click())
const btn = spellingButton()!
expect(btn.className).toContain('active')
act(() => btn.click())
expect(onSpellcheck).toHaveBeenCalledWith(false)
})
it('renders inactive when spellcheck is off and re-enables on click', () => {
renderRibbon(false)
const reviewTab = [...container.querySelectorAll<HTMLButtonElement>('.ribbon-tab')].find(
(b) => b.textContent === t('ribbonTabReview'),
)!
act(() => reviewTab.click())
const btn = spellingButton()!
expect(btn.className).not.toContain('active')
act(() => btn.click())
expect(onSpellcheck).toHaveBeenCalledWith(true)
})
})