Skip to content

Commit

Permalink
refactor: use a helper for synchronising nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Jul 14, 2021
1 parent 99c04f9 commit 310ce69
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 141 deletions.
20 changes: 3 additions & 17 deletions source/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import { version as gatsbyVersion } from 'gatsby/package.json';

import { name } from '#.';
import { Notion } from '#client';
import { getDatabases, getPages } from '#plugin';
import { NodeManager } from '#node';
import { normaliseConfig } from '#plugin';
import { normaliseConfig, sync } from '#plugin';

import type { GatsbyNode } from 'gatsby';

Expand Down Expand Up @@ -54,19 +51,8 @@ export const sourceNodes: NonNullable<GatsbyNode['sourceNodes']> = async (
args,
partialConfig,
) => {
const pluginConfig = normaliseConfig(partialConfig);
const client = new Notion(pluginConfig);

// getting entries from notion
const databases = await getDatabases(client, pluginConfig);
const pages = await getPages(client, pluginConfig);
for (const database of databases) {
pages.push(...database.pages);
}

// update nodes
const manager = new NodeManager(args);
manager.update([...databases, ...pages]);
// sync entities from notion
await sync(args, normaliseConfig(partialConfig));
};

/* eslint-enable */
25 changes: 24 additions & 1 deletion source/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
*/

import { Notion } from '#client';
import { NodeManager } from '#node';

import type { PluginOptions } from 'gatsby';
import type { NodePluginArgs, PluginOptions } from 'gatsby';

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

return pages;
}

/**
* synchronise data between Notion and Gatsby
* @param args argument passed from Gatsby's Node API
* @param pluginConfig pluginConfig passed from the plugin options
*/
export async function sync(
args: NodePluginArgs,
pluginConfig: FullPluginConfig,
): Promise<void> {
const client = new Notion(pluginConfig);
const manager = new NodeManager(args);

const databases = await getDatabases(client, pluginConfig);
const pages = await getPages(client, pluginConfig);
for (const database of databases) {
pages.push(...database.pages);
}

// update nodes
manager.update([...databases, ...pages]);
}
136 changes: 14 additions & 122 deletions spec/gatsby-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@ import gatsbyPackage from 'gatsby/package.json';
import joi from 'joi';

import { pluginOptionsSchema, sourceNodes } from '#gatsby-node';

import { mockDatabase, mockPage } from './mock';
import { sync } from '#plugin';

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

const mockUpdate = jest.fn();
jest.mock('#node', () => ({
__esModule: true,
NodeManager: jest.fn().mockImplementation(() => {
return { update: mockUpdate };
}),
}));
jest.mock('#plugin', () => {
return {
__esModule: true,
...jest.requireActual('#plugin'),
sync: jest.fn(),
};
});

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

