forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-source-contentful): add options validation and more detai…
…led error messages (gatsbyjs#9231)
- Loading branch information
Showing
8 changed files
with
643 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 227 additions & 0 deletions
227
packages/gatsby-source-contentful/src/__tests__/fetch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
// disable output coloring for tests | ||
process.env.FORCE_COLOR = 0 | ||
|
||
const mockClient = { | ||
getLocales: jest.fn(() => | ||
Promise.resolve({ | ||
items: [ | ||
{ | ||
code: `en-us`, | ||
default: true, | ||
}, | ||
], | ||
}) | ||
), | ||
sync: jest.fn(() => { | ||
return { | ||
entries: [], | ||
assets: [], | ||
deletedEntries: [], | ||
deletedAssets: [], | ||
} | ||
}), | ||
getContentTypes: jest.fn(async () => { | ||
return { | ||
items: [], | ||
total: 0, | ||
} | ||
}), | ||
} | ||
|
||
jest.mock(`contentful`, () => { | ||
return { | ||
createClient: jest.fn(() => mockClient), | ||
} | ||
}) | ||
|
||
jest.mock(`../plugin-options`, () => { | ||
return { | ||
...jest.requireActual(`../plugin-options`), | ||
formatPluginOptionsForCLI: jest.fn(() => `formatPluginOptionsForCLIMock`), | ||
} | ||
}) | ||
|
||
// jest so test output is not filled with contentful plugin logs | ||
global.console = { log: jest.fn(), time: jest.fn(), timeEnd: jest.fn() } | ||
|
||
const contentful = require(`contentful`) | ||
const fetchData = require(`../fetch`) | ||
const { | ||
formatPluginOptionsForCLI, | ||
createPluginConfig, | ||
} = require(`../plugin-options`) | ||
|
||
const options = { | ||
spaceId: `rocybtov1ozk`, | ||
accessToken: `6f35edf0db39085e9b9c19bd92943e4519c77e72c852d961968665f1324bfc94`, | ||
host: `host`, | ||
environment: `env`, | ||
} | ||
|
||
const pluginConfig = createPluginConfig(options) | ||
|
||
let realProcess | ||
beforeAll(() => { | ||
realProcess = global.process | ||
|
||
global.process = { | ||
...realProcess, | ||
exit: jest.fn(), | ||
} | ||
}) | ||
|
||
const reporter = { | ||
panic: jest.fn(), | ||
} | ||
|
||
beforeEach(() => { | ||
global.process.exit.mockClear() | ||
reporter.panic.mockClear() | ||
mockClient.getLocales.mockClear() | ||
formatPluginOptionsForCLI.mockClear() | ||
contentful.createClient.mockClear() | ||
}) | ||
|
||
afterAll(() => { | ||
global.process = realProcess | ||
}) | ||
|
||
it(`calls contentful.createClient with expected params`, async () => { | ||
await fetchData({ pluginConfig, reporter }) | ||
expect(reporter.panic).not.toBeCalled() | ||
expect(contentful.createClient).toBeCalledWith( | ||
expect.objectContaining({ | ||
accessToken: `6f35edf0db39085e9b9c19bd92943e4519c77e72c852d961968665f1324bfc94`, | ||
environment: `env`, | ||
host: `host`, | ||
space: `rocybtov1ozk`, | ||
}) | ||
) | ||
}) | ||
|
||
it(`calls contentful.createClient with expected params and default fallbacks`, async () => { | ||
await fetchData({ | ||
pluginConfig: createPluginConfig({ | ||
accessToken: `6f35edf0db39085e9b9c19bd92943e4519c77e72c852d961968665f1324bfc94`, | ||
spaceId: `rocybtov1ozk`, | ||
}), | ||
reporter, | ||
}) | ||
expect(reporter.panic).not.toBeCalled() | ||
expect(contentful.createClient).toBeCalledWith( | ||
expect.objectContaining({ | ||
accessToken: `6f35edf0db39085e9b9c19bd92943e4519c77e72c852d961968665f1324bfc94`, | ||
environment: `master`, | ||
host: `cdn.contentful.com`, | ||
space: `rocybtov1ozk`, | ||
}) | ||
) | ||
}) | ||
|
||
describe(`Displays troubleshooting tips and detailed plugin options on contentful client error`, () => { | ||
it(`Generic fallback error`, async () => { | ||
mockClient.getLocales.mockImplementation(() => { | ||
throw new Error(`error`) | ||
}) | ||
|
||
await fetchData({ pluginConfig, reporter }) | ||
|
||
expect(reporter.panic).toBeCalledWith( | ||
expect.stringContaining(`Accessing your Contentful space failed`) | ||
) | ||
|
||
expect(reporter.panic).toBeCalledWith( | ||
expect.stringContaining(`formatPluginOptionsForCLIMock`) | ||
) | ||
|
||
expect(formatPluginOptionsForCLI).toBeCalledWith( | ||
expect.objectContaining({ | ||
...options, | ||
}), | ||
undefined | ||
) | ||
}) | ||
|
||
it(`Connection error`, async () => { | ||
mockClient.getLocales.mockImplementation(() => { | ||
const err = new Error(`error`) | ||
err.code = `ENOTFOUND` | ||
throw err | ||
}) | ||
|
||
await fetchData({ pluginConfig, reporter }) | ||
|
||
expect(reporter.panic).toBeCalledWith( | ||
expect.stringContaining(`You seem to be offline`) | ||
) | ||
|
||
expect(reporter.panic).toBeCalledWith( | ||
expect.stringContaining(`formatPluginOptionsForCLIMock`) | ||
) | ||
|
||
expect(formatPluginOptionsForCLI).toBeCalledWith( | ||
expect.objectContaining({ | ||
...options, | ||
}), | ||
undefined | ||
) | ||
}) | ||
|
||
it(`API 404 response handling`, async () => { | ||
mockClient.getLocales.mockImplementation(() => { | ||
const err = new Error(`error`) | ||
err.response = { status: 404 } | ||
throw err | ||
}) | ||
|
||
await fetchData({ pluginConfig, reporter }) | ||
|
||
expect(reporter.panic).toBeCalledWith( | ||
expect.stringContaining(`Check if host and spaceId settings are correct`) | ||
) | ||
|
||
expect(reporter.panic).toBeCalledWith( | ||
expect.stringContaining(`formatPluginOptionsForCLIMock`) | ||
) | ||
|
||
expect(formatPluginOptionsForCLI).toBeCalledWith( | ||
expect.objectContaining({ | ||
...options, | ||
}), | ||
{ | ||
host: `Check if setting is correct`, | ||
spaceId: `Check if setting is correct`, | ||
} | ||
) | ||
}) | ||
|
||
it(`API authorization error handling`, async () => { | ||
mockClient.getLocales.mockImplementation(() => { | ||
const err = new Error(`error`) | ||
err.response = { status: 401 } | ||
throw err | ||
}) | ||
|
||
await fetchData({ pluginConfig, reporter }) | ||
|
||
expect(reporter.panic).toBeCalledWith( | ||
expect.stringContaining( | ||
`Check if accessToken and environment are correct` | ||
) | ||
) | ||
|
||
expect(reporter.panic).toBeCalledWith( | ||
expect.stringContaining(`formatPluginOptionsForCLIMock`) | ||
) | ||
|
||
expect(formatPluginOptionsForCLI).toBeCalledWith( | ||
expect.objectContaining({ | ||
...options, | ||
}), | ||
{ | ||
accessToken: `Check if setting is correct`, | ||
environment: `Check if setting is correct`, | ||
} | ||
) | ||
}) | ||
}) |
Oops, something went wrong.