Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const createActionInstance = () => ({
});

vi.mock('../../services/ai-actions-service', () => {
const factory = vi.fn(() => {
const factory = vi.fn(function () {
const instance = createActionInstance();
actionInstances.push(instance);
return instance;
Expand Down
11 changes: 8 additions & 3 deletions packages/collaboration-yjs/src/shared-doc/shared-doc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,14 @@ beforeEach(() => {
});

afterEach(() => {
vi.unmock('../shared-doc/constants.js');
vi.unmock('../shared-doc/callback.js');
vi.unmock('../shared-doc/utils.js');
// AIDEV-NOTE: These modules are registered with `vi.doMock`, so they must be
// released with the runtime counterpart. `vi.unmock` is hoisted to the top of
// the module regardless of where it appears, so it ran before any test and
// never undid the `doMock` registrations. Vitest 4 warns about that hoisting
// and will make it an error.
vi.doUnmock('../shared-doc/constants.js');
vi.doUnmock('../shared-doc/callback.js');
vi.doUnmock('../shared-doc/utils.js');
});

describe('shared-doc constants and utils', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ describe('SuperDocCollaboration', () => {
this.has = vi.fn();
});

connectionHandlerCtor.mockImplementation(({ documentManager, hooks }) => ({
handle: handleSpy,
documentManager,
hooks,
}));
connectionHandlerCtor.mockImplementation(function ({ documentManager, hooks }) {
return {
handle: handleSpy,
documentManager,
hooks,
};
});

generateParamsFn.mockImplementation(() => ({
documentId: 'doc-123',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ vi.mock('../internal-logger/logger.js', () => ({
}));

vi.mock('../shared-doc/index.js', () => {
const SharedSuperDoc = vi.fn((documentId: string) => {
const SharedSuperDoc = vi.fn(function (documentId: string) {
const doc = {
name: documentId,
conns: new Map<CollaborationWebSocket, Set<number>>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ describe('Toolbar', () => {
it('does not attach ResizeObserver when responsiveToContainer is disabled', () => {
const observe = vi.fn();
const disconnect = vi.fn();
const ResizeObserverMock = vi.fn(() => ({ observe, disconnect }));
const ResizeObserverMock = vi.fn(function () {
return { observe, disconnect };
});
vi.stubGlobal('ResizeObserver', ResizeObserverMock);

const mockToolbar = {
Expand Down Expand Up @@ -177,7 +179,9 @@ describe('Toolbar', () => {
it('attaches ResizeObserver to the container when responsiveToContainer is enabled', () => {
const observe = vi.fn();
const disconnect = vi.fn();
const ResizeObserverMock = vi.fn(() => ({ observe, disconnect }));
const ResizeObserverMock = vi.fn(function () {
return { observe, disconnect };
});
vi.stubGlobal('ResizeObserver', ResizeObserverMock);

const container = document.createElement('div');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { Telemetry, COMMUNITY_LICENSE_KEY } from '@superdoc/common';

// Mock the Telemetry class to verify it's called correctly
vi.mock('@superdoc/common', () => ({
Telemetry: vi.fn().mockImplementation(() => ({
trackDocumentOpen: vi.fn(),
})),
Telemetry: vi.fn().mockImplementation(function () {
return {
trackDocumentOpen: vi.fn(),
};
}),
COMMUNITY_LICENSE_KEY: 'community-and-eval-agplv3',
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// @ts-check
// @vitest-environment node
// AIDEV-NOTE: This suite asserts the no-view (headless) dispatch path, so it
// must run without a DOM. It relied on the package's environmentMatchGlobs
// node rule, which Vitest 4 removed; the docblock now states it locally.
/**
* Headless dispatch regression for `continueNumbering`.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// @ts-check
// @vitest-environment node
// AIDEV-NOTE: This suite asserts the no-view (headless) dispatch path, so it
// must run without a DOM. It relied on the package's environmentMatchGlobs
// node rule, which Vitest 4 removed; the docblock now states it locally.
/**
* Headless dispatch regression for `restartNumbering` (first-item branch).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,24 @@ const { mockCreateHeaderFooterEditor, mockOnHeaderFooterDataUpdate, mockToFlowBl
return editorStub;
};

const mockCreateHeaderFooterEditor = vi.fn(
(input?: { editorContainer?: HTMLElement; editorHost?: HTMLElement }) => {
const editor = createSectionEditor();
if (input?.editorContainer instanceof HTMLElement) {
if (input.editorHost instanceof HTMLElement) {
input.editorHost.appendChild(input.editorContainer);
} else {
document.body.appendChild(input.editorContainer);
}
const mockCreateHeaderFooterEditor = vi.fn(function (input?: {
editorContainer?: HTMLElement;
editorHost?: HTMLElement;
}) {
const editor = createSectionEditor();
if (input?.editorContainer instanceof HTMLElement) {
if (input.editorHost instanceof HTMLElement) {
input.editorHost.appendChild(input.editorContainer);
} else {
document.body.appendChild(input.editorContainer);
}
editors.push({ editor, emit: editor.emit });
queueMicrotask(() => {
editor.emit('create');
});
return editor;
},
);
}
editors.push({ editor, emit: editor.emit });
queueMicrotask(() => {
editor.emit('create');
});
return editor;
});

return {
mockCreateHeaderFooterEditor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,41 @@ const {

// Mock Editor class
vi.mock('../../Editor', () => ({
Editor: vi.fn().mockImplementation(() => ({
setDocumentMode: vi.fn(),
setOptions: vi.fn(),
on: vi.fn(),
off: vi.fn(),
destroy: vi.fn(),
getJSON: vi.fn(() => ({ type: 'doc', content: [] })),
isEditable: true,
state: {
selection: { from: 0, to: 0, anchor: 0, head: 0, empty: true },
doc: {
nodeSize: 100,
content: {
size: 100,
Editor: vi.fn().mockImplementation(function () {
return {
setDocumentMode: vi.fn(),
setOptions: vi.fn(),
on: vi.fn(),
off: vi.fn(),
destroy: vi.fn(),
getJSON: vi.fn(() => ({ type: 'doc', content: [] })),
isEditable: true,
state: {
selection: { from: 0, to: 0, anchor: 0, head: 0, empty: true },
doc: {
nodeSize: 100,
content: {
size: 100,
},
descendants: vi.fn(),
},
descendants: vi.fn(),
},
},
view: {
dom: document.createElement('div'),
hasFocus: vi.fn(() => false),
},
options: {
documentId: 'test-doc',
element: document.createElement('div'),
},
converter: mockEditorConverterStore.current,
storage: {
image: {
media: mockEditorConverterStore.mediaFiles,
view: {
dom: document.createElement('div'),
hasFocus: vi.fn(() => false),
},
},
})),
options: {
documentId: 'test-doc',
element: document.createElement('div'),
},
converter: mockEditorConverterStore.current,
storage: {
image: {
media: mockEditorConverterStore.mediaFiles,
},
},
};
}),
}));

// Mock dependencies
Expand All @@ -111,14 +113,16 @@ vi.mock('@superdoc/layout-bridge', () => ({
findWordBoundaries: vi.fn(),
findParagraphBoundaries: vi.fn(),
createDragHandler: vi.fn(),
PageGeometryHelper: vi.fn().mockImplementation(({ layout, pageGap }) => ({
updateLayout: vi.fn(),
getPageIndexAtY: vi.fn(() => 0),
getNearestPageIndex: vi.fn(() => 0),
getPageTop: vi.fn(() => 0),
getPageGap: vi.fn(() => pageGap ?? 0),
getLayout: vi.fn(() => layout),
})),
PageGeometryHelper: vi.fn().mockImplementation(function ({ layout, pageGap }) {
return {
updateLayout: vi.fn(),
getPageIndexAtY: vi.fn(() => 0),
getNearestPageIndex: vi.fn(() => 0),
getPageTop: vi.fn(() => 0),
getPageGap: vi.fn(() => pageGap ?? 0),
getLayout: vi.fn(() => layout),
};
}),
}));

vi.mock('@superdoc/painter-dom', () => ({
Expand Down Expand Up @@ -149,38 +153,44 @@ vi.mock('@superdoc/measuring-dom', () => ({
}));

vi.mock('../../header-footer/HeaderFooterRegistry', () => ({
HeaderFooterEditorManager: vi.fn(() => ({
createEditor: vi.fn(),
destroyEditor: vi.fn(),
getEditor: vi.fn(),
refresh: mockHeaderFooterRefresh,
on: vi.fn(),
off: vi.fn(),
destroy: vi.fn(),
})),
HeaderFooterLayoutAdapter: vi.fn(() => ({
clear: vi.fn(),
getBatch: vi.fn(() => []),
getBlocksByRId: vi.fn(() => new Map()),
invalidateAll: mockHeaderFooterInvalidateAll,
setTrackedChangesRenderConfig: vi.fn(),
})),
HeaderFooterEditorManager: vi.fn(function () {
return {
createEditor: vi.fn(),
destroyEditor: vi.fn(),
getEditor: vi.fn(),
refresh: mockHeaderFooterRefresh,
on: vi.fn(),
off: vi.fn(),
destroy: vi.fn(),
};
}),
HeaderFooterLayoutAdapter: vi.fn(function () {
return {
clear: vi.fn(),
getBatch: vi.fn(() => []),
getBlocksByRId: vi.fn(() => new Map()),
invalidateAll: mockHeaderFooterInvalidateAll,
setTrackedChangesRenderConfig: vi.fn(),
};
}),
}));

vi.mock('../../header-footer/EditorOverlayManager', () => ({
EditorOverlayManager: vi.fn(() => ({
showEditingOverlay: vi.fn(() => ({
success: true,
editorHost: document.createElement('div'),
reason: null,
})),
hideEditingOverlay: vi.fn(),
showSelectionOverlay: vi.fn(),
hideSelectionOverlay: vi.fn(),
setOnDimmingClick: vi.fn(),
getActiveEditorHost: vi.fn(() => null),
destroy: vi.fn(),
})),
EditorOverlayManager: vi.fn(function () {
return {
showEditingOverlay: vi.fn(() => ({
success: true,
editorHost: document.createElement('div'),
reason: null,
})),
hideEditingOverlay: vi.fn(),
showSelectionOverlay: vi.fn(),
hideSelectionOverlay: vi.fn(),
setOnDimmingClick: vi.fn(),
getActiveEditorHost: vi.fn(() => null),
destroy: vi.fn(),
};
}),
}));

vi.mock('y-prosemirror', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,21 @@ const {
setShowFormattingMarks: vi.fn(),
})),
mockEditorConverterStore: converterStore,
mockEditorOverlayManager: vi.fn().mockImplementation(() => ({
showEditingOverlay: vi.fn(() => ({
success: true,
editorHost: document.createElement('div'),
reason: null,
})),
hideEditingOverlay: vi.fn(),
showSelectionOverlay: vi.fn(),
hideSelectionOverlay: vi.fn(),
setOnDimmingClick: vi.fn(),
getActiveEditorHost: vi.fn(() => null),
destroy: vi.fn(),
})),
mockEditorOverlayManager: vi.fn().mockImplementation(function () {
return {
showEditingOverlay: vi.fn(() => ({
success: true,
editorHost: document.createElement('div'),
reason: null,
})),
hideEditingOverlay: vi.fn(),
showSelectionOverlay: vi.fn(),
hideSelectionOverlay: vi.fn(),
setOnDimmingClick: vi.fn(),
getActiveEditorHost: vi.fn(() => null),
destroy: vi.fn(),
};
}),
mockPlugins: plugins,
mockEditorOn: editorOn,
};
Expand All @@ -169,7 +171,7 @@ vi.mock('../input/PositionHitResolver.js', () => ({

vi.mock('../../Editor.js', () => {
return {
Editor: vi.fn().mockImplementation(() => {
Editor: vi.fn().mockImplementation(function () {
const domElement = document.createElement('div');

const mockState = {
Expand Down Expand Up @@ -250,14 +252,16 @@ vi.mock('@superdoc/layout-bridge', () => ({
computeDisplayPageNumber: vi.fn((pages: Array<{ number?: number }>) =>
pages.map((p) => ({ displayText: String(p.number ?? 1) })),
),
PageGeometryHelper: vi.fn().mockImplementation(() => ({
updateLayout: vi.fn(),
getPageIndexAtY: vi.fn(() => 0),
getNearestPageIndex: vi.fn(() => 0),
getPageTop: vi.fn(() => 0),
getPageGap: vi.fn(() => 0),
getLayout: vi.fn(() => ({ pages: [] })),
})),
PageGeometryHelper: vi.fn().mockImplementation(function () {
return {
updateLayout: vi.fn(),
getPageIndexAtY: vi.fn(() => 0),
getNearestPageIndex: vi.fn(() => 0),
getPageTop: vi.fn(() => 0),
getPageGap: vi.fn(() => 0),
getLayout: vi.fn(() => ({ pages: [] })),
};
}),
}));

vi.mock('@superdoc/painter-dom', () => ({
Expand Down
Loading
Loading