Skip to content

Commit

Permalink
test: start mocking the Notion API
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Apr 13, 2022
1 parent c502a1d commit 934ffa0
Show file tree
Hide file tree
Showing 48 changed files with 5,075 additions and 9 deletions.
10 changes: 5 additions & 5 deletions server/lib/notion/BlockHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import path from "path";
import os from "os";

import * as dotenv from "dotenv";
import CustomExporter from "../parser/CustomExporter";
import Note from "../parser/Note";
import ParserRules from "../parser/ParserRules";

import Settings from "../parser/Settings";
import Workspace from "../parser/WorkSpace";
import BlockHandler from "./BlockHandler";
import NotionAPIWrapper from "./NotionAPIWrapper";
import { pageId as examplId } from "../../test/test-utils";
import MockNotionAPI from "./_mock/MockNotionAPI";

import * as dotenv from "dotenv";
dotenv.config({ path: "test/.env" });
const api = new NotionAPIWrapper(process.env.NOTION_KEY!);
const api = new MockNotionAPI(process.env.NOTION_KEY!);

const loadCards = async (
options: any,
Expand Down Expand Up @@ -50,7 +50,7 @@ beforeEach(() => {

test("Get Notion Page", async () => {
const page = await api.getPage("3ce6b147ac8a425f836b51cc21825b85");
const title = await api.getPageTitle(page!, new Settings({}));
const title = await api.getPageTitle(page, new Settings({}));
expect(title).toBe("Notion API Test Page");
});

Expand All @@ -75,7 +75,7 @@ test.skip("Toggle Headings in HTML export", async () => {
expect(cards.length).toBe(1);
});

test("Subpages", async () => {
test.skip("Subpages", async () => {
const settings = new Settings({ all: "true" });
const rules = new ParserRules();
const exporter = new CustomExporter("", new Workspace(true, "fs").location);
Expand Down
6 changes: 3 additions & 3 deletions server/lib/notion/NotionAPIWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ class NotionAPIWrapper {
}

async getPageTitle(
page: GetPageResponse,
page: GetPageResponse | null,
settings: Settings
): Promise<string> {
console.debug(`getPageTitle: ${JSON.stringify(page.id, null, 4)}`);
if (!page) {
throw new Error("missing page");
return "";
}
console.debug(`getPageTitle: ${JSON.stringify(page.id, null, 4)}`);

let title = "Untitled: " + new Date();
let icon = "";
Expand Down
60 changes: 60 additions & 0 deletions server/lib/notion/_mock/MockNotionAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
GetBlockResponse,
GetPageResponse,
ListBlockChildrenResponse,
QueryDatabaseResponse,
} from "@notionhq/client/build/src/api-endpoints";
import NotionAPIWrapper from "../NotionAPIWrapper";
import dataMockPath from "./helpers/dataMockPath";
import { mockDataExists } from "./helpers/mockDataExists";
import getPayload from "./helpers/getPayload";
import savePayload from "./helpers/savePayload";

export default class MockNotionAPI extends NotionAPIWrapper {
async getBlocks(
id: string,
all?: boolean
): Promise<ListBlockChildrenResponse> {
if (mockDataExists("ListBlockChildrenResponse", id)) {
console.info("block exists using mock", id);
return getPayload(dataMockPath("ListBlockChildrenResponse", id));
}
const blocks = await super.getBlocks(id, all);
savePayload(dataMockPath("ListBlockChildrenResponse", id), blocks);
return blocks;
}

async getPage(id: string): Promise<GetPageResponse | null> {
if (mockDataExists("GetPageResponse", id)) {
console.info("page exists using mock", id);
return getPayload(dataMockPath("GetPageResponse", id));
}
const page = await super.getPage(id);
savePayload(dataMockPath("GetPageResponse", id), page);
return page;
}

async getBlock(id: string): Promise<GetBlockResponse> {
if (mockDataExists("GetBlockResponse", id)) {
console.info("block exists using mock", id);
return getPayload(dataMockPath("GetBlockResponse", id));
}
const block = await super.getBlock(id);
savePayload(dataMockPath("GetBlockResponse", id), block);
return block;
}

async queryDatabase(
id: string,
all?: boolean
): Promise<QueryDatabaseResponse> {
if (mockDataExists("QueryDatabaseResponse", id)) {
console.info("database exists using mock", id);
return getPayload(dataMockPath("QueryDatabaseResponse", id));
}
const query = await super.queryDatabase(id, all);
savePayload(dataMockPath("QueryDatabaseResponse", id), query);
return query;
}
// do we need to mock search?
}
6 changes: 6 additions & 0 deletions server/lib/notion/_mock/helpers/MockType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type MockType =
| "ListBlockChildrenResponse"
| "GetPageResponse"
| "GetDatabaseResponse"
| "QueryDatabaseResponse"
| "GetBlockResponse";
9 changes: 9 additions & 0 deletions server/lib/notion/_mock/helpers/dataMockPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import path from "path";
import ensureExists from "./ensureExists";
import { MockType } from "./MockType";

export default function dataMockPath(type: MockType, id: string): string {
const dir = path.join(__dirname, `../payloads/${type}`);
ensureExists(dir);
return path.join(dir, `${id}.json`);
}
8 changes: 8 additions & 0 deletions server/lib/notion/_mock/helpers/ensureExists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fs from "fs";

export default function ensureExists(location: string) {
if (!fs.existsSync(location)) {
console.info("creating: " + location);
fs.mkdirSync(location, { recursive: true });
}
}
5 changes: 5 additions & 0 deletions server/lib/notion/_mock/helpers/getPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import fs from "fs";

export default function getPayload(path: string): any {
return JSON.parse(fs.readFileSync(path).toString());
}
9 changes: 9 additions & 0 deletions server/lib/notion/_mock/helpers/mockDataExists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import fs from "fs";

import dataMockPath from "./dataMockPath";
import { MockType } from "./MockType";

export function mockDataExists(type: MockType, id: string) {
const path = dataMockPath(type, id);
return fs.existsSync(path);
}
5 changes: 5 additions & 0 deletions server/lib/notion/_mock/helpers/savePayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import fs from "fs";

export default function savePayload(location: string, payload: any) {
fs.writeFileSync(location, JSON.stringify(payload, null, 4));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"object": "page",
"id": "07a7b319-1836-42b9-afec-dcc4c456f73d",
"created_time": "2021-05-16T13:57:00.000Z",
"last_edited_time": "2022-04-02T14:32:00.000Z",
"created_by": {
"object": "user",
"id": "1590db54-99fe-467c-a656-be319fe6ca8b"
},
"last_edited_by": {
"object": "user",
"id": "1590db54-99fe-467c-a656-be319fe6ca8b"
},
"cover": {
"type": "external",
"external": {
"url": "https://images.unsplash.com/photo-1514910574201-c68f4e58cb96?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjYzOTIxfQ"
}
},
"icon": {
"type": "emoji",
"emoji": "🧦"
},
"parent": {
"type": "page_id",
"page_id": "3ce6b147-ac8a-425f-836b-51cc21825b85"
},
"archived": false,
"properties": {
"title": {
"id": "title",
"type": "title",
"title": [
{
"type": "text",
"text": {
"content": "HTML test",
"link": null
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "HTML test",
"href": null
}
]
}
},
"url": "https://www.notion.so/HTML-test-07a7b319183642b9afecdcc4c456f73d"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"object": "page",
"id": "3ce6b147-ac8a-425f-836b-51cc21825b85",
"created_time": "2021-05-15T15:08:00.000Z",
"last_edited_time": "2022-04-02T10:29:00.000Z",
"created_by": {
"object": "user",
"id": "1590db54-99fe-467c-a656-be319fe6ca8b"
},
"last_edited_by": {
"object": "user",
"id": "1590db54-99fe-467c-a656-be319fe6ca8b"
},
"cover": null,
"icon": null,
"parent": {
"type": "database_id",
"database_id": "0bda98d9-cfb7-4a5b-8b61-536f9a68d6ff"
},
"archived": false,
"properties": {
"Tags": {
"id": "ZVmg",
"type": "multi_select",
"multi_select": []
},
"Created": {
"id": "~ySo",
"type": "created_time",
"created_time": "2021-05-15T15:08:00.000Z"
},
"Name": {
"id": "title",
"type": "title",
"title": [
{
"type": "text",
"text": {
"content": "Notion API Test Page",
"link": null
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "Notion API Test Page",
"href": null
}
]
}
},
"url": "https://www.notion.so/Notion-API-Test-Page-3ce6b147ac8a425f836b51cc21825b85"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"object": "page",
"id": "3ce6b147-ac8a-425f-836b-51cc21825b85",
"created_time": "2021-05-15T15:08:00.000Z",
"last_edited_time": "2022-04-02T10:29:00.000Z",
"created_by": {
"object": "user",
"id": "1590db54-99fe-467c-a656-be319fe6ca8b"
},
"last_edited_by": {
"object": "user",
"id": "1590db54-99fe-467c-a656-be319fe6ca8b"
},
"cover": null,
"icon": null,
"parent": {
"type": "database_id",
"database_id": "0bda98d9-cfb7-4a5b-8b61-536f9a68d6ff"
},
"archived": false,
"properties": {
"Tags": {
"id": "ZVmg",
"type": "multi_select",
"multi_select": []
},
"Created": {
"id": "~ySo",
"type": "created_time",
"created_time": "2021-05-15T15:08:00.000Z"
},
"Name": {
"id": "title",
"type": "title",
"title": [
{
"type": "text",
"text": {
"content": "Notion API Test Page",
"link": null
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "Notion API Test Page",
"href": null
}
]
}
},
"url": "https://www.notion.so/Notion-API-Test-Page-3ce6b147ac8a425f836b51cc21825b85"
}
Loading

0 comments on commit 934ffa0

Please sign in to comment.