Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ jobs:
json-summary-path: packages/conventional-gitmoji/coverage/coverage-summary.json
vite-config-path: packages/conventional-gitmoji/vitest.config.ts

- name: "📊 Coverage (notion)"
id: pull-coverage-notion
if: always()
uses: davelosert/vitest-coverage-report-action@v2
with:
github-token: ${{ secrets.GH_BOT_TOKEN }}
name: "@jeromefitz/notion"
json-final-path: packages/notion/coverage/coverage-final.json
json-summary-path: packages/notion/coverage/coverage-summary.json
vite-config-path: packages/notion/vitest.config.ts

- name: "📊 Coverage (release-notes-generator)"
id: pull-coverage-release-notes-generator
if: always()
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- canary
- develop
- ci/**
- deps/**
- feat/**
- feature/**
- fix/**
Expand Down
7 changes: 5 additions & 2 deletions packages/notion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@
"dev": "tsdown --watch",
"lint:ts": "tsc --noEmit --declaration",
"semantic-release": "semantic-release",
"semantic-release:dry": "semantic-release --dry-run"
"semantic-release:dry": "semantic-release --dry-run",
"test:unit": "NODE_NO_WARNINGS=1 vitest run",
"test:unit:coverage": "NODE_NO_WARNINGS=1 vitest run --coverage",
"test:watch": "NODE_NO_WARNINGS=1 vitest run --watch"
},
"dependencies": {
"@notionhq/client": "4.0.2",
"@notionhq/client": "5.22.0",
"date-fns": "4.4.0",
"github-slugger": "2.0.0"
},
Expand Down
91 changes: 91 additions & 0 deletions packages/notion/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

const { retrieveMock, queryMock } = vi.hoisted(() => ({
retrieveMock: vi.fn<(args: { database_id: string }) => Promise<unknown>>(),
queryMock: vi.fn<(args: Record<string, unknown>) => Promise<unknown>>(),
}));

vi.mock("@notionhq/client", () => ({
Client: vi.fn<() => unknown>(function MockClient() {
return {
blocks: { children: { list: vi.fn<() => Promise<unknown>>() } },
databases: { retrieve: retrieveMock },
dataSources: { query: queryMock },
pages: { retrieve: vi.fn<() => Promise<unknown>>() },
};
}),
}));

const { Client } = await import("./index.js");

describe("Client custom.getDatabasesByIdQuery", () => {
beforeEach(() => {
retrieveMock.mockReset();
queryMock.mockReset();
});

it("resolves the data source before querying", async () => {
retrieveMock.mockResolvedValue({ data_sources: [{ id: "ds_1", name: "Pages" }] });
queryMock.mockResolvedValue({ has_more: false, next_cursor: null, results: [] });

const client = new Client({ auth: "token" });
await client.custom.getDatabasesByIdQuery({ database_id: "db_1" });

expect(retrieveMock).toHaveBeenCalledWith({ database_id: "db_1" });
expect(queryMock).toHaveBeenCalledWith(expect.objectContaining({ data_source_id: "ds_1" }));
});

it("returns an empty array without querying when database_id is falsy", async () => {
const client = new Client({ auth: "token" });
const result = await client.custom.getDatabasesByIdQuery({ database_id: undefined });

expect(result).toEqual([]);
expect(retrieveMock).not.toHaveBeenCalled();
expect(queryMock).not.toHaveBeenCalled();
});

it("throws when the database has no data sources", async () => {
retrieveMock.mockResolvedValue({ data_sources: [] });
const client = new Client({ auth: "token" });

await expect(client.custom.getDatabasesByIdQuery({ database_id: "db_1" })).rejects.toThrow(
"No data source found for Notion database db_1",
);
expect(queryMock).not.toHaveBeenCalled();
});

it("throws when the response is a partial database object", async () => {
retrieveMock.mockResolvedValue({ id: "db_1", object: "database" });
const client = new Client({ auth: "token" });

await expect(client.custom.getDatabasesByIdQuery({ database_id: "db_1" })).rejects.toThrow(
"No data source found for Notion database db_1",
);
});
});

describe("Client custom.getQuery", () => {
beforeEach(() => {
retrieveMock.mockReset();
queryMock.mockReset();
});

it("resolves the data source and queries it for the EPISODES route", async () => {
retrieveMock.mockResolvedValue({ data_sources: [{ id: "ds_episodes", name: "Episodes" }] });
queryMock.mockResolvedValue({ results: [] });

const client = new Client({
auth: "token",
config: { NOTION: { EPISODES: { database_id: "db_episodes" } } },
});

await client.custom.getQuery({
reqQuery: { databaseType: "EPISODES", podcasts: "podcast-1,podcast-2" },
});

expect(retrieveMock).toHaveBeenCalledWith({ database_id: "db_episodes" });
expect(queryMock).toHaveBeenCalledWith(
expect.objectContaining({ data_source_id: "ds_episodes" }),
);
});
});
24 changes: 22 additions & 2 deletions packages/notion/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ type DataTypesProps = {
class Client extends _Client {
#config?: any;

#getDataSourceId = async (database_id: string): Promise<string> => {
const database = await this.databases.retrieve({ database_id });
const data_source_id = "data_sources" in database ? database.data_sources[0]?.id : undefined;
if (!data_source_id) {
throw new Error(`No data source found for Notion database ${database_id}`);
}
return data_source_id;
};

#queryDatabase = async ({
database_id,
...rest
}: {
database_id: string;
[key: string]: any;
}) => {
const data_source_id = await this.#getDataSourceId(database_id);
return await this.dataSources.query({ data_source_id, ...rest });
};

// @todo(notion) throw error if `config` is not passed
public readonly custom = {
getBlocksByIdChildren: async (props) =>
Expand All @@ -56,7 +76,7 @@ class Client extends _Client {
getDatabasesByIdQuery: async (props) =>
await getDatabasesByIdQuery({
...props,
getDatabasesQuery: this.databases.query,
getDatabasesQuery: this.#queryDatabase,
}),

getDeepFetchAllChildren: async (props) =>
Expand All @@ -78,7 +98,7 @@ class Client extends _Client {
await getQuery({
...props,
config: this.#config,
notionDatabasesQuery: this.databases.query,
notionDatabasesQuery: this.#queryDatabase,
}),
};

Expand Down
14 changes: 14 additions & 0 deletions packages/notion/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
coverage: {
exclude: ["src/**/*.test.ts", "src/**/__snapshots__/**"],
include: ["src/**/*.ts"],
provider: "v8",
reporter: ["text", "json-summary", "json"],
},
environment: "node",
include: ["src/**/*.test.ts"],
},
});
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion release.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const branches = [{ name: "main" }, { name: "feat/kebab-case", prerelease: "canary" }];
const branches = [{ name: "main" }, { name: "deps/notionhq-client-5.x", prerelease: "canary" }];

const config = {
branches,
Expand Down
Loading