Skip to content

Commit b6eb3f0

Browse files
committed
fixed: tests
- enhance mock Joplin API - update template selection tests
1 parent 4130a44 commit b6eb3f0

File tree

2 files changed

+55
-117
lines changed

2 files changed

+55
-117
lines changed

tests/mock-joplin-api.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,33 @@ export default {
55
},
66
views: {
77
dialogs: {
8+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
9+
create: async (id: string): Promise<string> => { return id; },
10+
11+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
12+
setHtml: async (handle: string, html: string): Promise<void> => { return; },
13+
14+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
15+
addScript: async (handle: string, script: string): Promise<void> => { return; },
16+
17+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
18+
setButtons: async (handle: string, buttons: unknown[]): Promise<void> => { return; },
19+
20+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
21+
setFitToContent: async (handle: string, fit: boolean): Promise<void> => { return; },
22+
823
// eslint-disable-next-line @typescript-eslint/no-unused-vars
924
showMessageBox: async (message: string): Promise<number> => { return 0; },
1025

1126
// eslint-disable-next-line @typescript-eslint/no-unused-vars
12-
open: async (handle: string): Promise<unknown> => { return ""; }
27+
open: async (handle: string): Promise<unknown> => { return { id: "ok", formData: {} }; }
1328
}
1429
},
1530
settings: {
1631
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1732
globalValue: async (setting: string): Promise<string> => { return ""; },
1833
value: async (setting: string): Promise<string> => { return ""; }
1934
},
35+
versionInfo: async (): Promise<{ platform: string }> => { return { platform: "desktop" }; },
2036
require: (): unknown => { return ""; }
2137
};

tests/utils/templates.spec.ts

Lines changed: 38 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import joplin from "api";
22
import * as tagUtils from "@templates/utils/tags";
33
import { getUserTemplateSelection } from "@templates/utils/templates";
4-
5-
interface DropdownOption {
6-
label: string;
7-
value: string;
8-
}
4+
import { encode } from "html-entities";
95

106
interface TagData {
117
id: string;
@@ -41,12 +37,18 @@ describe("Get user template selection", () => {
4137
}
4238
});
4339

44-
const expectTemplatesSelector = (templates: DropdownOption[], selectedValue: DropdownOption | null) => {
45-
jest.spyOn(joplin.commands, "execute").mockImplementation(async (cmd: string, props: Record<string, unknown>) => {
46-
expect(cmd).toEqual("showPrompt");
47-
expect(props.autocomplete).toEqual(templates);
40+
const expectTemplatesDialog = (selectedTemplateValue: string | null) => {
41+
jest.spyOn(joplin.views.dialogs, "open").mockImplementation(async (handle: string) => {
42+
if (selectedTemplateValue === null) {
43+
return { id: "cancel" };
44+
}
4845
return {
49-
answer: selectedValue
46+
id: "ok",
47+
formData: {
48+
"templates-form": {
49+
template: selectedTemplateValue
50+
}
51+
}
5052
};
5153
});
5254
};
@@ -121,30 +123,17 @@ describe("Get user template selection", () => {
121123
}
122124
]);
123125

124-
const templateOptions = [
125-
{
126-
label: "Template 1",
127-
value: JSON.stringify({
128-
id: "note-id-1",
129-
title: "Template 1",
130-
body: "Template Body"
131-
})
132-
},
133-
{
134-
label: "Template 2",
135-
value: JSON.stringify({
136-
id: "note-id-2",
137-
title: "Template 2",
138-
body: "Template Body"
139-
})
140-
}
141-
];
142-
const selectedTemplate = templateOptions[0];
126+
const selectedNote = {
127+
id: "note-id-1",
128+
title: "Template 1",
129+
body: "Template Body"
130+
};
131+
const selectedTemplateValue = encode(JSON.stringify(selectedNote));
143132

144-
expectTemplatesSelector(templateOptions, selectedTemplate);
133+
expectTemplatesDialog(selectedTemplateValue);
145134
const res = await getUserTemplateSelection(dialogHandle);
146-
testExpectedCalls(joplin.commands.execute, 1);
147-
expect(res).toEqual(selectedTemplate.value);
135+
testExpectedCalls(joplin.views.dialogs.open, 1);
136+
expect(res).toEqual(JSON.stringify(selectedNote));
148137
});
149138

