|
| 1 | +/* |
| 2 | + * *** MIT LICENSE *** |
| 3 | + * ------------------------------------------------------------------------- |
| 4 | + * This code may be modified and distributed under the MIT license. |
| 5 | + * See the LICENSE file for details. |
| 6 | + * ------------------------------------------------------------------------- |
| 7 | + * |
| 8 | + * @summary Tests on node generation |
| 9 | + * |
| 10 | + * @author Alvis HT Tang <alvis@hilbert.space> |
| 11 | + * @license MIT |
| 12 | + * @copyright Copyright (c) 2021 - All Rights Reserved. |
| 13 | + * ------------------------------------------------------------------------- |
| 14 | + */ |
| 15 | + |
| 16 | +import gatsbyPackage from 'gatsby/package.json'; |
| 17 | +import joi from 'joi'; |
| 18 | + |
| 19 | +import { pluginOptionsSchema, sourceNodes } from '#gatsby-node'; |
| 20 | + |
| 21 | +import { mockDatabase, mockPage } from './mock'; |
| 22 | + |
| 23 | +jest.mock('gatsby/package.json', () => { |
| 24 | + return { version: '3.0.0' }; |
| 25 | +}); |
| 26 | + |
| 27 | +const mockUpdate = jest.fn(); |
| 28 | +jest.mock('#node', () => ({ |
| 29 | + __esModule: true, |
| 30 | + NodeManager: jest.fn().mockImplementation(() => { |
| 31 | + return { update: mockUpdate }; |
| 32 | + }), |
| 33 | +})); |
| 34 | + |
| 35 | +describe('fn:pluginOptionsSchema', () => { |
| 36 | + it('pass with no option provided at all', () => { |
| 37 | + expect( |
| 38 | + pluginOptionsSchema({ |
| 39 | + Joi: joi, |
| 40 | + } as any).validate({}).error, |
| 41 | + ).toEqual(undefined); |
| 42 | + }); |
| 43 | + |
| 44 | + it('give an error if some data is given in the wrong format', () => { |
| 45 | + expect( |
| 46 | + pluginOptionsSchema({ |
| 47 | + Joi: joi, |
| 48 | + } as any).validate({ databases: ['database', 0] }).error, |
| 49 | + ).toBeInstanceOf(Error); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('fn:onPreBootstrap', () => { |
| 54 | + const testVersion = (version: string, shouldPass: boolean) => { |
| 55 | + return async () => { |
| 56 | + // edit the mocked package version here |
| 57 | + gatsbyPackage.version = version; |
| 58 | + const { onPreBootstrap } = await import('#gatsby-node'); |
| 59 | + const panic = jest.fn(); |
| 60 | + await onPreBootstrap( |
| 61 | + { |
| 62 | + reporter: { panic }, |
| 63 | + } as any, |
| 64 | + { plugins: [] }, |
| 65 | + jest.fn(), |
| 66 | + ); |
| 67 | + |
| 68 | + expect(panic).toBeCalledTimes(shouldPass ? 0 : 1); |
| 69 | + }; |
| 70 | + }; |
| 71 | + |
| 72 | + it('fail with gatsby earlier than v3', testVersion('2.0.0', false)); |
| 73 | + it('pass with gatsby v3', testVersion('3.0.0', true)); |
| 74 | + it('fail with future gatsby after v3', testVersion('4.0.0', false)); |
| 75 | +}); |
| 76 | + |
| 77 | +describe('fn:sourceNodes', () => { |
| 78 | + mockDatabase('database'); |
| 79 | + mockPage('page'); |
| 80 | + |
| 81 | + it('source all nodes', async () => { |
| 82 | + await sourceNodes( |
| 83 | + {} as any, |
| 84 | + { token: 'token', databases: ['database'], pages: ['page'], plugins: [] }, |
| 85 | + jest.fn(), |
| 86 | + ); |
| 87 | + |
| 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\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 | + ]); |
| 193 | + }); |
| 194 | +}); |
0 commit comments