Skip to content

Commit 84538a0

Browse files
authored
test: Add test case for the creation of redirects for SSR and DSG pages. (#108)
* test: Add test case for the creation of redirects for SSR and DSG pages. * :test - Update the Jest mock path for fs-extra.
1 parent aefaae7 commit 84538a0

File tree

1 file changed

+106
-35
lines changed

1 file changed

+106
-35
lines changed

src/__tests__/gatsby-node.js

Lines changed: 106 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,117 @@
1-
import { testPluginOptionsSchema } from "gatsby-plugin-utils"
1+
jest.mock('../plugin-data', () => {
2+
return {
3+
__esModule: true,
4+
default: jest.fn().mockReturnValue({
5+
publicFolder: jest.fn().mockReturnValue('mock-file-path'),
6+
}),
7+
}
8+
})
9+
jest.mock('../build-headers-program', () => {
10+
return {
11+
__esModule: true,
12+
default: jest.fn(),
13+
}
14+
})
15+
jest.mock('fs-extra', () => {
16+
return {
17+
__esModule: true,
18+
default: jest.fn(),
19+
existsSync: jest.fn(),
20+
readFile: jest.fn(),
21+
writeFile: jest.fn(),
22+
}
23+
})
224

3-
import { pluginOptionsSchema } from "../gatsby-node"
25+
// Importing writeFile here gives us access to the mocked method to assert the correct content is written to the file within test cases
26+
import { writeFile } from 'fs-extra'
27+
import { testPluginOptionsSchema } from 'gatsby-plugin-utils'
28+
import { pluginOptionsSchema, onPostBuild } from '../gatsby-node'
429

530
describe(`gatsby-node.js`, () => {
6-
it(`should provide meaningful errors when fields are invalid`, async () => {
7-
const expectedErrors = [
8-
`"headers" must be of type object`,
9-
`"allPageHeaders" must be an array`,
10-
`"mergeSecurityHeaders" must be a boolean`,
11-
`"mergeLinkHeaders" must be a boolean`,
12-
`"mergeCachingHeaders" must be a boolean`,
13-
`"transformHeaders" must have an arity lesser or equal to 2`,
14-
`"generateMatchPathRewrites" must be a boolean`,
15-
]
16-
17-
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
18-
headers: `this should be an object`,
19-
allPageHeaders: `this should be an array`,
20-
mergeSecurityHeaders: `this should be a boolean`,
21-
mergeLinkHeaders: `this should be a boolean`,
22-
mergeCachingHeaders: `this should be a boolean`,
23-
transformHeaders: (too, many, args) => ``,
24-
generateMatchPathRewrites: `this should be a boolean`,
31+
describe('testPluginOptionsSchema', () => {
32+
it(`should provide meaningful errors when fields are invalid`, async () => {
33+
const expectedErrors = [
34+
`"headers" must be of type object`,
35+
`"allPageHeaders" must be an array`,
36+
`"mergeSecurityHeaders" must be a boolean`,
37+
`"mergeLinkHeaders" must be a boolean`,
38+
`"mergeCachingHeaders" must be a boolean`,
39+
`"transformHeaders" must have an arity lesser or equal to 2`,
40+
`"generateMatchPathRewrites" must be a boolean`,
41+
]
42+
43+
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
44+
headers: `this should be an object`,
45+
allPageHeaders: `this should be an array`,
46+
mergeSecurityHeaders: `this should be a boolean`,
47+
mergeLinkHeaders: `this should be a boolean`,
48+
mergeCachingHeaders: `this should be a boolean`,
49+
transformHeaders: (too, many, args) => ``,
50+
generateMatchPathRewrites: `this should be a boolean`,
51+
})
52+
53+
expect(errors).toEqual(expectedErrors)
2554
})
2655

27-
expect(errors).toEqual(expectedErrors)
56+
it(`should validate the schema`, async () => {
57+
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
58+
headers: {
59+
'/some-page': [`Bearer: Some-Magic-Token`],
60+
'/some-other-page': [`some`, `great`, `headers`],
61+
},
62+
allPageHeaders: [`First header`, `Second header`],
63+
mergeSecurityHeaders: true,
64+
mergeLinkHeaders: false,
65+
mergeCachingHeaders: true,
66+
transformHeaders: () => null,
67+
generateMatchPathRewrites: false,
68+
})
69+
70+
expect(isValid).toBe(true)
71+
})
2872
})
2973

30-
it(`should validate the schema`, async () => {
31-
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
32-
headers: {
33-
"/some-page": [`Bearer: Some-Magic-Token`],
34-
"/some-other-page": [`some`, `great`, `headers`],
35-
},
36-
allPageHeaders: [`First header`, `Second header`],
37-
mergeSecurityHeaders: true,
38-
mergeLinkHeaders: false,
39-
mergeCachingHeaders: true,
40-
transformHeaders: () => null,
41-
generateMatchPathRewrites: false,
74+
describe('onPostBuild', () => {
75+
let store = {}
76+
const reporter = {
77+
info: jest.fn(),
78+
}
79+
beforeEach(() => {
80+
store = {
81+
getState: jest.fn().mockReturnValue({
82+
redirects: [],
83+
pages: [
84+
{
85+
mode: 'SSR',
86+
path: 'some/path',
87+
},
88+
{
89+
mode: 'DSG',
90+
path: 'some/other/path',
91+
},
92+
],
93+
functions: [],
94+
program: { directory: '' },
95+
}),
96+
}
97+
})
98+
99+
afterEach(() => {
100+
jest.resetAllMocks()
42101
})
43102

44-
expect(isValid).toBe(true)
103+
it('creates redirects for SSR and DSG pages in format Netlify expects', async () => {
104+
const expectedValue = [
105+
'',
106+
'## Created with gatsby-plugin-netlify',
107+
'some/path /.netlify/functions/__ssr 200',
108+
'/page-data/some/path/page-data.json /.netlify/functions/__ssr 200',
109+
'some/other/path /.netlify/functions/__dsg 200',
110+
'/page-data/some/other/path/page-data.json /.netlify/functions/__dsg 200',
111+
].join(`\n`)
112+
113+
await onPostBuild({ store, pathPrefix: '', reporter }, {})
114+
expect(writeFile).toHaveBeenCalledWith('mock-file-path', expectedValue)
115+
})
45116
})
46117
})

0 commit comments

Comments
 (0)