150139
test("should show selector correctly when there are multiple template tags", async () => {
@@ -183,38 +172,17 @@ describe("Get user template selection", () => {
183172
}
184173
]);
185174

186-
const templateOptions = [
187-
{
188-
label: "Template 1",
189-
value: JSON.stringify({
190-
id: "note-id-1",
191-
title: "Template 1",
192-
body: "Template Body"
193-
})
194-
},
195-
{
196-
label: "Template 2",
197-
value: JSON.stringify({
198-
id: "note-id-2",
199-
title: "Template 2",
200-
body: "Template Body"
201-
})
202-
},
203-
{
204-
label: "Template 3",
205-
value: JSON.stringify({
206-
id: "note-id-3",
207-
title: "Template 3",
208-
body: "Template Body"
209-
})
210-
},
211-
];
212-
const selectedTemplate = templateOptions[1];
175+
const selectedNote = {
176+
id: "note-id-2",
177+
title: "Template 2",
178+
body: "Template Body"
179+
};
180+
const selectedTemplateValue = encode(JSON.stringify(selectedNote));
213181

214-
expectTemplatesSelector(templateOptions, selectedTemplate);
182+
expectTemplatesDialog(selectedTemplateValue);
215183
const res = await getUserTemplateSelection(dialogHandle);
216-
testExpectedCalls(joplin.commands.execute, 1);
217-
expect(res).toEqual(selectedTemplate.value);
184+
testExpectedCalls(joplin.views.dialogs.open, 1);
185+
expect(res).toEqual(JSON.stringify(selectedNote));
218186
});
219187

220188
test("should return null if no template is selected", async () => {
@@ -237,29 +205,9 @@ describe("Get user template selection", () => {
237205
}
238206
]);
239207

240-
const templateOptions = [
241-
{
242-
label: "Template 1",
243-
value: JSON.stringify({
244-
id: "note-id-1",
245-
title: "Template 1",
246-
body: "Template Body"
247-
})
248-
},
249-
{
250-
label: "Template 2",
251-
value: JSON.stringify({
252-
id: "note-id-2",
253-
title: "Template 2",
254-
body: "Template Body"
255-
})
256-
}
257-
];
258-
const selectedTemplate = null;
259-
260-
expectTemplatesSelector(templateOptions, selectedTemplate);
208+
expectTemplatesDialog(null);
261209
const res = await getUserTemplateSelection(dialogHandle);
262-
testExpectedCalls(joplin.commands.execute, 1);
210+
testExpectedCalls(joplin.views.dialogs.open, 1);
263211
expect(res).toBeNull();
264212
});
265213

@@ -283,22 +231,12 @@ describe("Get user template selection", () => {
283231
}
284232
]);
285233

286-
const templateOptions = [
287-
{
288-
label: "Template 1",
289-
value: "Template Body"
290-
},
291-
{
292-
label: "Template 2",
293-
value: "Template Body"
294-
}
295-
];
296-
const selectedTemplate = templateOptions[1];
234+
const selectedTemplateValue = encode("Template Body");
297235

298-
expectTemplatesSelector(templateOptions, selectedTemplate);
236+
expectTemplatesDialog(selectedTemplateValue);
299237
const res = await getUserTemplateSelection(dialogHandle, "body");
300-
testExpectedCalls(joplin.commands.execute, 1);
301-
expect(res).toEqual(selectedTemplate.value);
238+
testExpectedCalls(joplin.views.dialogs.open, 1);
239+
expect(res).toEqual("Template Body");
302240
});
303241

304242
test("should sort the templates correctly", async () => {
@@ -326,25 +264,9 @@ describe("Get user template selection", () => {
326264
}
327265
]);
328266

329-
const templateOptions = [
330-
{
331-
label: "Template 1",
332-
value: "Template Body"
333-
},
334-
{
335-
label: "Template 2",
336-
value: "Template Body"
337-
},
338-
{
339-
label: "Template 10",
340-
value: "Template Body"
341-
}
342-
];
343-
const selectedTemplate = null;
344-
345-
expectTemplatesSelector(templateOptions, selectedTemplate);
267+
expectTemplatesDialog(null);
346268
const res = await getUserTemplateSelection(dialogHandle, "body");
347-
testExpectedCalls(joplin.commands.execute, 1);
269+
testExpectedCalls(joplin.views.dialogs.open, 1);
348270
expect(res).toEqual(null);
349271
});
350272
});

0 commit comments

Comments
 (0)