Skip to content

Commit 3ef7a3f

Browse files
committed
chore: fix some test suite
1 parent 14cb254 commit 3ef7a3f

20 files changed

Lines changed: 934 additions & 171 deletions

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
testMatch: ["**/__tests__/**/*.test.ts"],
55
moduleNameMapper: {
66
"^obsidian$": "<rootDir>/src/__mocks__/obsidian.ts",
7+
"^moment$": "<rootDir>/src/__mocks__/moment.js",
78
"^@codemirror/state$": "<rootDir>/src/__mocks__/codemirror-state.ts",
89
"^@codemirror/view$": "<rootDir>/src/__mocks__/codemirror-view.ts",
910
"^@codemirror/language$":

src/__mocks__/moment.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Global moment.js mock
2+
const moment = function(input) {
3+
let date;
4+
if (input instanceof Date) {
5+
date = input;
6+
} else if (typeof input === "string") {
7+
date = new Date(input);
8+
} else if (typeof input === "number") {
9+
date = new Date(input);
10+
} else {
11+
date = new Date();
12+
}
13+
14+
return {
15+
format: function(format) {
16+
if (format === "YYYY-MM-DD") {
17+
return date.toISOString().split("T")[0];
18+
} else if (format === "D") {
19+
return date.getDate().toString();
20+
}
21+
return date.toISOString().split("T")[0];
22+
},
23+
diff: function() {
24+
return 0;
25+
},
26+
startOf: function(unit) {
27+
return this;
28+
},
29+
endOf: function(unit) {
30+
return this;
31+
},
32+
isSame: function(other, unit) {
33+
return true;
34+
},
35+
isSameOrBefore: function(other, unit) {
36+
return true;
37+
},
38+
isSameOrAfter: function(other, unit) {
39+
return true;
40+
},
41+
isBefore: function(other, unit) {
42+
return false;
43+
},
44+
isAfter: function(other, unit) {
45+
return false;
46+
},
47+
isBetween: function(start, end, unit, inclusivity) {
48+
return true;
49+
},
50+
clone: function() {
51+
return moment(date);
52+
},
53+
add: function(amount, unit) {
54+
return this;
55+
},
56+
subtract: function(amount, unit) {
57+
return this;
58+
},
59+
valueOf: function() {
60+
return date.getTime();
61+
},
62+
toDate: function() {
63+
return date;
64+
},
65+
weekday: function(day) {
66+
if (day !== undefined) {
67+
return this;
68+
}
69+
return 0;
70+
},
71+
day: function() {
72+
return date.getDay();
73+
},
74+
date: function() {
75+
return date.getDate();
76+
},
77+
_date: date,
78+
};
79+
};
80+
81+
// Static methods
82+
moment.utc = function() {
83+
return {
84+
format: function() {
85+
return "00:00:00";
86+
},
87+
};
88+
};
89+
90+
moment.duration = function() {
91+
return {
92+
asMilliseconds: function() {
93+
return 0;
94+
},
95+
};
96+
};
97+
98+
moment.locale = function(locale) {
99+
if (locale) {
100+
moment._currentLocale = locale;
101+
return locale;
102+
}
103+
return moment._currentLocale || "en";
104+
};
105+
106+
moment._currentLocale = "en";
107+
108+
moment.weekdaysShort = function(localeData) {
109+
return ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
110+
};
111+
112+
moment.weekdaysMin = function(localeData) {
113+
return ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
114+
};
115+
116+
moment.months = function() {
117+
return ["January", "February", "March", "April", "May", "June",
118+
"July", "August", "September", "October", "November", "December"];
119+
};
120+
121+
moment.monthsShort = function() {
122+
return ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
123+
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
124+
};
125+
126+
module.exports = moment;

src/__mocks__/obsidian.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ export class App {
2727
if (key === "useTab") return false;
2828
return null;
2929
},
30+
// Event system for vault
31+
_events: {} as Record<string, Function[]>,
32+
on: function (eventName: string, callback: Function) {
33+
if (!this._events[eventName]) {
34+
this._events[eventName] = [];
35+
}
36+
this._events[eventName].push(callback);
37+
return { unload: () => this.off(eventName, callback) };
38+
},
39+
off: function (eventName: string, callback: Function) {
40+
if (this._events[eventName]) {
41+
const index = this._events[eventName].indexOf(callback);
42+
if (index > -1) {
43+
this._events[eventName].splice(index, 1);
44+
}
45+
}
46+
},
47+
trigger: function (eventName: string, ...args: any[]) {
48+
if (this._events[eventName]) {
49+
this._events[eventName].forEach(callback => callback(...args));
50+
}
51+
},
52+
getFileByPath: function (path: string) {
53+
// Mock implementation for getFileByPath
54+
return {
55+
path: path,
56+
name: path.split('/').pop() || path,
57+
children: [], // For directory-like behavior
58+
};
59+
},
3060
};
3161

