|
| 1 | +/* @vitest-environment node */ |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 4 | +import { readFile } from 'node:fs/promises'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | +import { dirname, join } from 'node:path'; |
| 7 | +import { Editor } from '@core/Editor.js'; |
| 8 | +import { getStarterExtensions } from '@extensions/index.js'; |
| 9 | +import { createDOMGlobalsLifecycle } from '../helpers/dom-globals-test-utils.js'; |
| 10 | + |
| 11 | +const __filename = fileURLToPath(import.meta.url); |
| 12 | +const __dirname = dirname(__filename); |
| 13 | + |
| 14 | +const loadDocxFixture = async (filename) => { |
| 15 | + return readFile(join(__dirname, '../data', filename)); |
| 16 | +}; |
| 17 | + |
| 18 | +describe('Editor.currentTotalPages', () => { |
| 19 | + const domLifecycle = createDOMGlobalsLifecycle(); |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + domLifecycle.setup(); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + domLifecycle.teardown(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns undefined when presentationEditor is null', async () => { |
| 30 | + const buffer = await loadDocxFixture('blank-doc.docx'); |
| 31 | + const [content, , mediaFiles, fonts] = await Editor.loadXmlData(buffer, true); |
| 32 | + |
| 33 | + const editor = new Editor({ |
| 34 | + mode: 'docx', |
| 35 | + documentId: 'test-no-pagination', |
| 36 | + extensions: getStarterExtensions(), |
| 37 | + content, |
| 38 | + mediaFiles, |
| 39 | + fonts, |
| 40 | + }); |
| 41 | + |
| 42 | + expect(editor.presentationEditor).toBeNull(); |
| 43 | + expect(editor.currentTotalPages).toBeUndefined(); |
| 44 | + |
| 45 | + editor.destroy(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns undefined when presentationEditor has no pages yet', async () => { |
| 49 | + const buffer = await loadDocxFixture('blank-doc.docx'); |
| 50 | + const [content, , mediaFiles, fonts] = await Editor.loadXmlData(buffer, true); |
| 51 | + |
| 52 | + const editor = new Editor({ |
| 53 | + mode: 'docx', |
| 54 | + documentId: 'test-empty-pages', |
| 55 | + extensions: getStarterExtensions(), |
| 56 | + content, |
| 57 | + mediaFiles, |
| 58 | + fonts, |
| 59 | + }); |
| 60 | + |
| 61 | + // Simulate a presentationEditor with empty pages (before first layout) |
| 62 | + editor.presentationEditor = /** @type {any} */ ({ |
| 63 | + getPages: vi.fn(() => []), |
| 64 | + }); |
| 65 | + |
| 66 | + expect(editor.currentTotalPages).toBeUndefined(); |
| 67 | + |
| 68 | + editor.presentationEditor = null; |
| 69 | + editor.destroy(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('returns the page count when presentationEditor has pages', async () => { |
| 73 | + const buffer = await loadDocxFixture('blank-doc.docx'); |
| 74 | + const [content, , mediaFiles, fonts] = await Editor.loadXmlData(buffer, true); |
| 75 | + |
| 76 | + const editor = new Editor({ |
| 77 | + mode: 'docx', |
| 78 | + documentId: 'test-with-pages', |
| 79 | + extensions: getStarterExtensions(), |
| 80 | + content, |
| 81 | + mediaFiles, |
| 82 | + fonts, |
| 83 | + }); |
| 84 | + |
| 85 | + // Simulate a presentationEditor after layout completes |
| 86 | + editor.presentationEditor = /** @type {any} */ ({ |
| 87 | + getPages: vi.fn(() => [ |
| 88 | + { number: 1, size: { w: 612, h: 792 } }, |
| 89 | + { number: 2, size: { w: 612, h: 792 } }, |
| 90 | + { number: 3, size: { w: 612, h: 792 } }, |
| 91 | + ]), |
| 92 | + }); |
| 93 | + |
| 94 | + expect(editor.currentTotalPages).toBe(3); |
| 95 | + |
| 96 | + editor.presentationEditor = null; |
| 97 | + editor.destroy(); |
| 98 | + }); |
| 99 | +}); |
0 commit comments