Skip to content

Commit 310ce69

Browse files
committed
refactor: use a helper for synchronising nodes
1 parent 99c04f9 commit 310ce69

File tree

4 files changed

+172
-141
lines changed

4 files changed

+172
-141
lines changed

source/gatsby-node.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
import { version as gatsbyVersion } from 'gatsby/package.json';
1717

1818
import { name } from '#.';
19-
import { Notion } from '#client';
20-
import { getDatabases, getPages } from '#plugin';
21-
import { NodeManager } from '#node';
22-
import { normaliseConfig } from '#plugin';
19+
import { normaliseConfig, sync } from '#plugin';
2320

2421
import type { GatsbyNode } from 'gatsby';
2522

@@ -54,19 +51,8 @@ export const sourceNodes: NonNullable<GatsbyNode['sourceNodes']> = async (
5451
args,
5552
partialConfig,
5653
) => {
57-
const pluginConfig = normaliseConfig(partialConfig);
58-
const client = new Notion(pluginConfig);
59-
60-
// getting entries from notion
61-
const databases = await getDatabases(client, pluginConfig);
62-
const pages = await getPages(client, pluginConfig);
63-
for (const database of databases) {
64-
pages.push(...database.pages);
65-
}
66-
67-
// update nodes
68-
const manager = new NodeManager(args);
69-
manager.update([...databases, ...pages]);
54+
// sync entities from notion
55+
await sync(args, normaliseConfig(partialConfig));
7056
};
7157

7258
/* eslint-enable */

source/plugin.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
*/
1515

1616
import { Notion } from '#client';
17+
import { NodeManager } from '#node';
1718

18-
import type { PluginOptions } from 'gatsby';
19+
import type { NodePluginArgs, PluginOptions } from 'gatsby';
1920

2021
import type { NotionOptions } from '#client';
2122
import type { FullDatabase, FullPage } from '#types';
@@ -98,3 +99,25 @@ export async function getPages(
9899

99100
return pages;
100101
}
102+
103+
/**
104+
* synchronise data between Notion and Gatsby
105+
* @param args argument passed from Gatsby's Node API
106+
* @param pluginConfig pluginConfig passed from the plugin options
107+
*/
108+
export async function sync(
109+
args: NodePluginArgs,
110+
pluginConfig: FullPluginConfig,
111+
): Promise<void> {
112+
const client = new Notion(pluginConfig);
113+
const manager = new NodeManager(args);
114+
115+
const databases = await getDatabases(client, pluginConfig);
116+
const pages = await getPages(client, pluginConfig);
117+
for (const database of databases) {
118+
pages.push(...database.pages);
119+
}
120+
121+
// update nodes
122+
manager.update([...databases, ...pages]);
123+
}

spec/gatsby-node.spec.ts

Lines changed: 14 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@ import gatsbyPackage from 'gatsby/package.json';
1717
import joi from 'joi';
1818

1919
import { pluginOptionsSchema, sourceNodes } from '#gatsby-node';
20-
21-
import { mockDatabase, mockPage } from './mock';
20+
import { sync } from '#plugin';
2221

2322
jest.mock('gatsby/package.json', () => {
2423
return { version: '3.0.0' };
2524
});
2625

27-
const mockUpdate = jest.fn();
28-
jest.mock('#node', () => ({
29-
__esModule: true,
30-
NodeManager: jest.fn().mockImplementation(() => {
31-
return { update: mockUpdate };
32-
}),
33-
}));
26+
jest.mock('#plugin', () => {
27+
return {
28+
__esModule: true,
29+
...jest.requireActual('#plugin'),
30+
sync: jest.fn(),
31+
};
32+
});
3433