3262
workspace = {
@@ -298,9 +328,16 @@ function momentFn(input?: any) {
298328
};
299329

300330
(momentFn as any).locale = function (locale?: string) {
301-
return locale || "en";
331+
if (locale) {
332+
(momentFn as any)._currentLocale = locale;
333+
return locale;
334+
}
335+
return (momentFn as any)._currentLocale || "en";
302336
};
303337

338+
// Initialize default locale
339+
(momentFn as any)._currentLocale = "en";
340+
304341
(momentFn as any).weekdaysShort = function (localeData?: boolean) {
305342
return ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
306343
};
@@ -309,6 +346,16 @@ function momentFn(input?: any) {
309346
return ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
310347
};
311348

349+
(momentFn as any).months = function () {
350+
return ["January", "February", "March", "April", "May", "June",
351+
"July", "August", "September", "October", "November", "December"];
352+
};
353+
354+
(momentFn as any).monthsShort = function () {
355+
return ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
356+
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
357+
};
358+
312359
export const moment = momentFn as any;
313360

314361
// Mock Component class
@@ -366,6 +413,12 @@ export class Component {
366413
// Mock implementation
367414
return id;
368415
}
416+
417+
private _events: Array<{ unload: () => void }> = [];
418+
419+
registerEvent(eventRef: { unload: () => void }): void {
420+
this._events.push(eventRef);
421+
}
369422
}
370423

371424
// Mock other common Obsidian utilities
@@ -391,4 +444,26 @@ export function debounce<T extends (...args: any[]) => any>(
391444
}) as T;
392445
}
393446

447+
// Mock EditorSuggest class
448+
export abstract class EditorSuggest<T> extends Component {
449+
app: App;
450+
451+
constructor(app: App) {
452+
super();
453+
this.app = app;
454+
}
455+
456+
abstract getSuggestions(context: any): T[] | Promise<T[]>;
457+
abstract renderSuggestion(suggestion: T, el: HTMLElement): void;
458+
abstract selectSuggestion(suggestion: T, evt: MouseEvent | KeyboardEvent): void;
459+
460+
onTrigger(cursor: any, editor: any, file: any): any {
461+
return null;
462+
}
463+
464+
close(): void {
465+
// Mock implementation
466+
}
467+
}
468+
394469
// Add any other Obsidian classes or functions needed for tests

src/__tests__/QuickCaptureModal.integration.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,48 @@ jest.mock("obsidian", () => ({
5050
Platform: { isPhone: false },
5151
MarkdownRenderer: jest.fn(),
5252
moment: () => ({ format: jest.fn(() => "2025-01-04") }),
53+
EditorSuggest: class {
54+
constructor() {}
55+
getSuggestions() { return []; }
56+
renderSuggestion() {}
57+
selectSuggestion() {}
58+
onTrigger() { return null; }
59+
close() {}
60+
},
5361
}));
5462

