Skip to content

Commit 391915e

Browse files
Merge pull request #37 from AB1908/test
Add tests
2 parents a6250cb + 97ed243 commit 391915e

13 files changed

+7484
-1470
lines changed

__mocks__/obsidian.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { RequestUrlParam, RequestUrlResponse } from "obsidian";
2+
3+
export function requestUrl(request: RequestUrlParam): Promise<RequestUrlResponse> {
4+
return fetch(request.url, {
5+
method: request.method,
6+
headers: request.headers,
7+
body: request.body,
8+
}).then(async (response) => {
9+
if (response.status >= 400 && request.throw) {
10+
throw new Error(`Request failed, ${response.status}`);
11+
}
12+
13+
// Turn response headers into Record<string, string> object
14+
const headers: Record<string, string> = {};
15+
response.headers.forEach((value, key) => {
16+
headers[key] = value;
17+
});
18+
19+
const arraybuffer = await response.arrayBuffer();
20+
const text = arraybuffer ? new TextDecoder().decode(arraybuffer) : '';
21+
const json = text ? JSON.parse(text) : {};
22+
23+
let response_body: RequestUrlResponse = {
24+
status: response.status,
25+
headers: headers,
26+
arrayBuffer: arraybuffer,
27+
json: json,
28+
text: text,
29+
};
30+
return response_body;
31+
});
32+
}

jest.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
"roots": [
3+
"<rootDir>/src",
4+
"<rootDir>"
5+
],
6+
"testMatch": [
7+
"**/__tests__/**/*.+(ts|tsx|js)",
8+
"**/?(*.)+(spec|test).+(ts|tsx|js)"
9+
],
10+
"transform": {
11+
"^.+\\.(ts|tsx)$": "ts-jest"
12+
},
13+
}

0 commit comments

Comments
 (0)