3534
describe('fn:pluginOptionsSchema', () => {
3635
it('pass with no option provided at all', () => {
@@ -75,120 +74,13 @@ describe('fn:onPreBootstrap', () => {
7574
});
7675

7776
describe('fn:sourceNodes', () => {
78-
mockDatabase('database');
79-
mockPage('page');
77+
beforeEach(() => jest.clearAllMocks());
78+
beforeEach(() => jest.useFakeTimers());
79+
afterAll(() => jest.useRealTimers());
8080

81-
it('source all nodes', async () => {
82-
await sourceNodes(
83-
{} as any,
84-
{ token: 'token', databases: ['database'], pages: ['page'], plugins: [] },
85-
jest.fn(),
86-
);
81+
it('sync data with Notion', async () => {
82+
await sourceNodes({} as any, { plugins: [] }, jest.fn());
8783

88-
expect(mockUpdate).toBeCalledWith([
89-
{
90-
created_time: '2020-01-01T00:00:00Z',
91-
id: 'database',
92-
last_edited_time: '2020-01-01T00:00:00Z',
93-
object: 'database',
94-
pages: [],
95-
parent: { type: 'workspace' },
96-
properties: { Name: { id: 'title', title: {}, type: 'title' } },
97-
title: 'Title',
98-
},
99-
{
100-
archived: false,
101-
blocks: [
102-
{
103-
children: [
104-
{
105-
created_time: '2020-01-01T00:00:00Z',
106-
has_children: false,
107-
id: 'page-block0-block0',
108-
last_edited_time: '2020-01-01T00:00:00Z',
109-
object: 'block',
110-
paragraph: {
111-
text: [
112-
{
113-
annotations: {
114-
bold: false,
115-
code: false,
116-
color: 'default',
117-
italic: false,
118-
strikethrough: false,
119-
underline: false,
120-
},
121-
href: null,
122-
plain_text: 'block 0 for block page-block0',
123-
text: {
124-
content: 'block 0 for block page-block0',
125-
link: null,
126-
},
127-
type: 'text',
128-
},
129-
],
130-
},
131-
type: 'paragraph',
132-
},
133-
],
134-
created_time: '2020-01-01T00:00:00Z',
135-
has_children: true,
136-
id: 'page-block0',
137-
last_edited_time: '2020-01-01T00:00:00Z',
138-
object: 'block',
139-
paragraph: {
140-
text: [
141-
{
142-
annotations: {
143-
bold: false,
144-
code: false,
145-
color: 'default',
146-
italic: false,
147-
strikethrough: false,
148-
underline: false,
149-
},
150-
href: null,
151-
plain_text: 'block 0 for block page',
152-
text: { content: 'block 0 for block page', link: null },
153-
type: 'text',
154-
},
155-
],
156-
},
157-
type: 'paragraph',
158-
},
159-
],
160-
created_time: '2020-01-01T00:00:00Z',
161-
id: 'page',
162-
last_edited_time: '2020-01-01T00:00:00Z',
163-
markdown:
164-
"---\nid: 'page'\ntitle: 'Title'\ncreatedTime: '2020-01-01T00:00:00Z'\nlastEditedTime: '2020-01-01T00:00:00Z'\n---\nblock 0 for block page\n\nblock 0 for block page-block0\n",
165-
object: 'page',
166-
parent: { database_id: 'database-page', type: 'database_id' },
167-
properties: {
168-
title: {
169-
id: 'title',
170-
title: [
171-
{
172-
annotations: {
173-
bold: false,
174-
code: false,
175-
color: 'default',
176-
italic: false,
177-
strikethrough: false,
178-
underline: false,
179-
},
180-
href: null,
181-
plain_text: 'Title',
182-
text: { content: 'Title', link: null },
183-
type: 'text',
184-
},
185-
],
186-
type: 'title',
187-
},
188-
},
189-
title: 'Title',
190-
url: 'https://www.notion.so/page',
191-
},
192-
]);
84+
expect(sync).toBeCalledTimes(1);
19385
});
19486
});

spec/plugin.spec.ts

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@
1414
*/
1515

1616
import { Notion } from '#client';
17-
import { getDatabases, getPages, normaliseConfig } from '#plugin';
17+
import { getDatabases, getPages, normaliseConfig, sync } from '#plugin';
1818
import { mockDatabase, mockPage } from './mock';
1919

20+
const mockUpdate = jest.fn();
21+
jest.mock('#node', () => ({
22+
__esModule: true,
23+
NodeManager: jest.fn().mockImplementation(() => {
24+
return { update: mockUpdate };
25+
}),
26+
}));
27+
2028
const client = new Notion({ token: 'token' });
2129

2230
describe('fn:normaliseConfig', () => {
@@ -83,3 +91,125 @@ describe('fn:getPages', () => {
8391
expect(pages.map((page) => page.id)).toEqual(['page']);
8492
});
8593
});
94+
95+
describe('fn:sync', () => {
96+
mockDatabase('database');
97+
mockPage('page');
98+
99+
it('source all nodes', async () => {
100+
await sync(
101+
{} as any,
102+
normaliseConfig({
103+
token: 'token',
104+
databases: ['database'],
105+
pages: ['page'],
106+
}),
107+
);
108+
109+
expect(mockUpdate).toBeCalledWith([
110+
{
111+
created_time: '2020-01-01T00:00:00Z',
112+
id: 'database',
113+
last_edited_time: '2020-01-01T00:00:00Z',
114+
object: 'database',
115+
pages: [],
116+
parent: { type: 'workspace' },
117+
properties: { Name: { id: 'title', title: {}, type: 'title' } },
118+
title: 'Title',
119+
},
120+
{
121+
archived: false,
122+
blocks: [
123+
{
124+
children: [
125+
{
126+
created_time: '2020-01-01T00:00:00Z',
127+
has_children: false,
128+
id: 'page-block0-block0',
129+
last_edited_time: '2020-01-01T00:00:00Z',
130+
object: 'block',
131+
paragraph: {
132+
text: [
133+
{
134+
annotations: {
135+
bold: false,
136+
code: false,
137+
color: 'default',
138+
italic: false,
139+
strikethrough: false,
140+
underline: false,
141+
},
142+
href: null,
143+
plain_text: 'block 0 for block page-block0',
144+
text: {
145+
content: 'block 0 for block page-block0',
146+
link: null,
147+
},
148+
type: 'text',
149+
},
150+
],
151+
},
152+
type: 'paragraph',
153+
},
154+
],
155+
created_time: '2020-01-01T00:00:00Z',
156+
has_children: true,
157+
id: 'page-block0',
158+
last_edited_time: '2020-01-01T00:00:00Z',
159+
object: 'block',
160+
paragraph: {
161+
text: [
162+
{
163+
annotations: {
164+
bold: false,
165+
code: false,
166+
color: 'default',
167+
italic: false,
168+
strikethrough: false,
169+
underline: false,
170+
},
171+
href: null,
172+
plain_text: 'block 0 for block page',
173+
text: { content: 'block 0 for block page', link: null },
174+
type: 'text',
175+
},
176+
],
177+
},
178+
type: 'paragraph',
179+
},
180+
],
181+
created_time: '2020-01-01T00:00:00Z',
182+
id: 'page',
183+
last_edited_time: '2020-01-01T00:00:00Z',
184+
markdown:
185+
"---\nid: 'page'\ntitle: 'Title'\ncreatedTime: '2020-01-01T00:00:00Z'\nlastEditedTime: '2020-01-01T00:00:00Z'\n---\nblock 0 for block page\n\nblock 0 for block page-block0\n",
186+
object: 'page',
187+
parent: { database_id: 'database-page', type: 'database_id' },
188+
properties: {
189+
title: {
190+
id: 'title',
191+
title: [
192+
{
193+
annotations: {
194+
bold: false,
195+
code: false,
196+
color: 'default',
197+
italic: false,
198+
strikethrough: false,
199+
underline: false,
200+
},
201+
href: null,
202+
plain_text: 'Title',
203+
text: { content: 'Title', link: null },
204+
type: 'text',
205+
},
206+
],
207+
type: 'title',
208+
},
209+
},
210+
title: 'Title',
211+
url: 'https://www.notion.so/page',
212+
},
213+
]);
214+
});
215+
});

0 commit comments

Comments
 (0)