63+
// Mock moment module
64+
jest.mock("moment", () => {
65+
const moment = function(input) {
66+
return {
67+
format: () => "2024-01-01",
68+
diff: () => 0,
69+
startOf: () => moment(),
70+
endOf: () => moment(),
71+
isSame: () => true,
72+
isSameOrBefore: () => true,
73+
isSameOrAfter: () => true,
74+
isBefore: () => false,
75+
isAfter: () => false,
76+
isBetween: () => true,
77+
clone: () => moment(),
78+
add: () => moment(),
79+
subtract: () => moment(),
80+
valueOf: () => Date.now(),
81+
toDate: () => new Date(),
82+
weekday: () => 0,
83+
day: () => 1,
84+
date: () => 1,
85+
};
86+
};
87+
moment.locale = jest.fn(() => "en");
88+
moment.utc = () => ({ format: () => "00:00:00" });
89+
moment.duration = () => ({ asMilliseconds: () => 0 });
90+
moment.weekdaysShort = () => ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
91+
moment.weekdaysMin = () => ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
92+
return moment;
93+
});
94+
5595
jest.mock("../editor-ext/markdownEditor", () => ({
5696
createEmbeddableMarkdownEditor: jest.fn(() => ({
5797
value: "",

src/__tests__/SuggestBackwardCompatibility.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,47 @@ jest.mock("obsidian", () => ({
99
EditorPosition: jest.fn(),
1010
EditorSuggest: class {
1111
constructor() {}
12+
getSuggestions() { return []; }
13+
renderSuggestion() {}
14+
selectSuggestion() {}
15+
onTrigger() { return null; }
16+
close() {}
1217
},
1318
setIcon: jest.fn(),
1419
}));
1520

21+
// Mock moment module
22+
jest.mock("moment", () => {
23+
const moment = function(input) {
24+
return {
25+
format: () => "2024-01-01",
26+
diff: () => 0,
27+
startOf: () => moment(),
28+
endOf: () => moment(),
29+
isSame: () => true,
30+
isSameOrBefore: () => true,
31+
isSameOrAfter: () => true,
32+
isBefore: () => false,
33+
isAfter: () => false,
34+
isBetween: () => true,
35+
clone: () => moment(),
36+
add: () => moment(),
37+
subtract: () => moment(),
38+
valueOf: () => Date.now(),
39+
toDate: () => new Date(),
40+
weekday: () => 0,
41+
day: () => 1,
42+
date: () => 1,
43+
};
44+
};
45+
moment.locale = jest.fn(() => "en");
46+
moment.utc = () => ({ format: () => "00:00:00" });
47+
moment.duration = () => ({ asMilliseconds: () => 0 });
48+
moment.weekdaysShort = () => ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
49+
moment.weekdaysMin = () => ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
50+
return moment;
51+
});
52+
1653
// Mock plugin
1754
const mockPlugin = {
1855
app: {

src/__tests__/SuggestPerformance.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,47 @@ jest.mock("obsidian", () => ({
1010
TFile: jest.fn(),
1111
EditorSuggest: class {
1212
constructor() {}
13+
getSuggestions() { return []; }
14+
renderSuggestion() {}
15+
selectSuggestion() {}
16+
onTrigger() { return null; }
17+
close() {}
1318
},
1419
setIcon: jest.fn(),
1520
}));
1621

22+
// Mock moment module
23+
jest.mock("moment", () => {
24+
const moment = function(input) {
25+
return {
26+
format: () => "2024-01-01",
27+
diff: () => 0,
28+
startOf: () => moment(),
29+
endOf: () => moment(),
30+
isSame: () => true,
31+
isSameOrBefore: () => true,
32+
isSameOrAfter: () => true,
33+
isBefore: () => false,
34+
isAfter: () => false,
35+
isBetween: () => true,
36+
clone: () => moment(),
37+
add: () => moment(),
38+
subtract: () => moment(),
39+
valueOf: () => Date.now(),
40+
toDate: () => new Date(),
41+
weekday: () => 0,
42+
day: () => 1,
43+
date: () => 1,
44+
};
45+
};
46+
moment.locale = jest.fn(() => "en");
47+
moment.utc = () => ({ format: () => "00:00:00" });
48+
moment.duration = () => ({ asMilliseconds: () => 0 });
49+
moment.weekdaysShort = () => ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
50+
moment.weekdaysMin = () => ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
51+
return moment;
52+
});
53+
1754
// Mock plugin with realistic data
1855
const mockPlugin = {
1956
app: {

0 commit comments

Comments
 (0)