Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __mocks__/webpack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
3 changes: 3 additions & 0 deletions __tests__/fixtures/intl/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"spanish": "Spanish version"
}
50 changes: 48 additions & 2 deletions __tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const fs = require(`fs`)

const { onCreatePage } = require(`../src/gatsby-node`)

const actions = {
Expand All @@ -9,10 +11,54 @@ const mocks = {
actions,
page: {
path: "/",
context: {},
},
}

it(`does not crash when no pluginConfig is provided`, () => {
beforeAll(() => {
// Create file src/en.json because the default option for reading a file is `./en.json`
fs.writeFileSync(
`${__dirname}/../src/en.json`,
JSON.stringify({ english: `English version` })
)
})

afterAll(() => {
fs.unlinkSync(`${__dirname}/../src/en.json`)
})

it(`should not crash when no pluginConfig is provided`, async () => {
const pluginOptions = {}
onCreatePage(mocks, pluginOptions)

await onCreatePage(mocks, pluginOptions)
})

it(`should read translations from file and create corresponding pages`, async () => {
const pluginOptions = {
languages: [`es`],
defaultLanguage: `es`,
path: `${__dirname}/fixtures/intl`,
}

await onCreatePage(mocks, pluginOptions)

expect(actions.createPage.mock.calls.length).toBe(4)

// assert the pages created match the requested languages
expect(actions.createPage.mock.calls[0][0].path).toBe(`/`)
expect(actions.createPage.mock.calls[1][0].path).toBe(`/en/`)
expect(actions.createPage.mock.calls[2][0].path).toBe(`/`)
expect(actions.createPage.mock.calls[3][0].path).toBe(`/es/`)
})

it(`should crash when translations file doesn't exist`, async () => {
const pluginOptions = {
languages: [`es`, `en`],
defaultLanguage: `es`,
path: `${__dirname}/fixtures/intl`,
}

await expect(onCreatePage(mocks, pluginOptions)).rejects.toThrow(
`Cannot find module`
)
})
Loading