Skip to content

Commit a4e32bd

Browse files
committed
fix: resolve test state pollution and update editor types
1 parent c487eb6 commit a4e32bd

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

src/mainview/editor/EditorManager.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ export class EditorManager {
1717
private static instance: Editor | null = null;
1818
private static autoSaveTimeout: any = null;
1919

20+
/**
21+
* Resets the editor instance and global state reference.
22+
* Useful for testing and clean teardown.
23+
*/
24+
static reset(): void {
25+
if (this.instance) {
26+
this.instance.destroy();
27+
this.instance = null;
28+
}
29+
state.editor = null;
30+
}
31+
2032
/**
2133
* Initializes the editor in the specified element.
2234
*/

src/mainview/state/workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const state = {
1414
expandedPaths: new Set<string>(), // Paths of folders currently expanded
1515
openTabs: new Map<string, TabState>(),
1616
untitledCount: 0,
17-
editor: null as unknown as Editor,
17+
editor: null as Editor | null,
1818
activeSidebarTab: 'explorer' as 'explorer' | 'search',
1919
searchResults: [] as any[],
2020
editorSettings: {

tests/manager-editor.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test, describe, beforeEach, spyOn, mock } from "bun:test";
1+
import { expect, test, describe, beforeEach, afterEach, spyOn, mock } from "bun:test";
22
import "./setup";
33
import { EditorManager } from "../src/mainview/editor/EditorManager";
44
import { state } from "../src/mainview/state/workspace";
@@ -53,24 +53,22 @@ describe("EditorManager", () => {
5353

5454
test("init should create editor instance and bind events", async () => {
5555
const editorEl = document.getElementById("editor") as HTMLElement;
56-
EditorManager.init(editorEl);
56+
const editor = EditorManager.init(editorEl);
5757

58-
expect(state.editor).toBeDefined();
58+
expect(state.editor).toBe(editor);
5959
expect(state.editor instanceof MockEditor).toBe(true);
60-
61-
const autoSaveToggle = document.getElementById("autoSaveToggle") as HTMLInputElement;
62-
expect(autoSaveToggle).toBeDefined();
60+
expect(state.editor!.options.element).toBe(editorEl);
6361
});
6462

6563
test("getHTML/setContent should work", async () => {
6664
const editorEl = document.getElementById("editor") as HTMLElement;
6765
EditorManager.init(editorEl);
6866

6967
const testHtml = "<h1>Test</h1>";
70-
(state.editor.commands.setContent as any).mockClear();
68+
(state.editor!.commands.setContent as any).mockClear();
7169

7270
await EditorManager.setContent(testHtml);
73-
expect(state.editor.commands.setContent).toHaveBeenCalledWith(testHtml);
71+
expect(state.editor!.commands.setContent).toHaveBeenCalledWith(testHtml);
7472

7573
const content = EditorManager.getHTML();
7674
expect(content).toBe("<p>mock content</p>");
@@ -85,7 +83,7 @@ describe("EditorManager", () => {
8583
state.openTabs.set("test.md", { filePath: "test.md", isDirty: false });
8684

8785
// Trigger update
88-
(EditorManager as any).handleUpdate(state.editor);
86+
(EditorManager as any).handleUpdate(state.editor!);
8987

9088
// Wait for auto-save timeout (2000ms in EditorManager)
9189
return new Promise<void>((resolve) => {

tests/performance.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, it, expect, mock, beforeEach, spyOn } from "bun:test";
33

44
import { Editor } from "@tiptap/core";
55
import { setEditorContent } from "../src/mainview/editor";
6+
import { EditorManager } from "../src/mainview/editor/EditorManager";
67
import { state } from "../src/mainview/state/workspace";
78

89
// Mock the editor instance
@@ -17,6 +18,7 @@ const mockEditor = {
1718

1819
describe("Performance Optimization Tests", () => {
1920
beforeEach(() => {
21+
EditorManager.reset();
2022
state.editor = mockEditor;
2123
});
2224

0 commit comments

Comments
 (0)