Skip to content

Commit fb35967

Browse files
committed
chore(tests): fix and improve tests to cover latest changes
create test for success reading the files. create test for failure when file couldn't be found.
1 parent 3771b85 commit fb35967

File tree

6 files changed

+2829
-150
lines changed

6 files changed

+2829
-150
lines changed

__mocks__/webpack.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}

__tests__/fixtures/intl/es.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"spanish": "Spanish version"
3+
}

__tests__/gatsby-node.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require(`fs`)
2+
13
const { onCreatePage } = require(`../src/gatsby-node`)
24

35
const actions = {
@@ -9,10 +11,54 @@ const mocks = {
911
actions,
1012
page: {
1113
path: "/",
14+
context: {},
1215
},
1316
}
1417

15-
it(`does not crash when no pluginConfig is provided`, () => {
18+
beforeAll(() => {
19+
// Create file src/en.json because the default option for reading a file is `./en.json`
20+
fs.writeFileSync(
21+
`${__dirname}/../src/en.json`,
22+
JSON.stringify({ english: `English version` })
23+
)
24+
})
25+
26+
afterAll(() => {
27+
fs.unlinkSync(`${__dirname}/../src/en.json`)
28+
})
29+
30+
it(`should not crash when no pluginConfig is provided`, async () => {
1631
const pluginOptions = {}
17-
onCreatePage(mocks, pluginOptions)
32+
33+
await onCreatePage(mocks, pluginOptions)
34+
})
35+
36+
it(`should read translations from file and create corresponding pages`, async () => {
37+
const pluginOptions = {
38+
languages: [`es`],
39+
defaultLanguage: `es`,
40+
path: `${__dirname}/fixtures/intl`,
41+
}
42+
43+
await onCreatePage(mocks, pluginOptions)
44+
45+
expect(actions.createPage.mock.calls.length).toBe(4)
46+
47+
// assert the pages created match the requested languages
48+
expect(actions.createPage.mock.calls[0][0].path).toBe(`/`)
49+
expect(actions.createPage.mock.calls[1][0].path).toBe(`/en/`)
50+
expect(actions.createPage.mock.calls[2][0].path).toBe(`/`)
51+
expect(actions.createPage.mock.calls[3][0].path).toBe(`/es/`)
52+
})
53+
54+
it(`should crash when translations file doesn't exist`, async () => {
55+
const pluginOptions = {
56+
languages: [`es`, `en`],
57+
defaultLanguage: `es`,
58+
path: `${__dirname}/fixtures/intl`,
59+
}
60+
61+
await expect(onCreatePage(mocks, pluginOptions)).rejects.toThrow(
62+
`Cannot find module`
63+
)
1864
})

0 commit comments

Comments
 (0)