Files
genoffice/apps/docs/tests/style-gallery-overflow.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

220 lines
6.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Regression (feedback row 53): the home-tab style gallery clipped overflowing
// cards with no way to reach them. The gallery must show a "more styles"
// expander whenever cards are cut off, and the expander grid must list every style.
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'
class FakeResizeObserver {
static instances: FakeResizeObserver[] = []
targets = new Set<Element>()
constructor(private cb: ResizeObserverCallback) {
FakeResizeObserver.instances.push(this)
}
observe(el: Element) {
this.targets.add(el)
}
unobserve(el: Element) {
this.targets.delete(el)
}
disconnect() {
this.targets.clear()
}
static fire(el: Element) {
for (const inst of FakeResizeObserver.instances) {
if (inst.targets.has(el)) {
inst.cb([{ target: el } as ResizeObserverEntry], inst as unknown as ResizeObserver)
}
}
}
}
function makeEditor(): Editor {
return new Editor({
element: document.createElement('div'),
extensions: editorExtensions,
content: {
type: 'doc',
content: [{ type: 'docParagraph', content: [{ type: 'text', text: 'hello world' }] }],
},
})
}
const noop = () => {}
function ribbonProps(editor: Editor) {
return {
editor,
formatState: computeFormatState(editor),
hasDoc: true,
blocks: [],
onOpen: noop,
onSave: noop,
onSaveAs: noop,
showAi: false,
onToggleAi: noop,
section: null,
onSection: noop,
activeSection: null,
onInsertSectionBreak: noop,
pageColor: null,
onPageColor: noop,
watermark: null,
onWatermark: noop,
themeFonts: null,
onThemeFonts: noop,
themeColors: null,
onThemeColors: noop,
inkTool: 'select' as const,
onInkTool: noop,
inkPen: { color: 'C00000', width: 2 },
onInkPen: noop,
inkHighlighter: { color: 'FFFF00', width: 10 },
onInkHighlighter: noop,
inkCount: 0,
onInkClearAll: noop,
onInsertNote: noop,
sources: [],
onAddSource: noop,
zoom: 100,
onZoom: noop,
onZoomFit: noop,
darkPage: false,
onDarkPage: noop,
onAiPreset: noop,
header: null,
onHeader: noop,
onPageNumFormat: noop,
onInsertField: noop,
footer: null,
onFooter: noop,
titlePg: false,
onTitlePg: noop,
evenOddHf: false,
onEvenOddHf: noop,
showMarks: false,
onShowMarks: noop,
showRuler: false,
onShowRuler: noop,
showNav: false,
onShowNav: noop,
commentCount: 0,
openCommentCount: 0,
onShowComments: noop,
canComment: false,
onNewComment: noop,
trackChanges: false,
onTrackChanges: noop,
spellcheck: true,
onSpellcheck: noop,
revisionDisplay: 'all' as const,
onRevisionDisplay: noop,
revisionCount: 0,
onAcceptRevision: noop,
onRejectRevision: noop,
onGotoRevision: noop,
isProtected: false,
commentsAllowed: false,
trackChangesForced: false,
protectActive: false,
onProtectDoc: noop,
onCompare: noop,
filePath: null,
viewMode: 'print' as const,
onViewMode: noop,
readMode: false,
onReadMode: noop,
showGrid: false,
onShowGrid: noop,
splitView: false,
onSplitView: noop,
onPagePreview: noop,
}
}
/**
* The gallery wraps whole cards onto a clipped second row; overflow detection
* reads each card's offsetTop. jsdom has no layout, so emulate it: the first
* `visibleCount` cards sit on row 0, the rest wrap to a second row.
*/
function setRowLayout(gallery: Element, visibleCount: number) {
const cards = gallery.querySelectorAll<HTMLElement>('.style-card')
cards.forEach((card, i) => {
const row2 = i >= visibleCount
Object.defineProperty(card, 'offsetTop', { configurable: true, value: row2 ? 66 : 0 })
Object.defineProperty(card, 'offsetLeft', {
configurable: true,
value: (row2 ? i - visibleCount : i) * 78,
})
Object.defineProperty(card, 'offsetWidth', { configurable: true, value: 74 })
})
}
describe('style gallery overflow', () => {
let editor: Editor
let root: Root
let container: HTMLElement
beforeEach(() => {
vi.stubGlobal('ResizeObserver', FakeResizeObserver)
editor = makeEditor()
container = document.createElement('div')
document.body.appendChild(container)
root = createRoot(container)
act(() => root.render(createElement(Ribbon, ribbonProps(editor))))
})
afterEach(() => {
act(() => root.unmount())
container.remove()
editor.destroy()
FakeResizeObserver.instances.length = 0
vi.unstubAllGlobals()
})
it('shows no expander while every card fits', () => {
expect(container.querySelector('.style-gallery')).not.toBeNull()
expect(container.querySelectorAll('.style-gallery .style-card').length).toBe(6)
expect(container.querySelector('.style-gallery-more')).toBeNull()
})
it('shows the expander when cards wrap out of view and hides it when they fit again', () => {
const gallery = container.querySelector('.style-gallery')!
const wrap = gallery.parentElement!
setRowLayout(gallery, 2)
act(() => FakeResizeObserver.fire(wrap))
expect(container.querySelector('.style-gallery-more')).not.toBeNull()
// the gallery is capped right after the last visible card (2 × 78 - 4 gap)
expect((gallery as HTMLElement).style.maxWidth).toBe('152px')
setRowLayout(gallery, 6)
act(() => FakeResizeObserver.fire(wrap))
expect(container.querySelector('.style-gallery-more')).toBeNull()
expect((gallery as HTMLElement).style.maxWidth).toBe('')
})
it('expander opens a grid with every style, and picking one applies it and closes', () => {
const gallery = container.querySelector('.style-gallery')!
setRowLayout(gallery, 2)
act(() => FakeResizeObserver.fire(gallery.parentElement!))
const more = container.querySelector<HTMLButtonElement>('.style-gallery-more')!
expect(more.getAttribute('aria-label')).toBeTruthy()
act(() => more.click())
const menu = container.querySelector('.style-gallery-menu')!
const cards = menu.querySelectorAll<HTMLButtonElement>('.style-card')
// 4 paragraph styles + 2 preset character styles: nothing is dropped
expect(cards.length).toBe(6)
editor.commands.setTextSelection(3)
act(() => cards[1].click()) // Heading 1
expect(editor.isActive('docHeading', { level: 1 })).toBe(true)
expect(container.querySelector('.style-gallery-menu')).toBeNull()
})
})