describe('fn:sourceNodes', () => {
mockDatabase('database');
mockPage('page');
beforeEach(() => jest.clearAllMocks());
beforeEach(() => jest.useFakeTimers());
afterAll(() => jest.useRealTimers());

it('source all nodes', async () => {
await sourceNodes(
{} as any,
{ token: 'token', databases: ['database'], pages: ['page'], plugins: [] },
jest.fn(),
);
it('sync data with Notion', async () => {
await sourceNodes({} as any, { plugins: [] }, jest.fn());

expect(mockUpdate).toBeCalledWith([
{
created_time: '2020-01-01T00:00:00Z',
id: 'database',
last_edited_time: '2020-01-01T00:00:00Z',
object: 'database',
pages: [],
parent: { type: 'workspace' },
properties: { Name: { id: 'title', title: {}, type: 'title' } },
title: 'Title',
},
{
archived: false,
blocks: [
{
children: [
{
created_time: '2020-01-01T00:00:00Z',
has_children: false,
id: 'page-block0-block0',
last_edited_time: '2020-01-01T00:00:00Z',
object: 'block',
paragraph: {
text: [
{
annotations: {
bold: false,
code: false,
color: 'default',
italic: false,
strikethrough: false,
underline: false,
},
href: null,
plain_text: 'block 0 for block page-block0',
text: {
content: 'block 0 for block page-block0',
link: null,
},
type: 'text',
},
],
},
type: 'paragraph',
},
],
created_time: '2020-01-01T00:00:00Z',
has_children: true,
id: 'page-block0',
last_edited_time: '2020-01-01T00:00:00Z',
object: 'block',
paragraph: {
text: [
{
annotations: {
bold: false,
code: false,
color: 'default',
italic: false,
strikethrough: false,
underline: false,
},
href: null,
plain_text: 'block 0 for block page',
text: { content: 'block 0 for block page', link: null },
type: 'text',
},
],
},
type: 'paragraph',
},
],
created_time: '2020-01-01T00:00:00Z',
id: 'page',
last_edited_time: '2020-01-01T00:00:00Z',
markdown:
"---\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",
object: 'page',
parent: { database_id: 'database-page', type: 'database_id' },
properties: {
title: {
id: 'title',
title: [
{
annotations: {
bold: false,
code: false,
color: 'default',
italic: false,
strikethrough: false,
underline: false,
},
href: null,
plain_text: 'Title',
text: { content: 'Title', link: null },
type: 'text',
},
],
type: 'title',
},
},
title: 'Title',
url: 'https://www.notion.so/page',
},
]);
expect(sync).toBeCalledTimes(1);
});
});
132 changes: 131 additions & 1 deletion spec/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
*/

import { Notion } from '#client';
import { getDatabases, getPages, normaliseConfig } from '#plugin';
import { getDatabases, getPages, normaliseConfig, sync } from '#plugin';
import { mockDatabase, mockPage } from './mock';

const mockUpdate = jest.fn();
jest.mock('#node', () => ({
__esModule: true,
NodeManager: jest.fn().mockImplementation(() => {
return { update: mockUpdate };
}),
}));

const client = new Notion({ token: 'token' });

describe('fn:normaliseConfig', () => {
Expand Down Expand Up @@ -83,3 +91,125 @@ describe('fn:getPages', () => {
expect(pages.map((page) => page.id)).toEqual(['page']);
});
});

describe('fn:sync', () => {
mockDatabase('database');
mockPage('page');

it('source all nodes', async () => {
await sync(
{} as any,
normaliseConfig({
token: 'token',
databases: ['database'],
pages: ['page'],
}),
);

expect(mockUpdate).toBeCalledWith([
{
created_time: '2020-01-01T00:00:00Z',
id: 'database',
last_edited_time: '2020-01-01T00:00:00Z',
object: 'database',
pages: [],
parent: { type: 'workspace' },
properties: { Name: { id: 'title', title: {}, type: 'title' } },
title: 'Title',
},
{
archived: false,
blocks: [
{
children: [
{
created_time: '2020-01-01T00:00:00Z',
has_children: false,
id: 'page-block0-block0',
last_edited_time: '2020-01-01T00:00:00Z',
object: 'block',
paragraph: {
text: [
{
annotations: {
bold: false,
code: false,
color: 'default',
italic: false,
strikethrough: false,
underline: false,
},
href: null,
plain_text: 'block 0 for block page-block0',
text: {
content: 'block 0 for block page-block0',
link: null,
},
type: 'text',
},
],
},
type: 'paragraph',
},
],
created_time: '2020-01-01T00:00:00Z',
has_children: true,
id: 'page-block0',
last_edited_time: '2020-01-01T00:00:00Z',
object: 'block',
paragraph: {
text: [
{
annotations: {
bold: false,
code: false,
color: 'default',
italic: false,
strikethrough: false,
underline: false,
},
href: null,
plain_text: 'block 0 for block page',
text: { content: 'block 0 for block page', link: null },
type: 'text',
},
],
},
type: 'paragraph',
},
],
created_time: '2020-01-01T00:00:00Z',
id: 'page',
last_edited_time: '2020-01-01T00:00:00Z',
markdown:
"---\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",
object: 'page',
parent: { database_id: 'database-page', type: 'database_id' },
properties: {
title: {
id: 'title',
title: [
{
annotations: {
bold: false,
code: false,
color: 'default',
italic: false,
strikethrough: false,
underline: false,
},
href: null,
plain_text: 'Title',
text: { content: 'Title', link: null },
type: 'text',
},
],
type: 'title',
},
},
title: 'Title',
url: 'https://www.notion.so/page',
},
]);
});
});

0 comments on commit 310ce69

Please sign in to comment.