|
1 | 1 | import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; |
2 | 2 | import { Doc as YDoc } from 'yjs'; |
| 3 | + |
| 4 | +const { seedPartsFromEditorSpy } = vi.hoisted(() => ({ |
| 5 | + seedPartsFromEditorSpy: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock('@extensions/collaboration/part-sync/seed-parts.js', async (importOriginal) => { |
| 9 | + const actual = await importOriginal<typeof import('@extensions/collaboration/part-sync/seed-parts.js')>(); |
| 10 | + |
| 11 | + return { |
| 12 | + ...actual, |
| 13 | + seedPartsFromEditor: vi.fn((...args: Parameters<typeof actual.seedPartsFromEditor>) => { |
| 14 | + seedPartsFromEditorSpy(...args); |
| 15 | + return actual.seedPartsFromEditor(...args); |
| 16 | + }), |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
3 | 20 | import { Editor } from './Editor.js'; |
4 | 21 | import { getStarterExtensions } from '@extensions/index.js'; |
5 | 22 | import { getTestDataAsFileBuffer, loadTestDataForEditorTests } from '@tests/helpers/helpers.js'; |
@@ -55,6 +72,7 @@ describe('Editor.replaceFile', () => { |
55 | 72 |
|
56 | 73 | afterEach(() => { |
57 | 74 | vi.useRealTimers(); |
| 75 | + vi.clearAllMocks(); |
58 | 76 | }); |
59 | 77 |
|
60 | 78 | it('applies replacement when provider emits sync(true) without synced event', async () => { |
@@ -186,6 +204,48 @@ describe('Editor.replaceFile', () => { |
186 | 204 | } |
187 | 205 | }); |
188 | 206 |
|
| 207 | + it('runs collaborative replace side effects once when the provider is already synced', async () => { |
| 208 | + const provider = createProviderStub(); |
| 209 | + provider.synced = true; |
| 210 | + provider.isSynced = true; |
| 211 | + |
| 212 | + const ydoc = new YDoc(); |
| 213 | + const editor = createTestEditor({ |
| 214 | + ydoc, |
| 215 | + collaborationProvider: provider, |
| 216 | + }); |
| 217 | + const expectedEditor = createTestEditor(); |
| 218 | + |
| 219 | + try { |
| 220 | + await editor.open(undefined, { |
| 221 | + mode: 'docx', |
| 222 | + content: blankDocData.docx as any, |
| 223 | + mediaFiles: blankDocData.mediaFiles as any, |
| 224 | + fonts: blankDocData.fonts as any, |
| 225 | + }); |
| 226 | + await expectedEditor.open(replacementBuffer, { mode: 'docx' }); |
| 227 | + |
| 228 | + const seedCallsBeforeReplace = seedPartsFromEditorSpy.mock.calls.length; |
| 229 | + |
| 230 | + await editor.replaceFile(replacementBuffer); |
| 231 | + |
| 232 | + const seedCallsDuringReplace = seedPartsFromEditorSpy.mock.calls.length - seedCallsBeforeReplace; |
| 233 | + |
| 234 | + expect(editor.state.doc.textContent).toBe(expectedEditor.state.doc.textContent); |
| 235 | + expect(seedCallsDuringReplace).toBe(1); |
| 236 | + expect(seedPartsFromEditorSpy).toHaveBeenLastCalledWith(editor, ydoc, { replaceExisting: true }); |
| 237 | + } finally { |
| 238 | + if (editor.lifecycleState === 'ready') { |
| 239 | + editor.close(); |
| 240 | + } |
| 241 | + if (expectedEditor.lifecycleState === 'ready') { |
| 242 | + expectedEditor.close(); |
| 243 | + } |
| 244 | + editor.destroy(); |
| 245 | + expectedEditor.destroy(); |
| 246 | + } |
| 247 | + }); |
| 248 | + |
189 | 249 | it('seeds collaborative bodySectPr metadata when replacing a file with a final section', async () => { |
190 | 250 | const provider = createProviderStub(); |
191 | 251 | const ydoc = new YDoc(); |
|
0 commit comments