Skip to content

Commit d62bdbd

Browse files
committedJun 16, 2023
WIP Get a test working
1 parent 4cdf900 commit d62bdbd

File tree

4 files changed

+1275
-26
lines changed

4 files changed

+1275
-26
lines changed
 

‎lib/plugin.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const plugin = {
22
constants: {
33
booksImportedLabel: "Readwise books imported into table",
4-
breakToDecadeBookCount: 10,
5-
breakToYearBookCount: 50,
4+
breakToDecadeBookCount: 3,
5+
breakToYearBookCount: 6,
66
defaultBaseTag: "library",
77
dashboardBookListTitle: "Readwise Book List",
88
defaultDashboardNoteTitle: "Readwise Library Dashboard",
@@ -12,7 +12,7 @@ const plugin = {
1212
lastUpdatedContentLabel: "Next sync for content updated after: ",
1313
maxReplaceContentLength: 100000, // Empirically derived
1414
maxHighlightLimit: 5000,
15-
rateLimit: 20, // Max requests per minute (20 is Readwise limit for Books and Highlights APIs)
15+
rateLimit: 2, // Max requests per minute (20 is Readwise limit for Books and Highlights APIs)
1616
readwiseBookDetailURL: bookId => `https://readwise.io/api/v2/books/${ bookId }`,
1717
readwiseBookIndexURL: "https://readwise.io/api/v2/books",
1818
readwiseHighlightsIndexURL: "https://readwise.io/api/v2/highlights",
@@ -108,8 +108,12 @@ const plugin = {
108108
await app.alert(`✅ ${ bookCount } book${ bookCount === "1" ? "" : "s" } fetched & refreshed successfully!`);
109109
}
110110
} catch (error) {
111-
await app.alert(String(error));
112-
this._abortExecution = true;
111+
if (this._testEnvironment) {
112+
throw(error);
113+
} else {
114+
await app.alert(String(error));
115+
this._abortExecution = true;
116+
}
113117
}
114118
},
115119

@@ -898,5 +902,7 @@ const plugin = {
898902
this._forceReprocess = false;
899903
this._lastRequestTime = null;
900904
this._requestsCount = 0;
905+
if (this._testEnvironment === undefined) this._testEnvironment = false;
901906
}
902907
}
908+
export default plugin;

‎lib/plugin.test.js

+33-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { jest } from "@jest/globals"
22
import { mockPlugin, mockApp } from "./test-helpers.js"
33

4+
import plugin from "./plugin.js"
5+
46
// --------------------------------------------------------------------------------------
57
// Note that some of these tests actually make calls to OpenAI. Normally tests would be mocked for
68
// a remote call, but for Bill's current purposes, it's pretty useful to be able to see what the real
@@ -12,28 +14,39 @@ describe("plugin", () => {
1214
describe("with a mocked app", () => {
1315
const app = mockApp();
1416

15-
it("should look up thesaurus entries from trivial input", async () => {
16-
const noteContent = `
17-
Author: Leo Tolstoy
18-
Title: War and Peace
19-
Date: 1869
20-
Book ID: 23
17+
it("should evaluate the lookup expression", async () => {
18+
const noteUUID = 123;
19+
const readwiseID = 456;
20+
const content = `
21+
# Library Details
22+
Last synced at: June 15, 2023 3:39 PM
23+
Oldest update synced in: August 5, 2022
24+
Next sync for content updated after: August 5, 2022
25+
Readwise books imported into table: 18
26+
Book count reported by Readwise: 25
2127
22-
### Yo mama so fat, she a multi-part download
23-
Highlighted at: January 3, 2021 12:00 AM
24-
Readwise URL: https://readwise.io/abc123
25-
`;
26-
const noteHandles = [ { name: "War and Peace by Tolstoy (R-ID #23)", uuid: "abc123" } ];
27-
app.notes.filter = jest.fn().mockResolvedValue(noteHandles)
28-
app.getNoteContent = jest.fn().mockResolvedValue(noteContent);
29-
await plugin.noteOption["Sync Readwise"](app, "abc123");
30-
});
28+
# ${ plugin.constants.dashboardBookListTitle }
29+
${ plugin._tablePreambleContent() }
30+
| ![book cover](https://www.gitclear.com/image.jpg) | William Harding | books | [kindle](https://kindle.com/blah) | [5 highlights](https://amplenote.com/notes/abc333) | August 5, 2022 | [Readwise link](${ readwiseID }) |
31+
| ![book cover](https://www.gitclear.com/image.jpg) | William Harding | books | [kindle](https://kindle.com/blah) | [5 highlights](https://amplenote.com/notes/abc333) | August 5, 2022 | [Readwise link](999) |
32+
`
33+
const noteHandle = {
34+
content: () => content,
35+
name: plugin.constants.defaultDashboardNoteTitle,
36+
uuid: noteUUID,
37+
};
38+
app.findNote = jest.fn().mockResolvedValue(noteHandle);
39+
app.notes.find = jest.fn().mockResolvedValue(noteHandle);
40+
app.getNoteContent = jest.fn().mockResolvedValue(content);
41+
plugin._readwiseMakeRequest = jest.fn().mockResolvedValue({ count: 0 });
42+
async function* getBook() {
43+
yield* ({ something: "for sure" })
44+
}
45+
plugin._readwiseFetchBooks = getBook.bind(plugin);
46+
plugin._testEnvironment = true;
3147

32-
it("should evaluate the lookup expression", async () => {
33-
app.notes.find.mockReturnValue({
34-
content: () => `To be, or not to be, that is the {${ plugin.constants.pluginName }: lookup}.`
35-
});
36-
const result = await plugin.insertText.lookup(app);
48+
app.notes.find.mockReturnValue({ content: () => content });
49+
const result = await plugin.noteOption["Sync all"](app, noteUUID);
3750
expect(result).toBe("question.");
3851
});
3952
});

‎package-lock.json

+1,229-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
"devDependencies": {
88
"@types/jest": "^29.5.0",
99
"jest": "^29.5.0",
10+
"jest-environment-jsdom": "^29.5.0",
1011
"ts-jest": "^29.1.0"
1112
},
1213
"main": "lib/index.js",
1314
"name": "yappy",
1415
"scripts": {
1516
"test": "jest"
1617
},
18+
"testEnvironment": "jsdom",
1719
"type": "module",
1820
"version": "0.1.1"
1921
}

0 commit comments

Comments
 (0)
Please sign in to comment.