logancyang_obsidian-copilot/__mocks__/obsidian.js
Zero Liu 710acc2436
chore(lint): enable obsidianmd/no-global-this and fix violations (#2413)
Removes the deferred `"off"` override and rewrites the 55 violations
(all in test/mocks/Node-script files) to use `window.*` instead of
`global.*`. Production code was already cleaned up in #2401.

- 18 test files + __mocks__/obsidian.js + jest.setup.js: mechanical
  `global.X` → `window.X` (Jest runs under jsdom, so window === globalThis)
- scripts/printPromptDebugEntry.ts: inline eslint-disable — Node-only
  debug script with no window available

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 01:14:47 -07:00

109 lines
3.5 KiB
JavaScript

// __mocks__/obsidian.js
import yaml from "js-yaml";
module.exports = {
// Reason: normalizePath is used by projectPaths.ts; identity function is sufficient for tests
normalizePath: jest.fn().mockImplementation((p) => p),
moment: jest.requireActual("moment"),
Vault: jest.fn().mockImplementation(() => {
return {
getMarkdownFiles: jest.fn().mockImplementation(() => {
// Return an array of mock markdown file objects
return [
{ path: "test/test2/note1.md" },
{ path: "test/note2.md" },
{ path: "test2/note3.md" },
{ path: "note4.md" },
];
}),
cachedRead: jest.fn().mockImplementation((file) => {
// Simulate reading file contents. You can adjust the content as needed for your tests.
const fileContents = {
"test/test2/note1.md": "---\ntags: [Tag1, tag2]\n---\nContent of note1",
"test/note2.md": "---\ntags: [tag2, tag3]\n---\nContent of note2",
"test2/note3.md": "something else ---\ntags: [false_tag]\n---\nContent of note3",
"note4.md": "---\ntags: [tag1, Tag4]\n---\nContent of note4",
};
return Promise.resolve(fileContents[file.path]);
}),
};
}),
Platform: {
isDesktop: true,
},
parseYaml: jest.fn().mockImplementation((content) => {
return yaml.load(content);
}),
Modal: class Modal {
constructor() {
this.open = jest.fn();
this.close = jest.fn();
this.onOpen = jest.fn();
this.onClose = jest.fn();
}
},
App: jest.fn().mockImplementation(() => ({
workspace: {
getActiveFile: jest.fn(),
},
vault: {
read: jest.fn(),
},
})),
ItemView: jest.fn().mockImplementation(function () {
this.containerEl = window.document.createElement("div");
this.onOpen = jest.fn();
this.onClose = jest.fn();
this.getDisplayText = jest.fn().mockReturnValue("Mock View");
this.getViewType = jest.fn().mockReturnValue("mock-view");
this.getIcon = jest.fn().mockReturnValue("document");
}),
Notice: jest.fn().mockImplementation(function (message) {
this.message = message;
this.noticeEl = window.document.createElement("div");
this.hide = jest.fn();
}),
TFile: jest.fn().mockImplementation(function (path) {
this.path = path;
this.name = path.split("/").pop();
this.basename = this.name.replace(/\.[^/.]+$/, "");
this.extension = path.split(".").pop();
}),
TFolder: jest.fn().mockImplementation(function (path) {
this.path = path || "";
this.name = this.path.split("/").pop() || "";
}),
WorkspaceLeaf: jest.fn().mockImplementation(function () {
this.view = null;
this.setViewState = jest.fn();
this.detach = jest.fn();
this.getViewState = jest.fn().mockReturnValue({});
}),
};
// Mock the global app object
window.app = {
vault: {
getAbstractFileByPath: jest.fn().mockReturnValue({
name: "test-file.md",
path: "test-file.md",
}),
read: jest.fn().mockResolvedValue("test content"),
modify: jest.fn().mockResolvedValue(undefined),
getMarkdownFiles: jest.fn().mockReturnValue([]),
getAllLoadedFiles: jest.fn().mockReturnValue([]),
},
workspace: {
getActiveFile: jest.fn().mockReturnValue(null),
getLeaf: jest.fn().mockReturnValue({
openFile: jest.fn().mockResolvedValue(undefined),
}),
},
metadataCache: {
getFirstLinkpathDest: jest.fn().mockReturnValue(null),
getFileCache: jest.fn().mockReturnValue(null),
},
fileManager: {
trashFile: jest.fn().mockResolvedValue(undefined),
},
};