From f4121fbd7fce0034816e3e743481c13cb4181d07 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Wed, 23 Mar 2022 12:29:05 +0100 Subject: [PATCH] fix(gatsby-source-filesystem): cleanup tests (#35200) --- .../src/__tests__/create-remote-file-node.js | 272 +++++++----------- .../src/create-remote-file-node.js | 6 +- 2 files changed, 101 insertions(+), 177 deletions(-) diff --git a/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js b/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js index e90addb78ecc8..1b45e29e5b4c2 100644 --- a/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js +++ b/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js @@ -1,202 +1,128 @@ -jest.mock(`fs-extra`, () => { - return { - createWriteStream: jest.fn(() => { - return { - on: jest.fn((type, cb) => { - if (type === `finish`) { - cb() - } - }), - close: jest.fn(), - } - }), - ensureDir: jest.fn(), - removeSync: jest.fn(), - move: jest.fn(), - stat: jest.fn(), - } -}) -jest.mock(`got`, () => { - return { - stream: jest.fn(), - } -}) +import path from "path" +import { fetchRemoteFile } from "gatsby-core-utils/fetch-remote-file" -jest.mock(`gatsby-core-utils/fetch-remote-file`, () => { - return { - fetchRemoteFile: jest.fn(), - } -}) - -jest.mock(`../create-file-node`, () => { - return { - createFileNode: jest.fn(), - } -}) const reporter = {} const createRemoteFileNode = require(`../create-remote-file-node`) -const { createFileNode } = require(`../create-file-node`) -const { fetchRemoteFile } = require(`gatsby-core-utils/fetch-remote-file`) -const createMockCache = () => { +jest.mock(`gatsby-core-utils/fetch-remote-file`, () => { + const path = require(`path`) + return { - get: jest.fn(), - set: jest.fn(), - directory: __dirname, + fetchRemoteFile: jest.fn(() => + path.join(__dirname, `fixtures`, `dog-thumbnail.jpg`) + ), } -} +}) describe(`create-remote-file-node`, () => { const cache = createMockCache() + let uuid = 0 + + beforeEach(async () => { + uuid = 1 + fetchRemoteFile.mockClear() + }) + + function createMockCache() { + return { + get: jest.fn(), + set: jest.fn(), + directory: __dirname, + } + } const defaultArgs = { - url: ``, + url: `https://external.com/dog.jpg`, store: {}, getCache: () => cache, createNode: jest.fn(), - createNodeId: jest.fn(), + createNodeId: jest.fn(() => String(uuid++)), reporter, + ext: `.jpg`, + name: `dog-thumbnail`, } - describe(`basic functionality`, () => { - describe(`non-url`, () => { - it(`short-circuits and resolves`, () => { - const value = createRemoteFileNode({ - ...defaultArgs, - url: ``, - }) - - expect(value).rejects.toMatch( - `url passed to createRemoteFileNode is either missing or not a proper web uri: ` - ) - }) - - it(`does not increment progress bar total`, () => { - const value = createRemoteFileNode({ - ...defaultArgs, - url: ``, - }) - - expect(value).rejects.toMatch( - `url passed to createRemoteFileNode is either missing or not a proper web uri: ` - ) + it(`should throw an error when an invalid url is given`, () => { + expect(() => + createRemoteFileNode({ + ...defaultArgs, + url: ``, }) - }) + ).toThrowError( + `url passed to createRemoteFileNode is either missing or not a proper web uri: ` + ) }) - describe(`valid url`, () => { - let uuid = 0 - - const setup = (args = {}, response = { statusCode: 200 }) => { - const url = `https://images.whatever.com/real-image-trust-me-${uuid}.png` - - if (response.statusCode === 404) { - fetchRemoteFile.mockImplementation(({ url }) => { - // eslint-disable-next-line no-throw-literal - throw `failed to process ${url}` - }) - } - - fetchRemoteFile.mockClear() - - createFileNode.mockImplementationOnce(() => { - return { - internal: {}, - } + it(`should download and create file node`, async () => { + const node = await createRemoteFileNode(defaultArgs) + + expect(node).toMatchObject( + expect.objectContaining({ + id: `1`, + extension: `jpg`, + name: `dog-thumbnail`, + base: `dog-thumbnail.jpg`, + url: defaultArgs.url, + internal: expect.objectContaining({ + type: `File`, + }), }) + ) + }) - uuid += 1 + it(`passes headers`, async () => { + await createRemoteFileNode({ + ...defaultArgs, + // bypass cache + url: `${defaultArgs.url}?header`, + httpHeaders: { + "x-test": `test`, + }, + }) - return createRemoteFileNode({ - ...defaultArgs, - store: { - getState: jest.fn(() => { - return { - program: { - directory: `__whatever__`, - }, - } - }), + expect(fetchRemoteFile).toHaveBeenCalledWith( + expect.objectContaining({ + httpHeaders: { + "x-test": `test`, }, - url, - ...args, }) - } + ) + }) - it(`invokes ProgressBar tick`, async () => { - await setup() + it(`passes custom auth header, if defined`, async () => { + const auth = { + htaccess_user: `hunter2`, + htaccess_pass: `*******`, + } + await createRemoteFileNode({ + ...defaultArgs, + // bypass cache + url: `${defaultArgs.url}?auth`, + auth, }) - describe(`requesting remote image`, () => { - it(`passes correct url`, async () => { - const url = `https://hello.com/image.png` - await setup({ - url, - }) - - expect(fetchRemoteFile).toHaveBeenCalledWith( - expect.objectContaining({ - url, - }) - ) - }) - - it(`passes headers`, async () => { - await setup() - - expect(fetchRemoteFile).toHaveBeenCalledWith( - expect.objectContaining({ - httpHeaders: {}, - }) - ) - }) - - it(`passes custom auth header, if defined`, async () => { - const auth = { - htaccess_user: `hunter2`, - htaccess_pass: `*******`, - } - await setup({ - auth, - }) - - expect(fetchRemoteFile).toHaveBeenCalledWith( - expect.objectContaining({ - auth, - }) - ) - }) - - it(`passes custom http header, if defined`, async () => { - await setup({ - httpHeaders: { - Authorization: `Bearer foobar`, - }, - }) - - expect(fetchRemoteFile).toHaveBeenCalledWith( - expect.objectContaining({ - httpHeaders: { - Authorization: `Bearer foobar`, - }, - }) - ) + expect(fetchRemoteFile).toHaveBeenCalledWith( + expect.objectContaining({ + auth, }) + ) + }) - it(`fails when 404 is given`, async () => { - expect.assertions(1) - try { - await setup({}, { statusCode: 404 }) - } catch (err) { - expect(err).toEqual( - expect.stringContaining( - `failed to process https://images.whatever.com/real-image-trust-me` - ) - ) - } + it(`fails when fetchRemoteFile throws an error`, async () => { + expect.assertions(1) + try { + fetchRemoteFile.mockImplementationOnce(({ url }) => + Promise.reject(new Error(`failed to process ${url}`)) + ) + await createRemoteFileNode({ + ...defaultArgs, + // bypass cache + url: `${defaultArgs.url}?error`, }) - }) + } catch (err) { + expect(err.message).toEqual(`failed to process ${defaultArgs.url}?error`) + } }) describe(`validation`, () => { @@ -223,33 +149,33 @@ describe(`create-remote-file-node`, () => { }) it(`throws on invalid inputs: cache and getCache undefined`, () => { - expect(() => { + expect(() => createRemoteFileNode({ ...defaultArgs, cache: undefined, getCache: undefined, }) - }).toThrowErrorMatchingInlineSnapshot( + ).toThrowErrorMatchingInlineSnapshot( `"Neither \\"cache\\" or \\"getCache\\" was passed. getCache must be function that return Gatsby cache, \\"cache\\" must be the Gatsby cache, was undefined"` ) }) it(`doesn't throw when getCache is defined`, () => { - expect(() => { + expect(() => createRemoteFileNode({ ...defaultArgs, getCache: () => createMockCache(), }) - }).not.toThrow() + ).not.toThrow() }) it(`doesn't throw when cache is defined`, () => { - expect(() => { + expect(() => createRemoteFileNode({ ...defaultArgs, cache: createMockCache(), }) - }).not.toThrow() + ).not.toThrow() }) }) }) diff --git a/packages/gatsby-source-filesystem/src/create-remote-file-node.js b/packages/gatsby-source-filesystem/src/create-remote-file-node.js index 66655132bec74..4b0645cba3166 100644 --- a/packages/gatsby-source-filesystem/src/create-remote-file-node.js +++ b/packages/gatsby-source-filesystem/src/create-remote-file-node.js @@ -142,10 +142,8 @@ module.exports = function createRemoteFileNode({ } if (!url || isWebUri(url) === undefined) { - return Promise.reject( - new Error( - `url passed to createRemoteFileNode is either missing or not a proper web uri: ${url}` - ) + throw new Error( + `url passed to createRemoteFileNode is either missing or not a proper web uri: ${url}` ) }