Skip to content

Commit f4121fb

Browse files
authored
fix(gatsby-source-filesystem): cleanup tests (#35200)
1 parent 3f2bc25 commit f4121fb

File tree

2 files changed

+101
-177
lines changed

2 files changed

+101
-177
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,202 +1,128 @@
1-
jest.mock(`fs-extra`, () => {
2-
return {
3-
createWriteStream: jest.fn(() => {
4-
return {
5-
on: jest.fn((type, cb) => {
6-
if (type === `finish`) {
7-
cb()
8-
}
9-
}),
10-
close: jest.fn(),
11-
}
12-
}),
13-
ensureDir: jest.fn(),
14-
removeSync: jest.fn(),
15-
move: jest.fn(),
16-
stat: jest.fn(),
17-
}
18-
})
19-
jest.mock(`got`, () => {
20-
return {
21-
stream: jest.fn(),
22-
}
23-
})
1+
import path from "path"
2+
import { fetchRemoteFile } from "gatsby-core-utils/fetch-remote-file"
243

25-
jest.mock(`gatsby-core-utils/fetch-remote-file`, () => {
26-
return {
27-
fetchRemoteFile: jest.fn(),
28-
}
29-
})
30-
31-
jest.mock(`../create-file-node`, () => {
32-
return {
33-
createFileNode: jest.fn(),
34-
}
35-
})
364
const reporter = {}
375

386
const createRemoteFileNode = require(`../create-remote-file-node`)
39-
const { createFileNode } = require(`../create-file-node`)
40-
const { fetchRemoteFile } = require(`gatsby-core-utils/fetch-remote-file`)
417

42-
const createMockCache = () => {
8+
jest.mock(`gatsby-core-utils/fetch-remote-file`, () => {
9+
const path = require(`path`)
10+
4311
return {
44-
get: jest.fn(),
45-
set: jest.fn(),
46-
directory: __dirname,
12+
fetchRemoteFile: jest.fn(() =>
13+
path.join(__dirname, `fixtures`, `dog-thumbnail.jpg`)
14+
),
4715
}
48-
}
16+
})
4917

5018
describe(`create-remote-file-node`, () => {
5119
const cache = createMockCache()
20+
let uuid = 0
21+
22+
beforeEach(async () => {
23+
uuid = 1
24+
fetchRemoteFile.mockClear()
25+
})
26+
27+
function createMockCache() {
28+
return {
29+
get: jest.fn(),
30+
set: jest.fn(),
31+
directory: __dirname,
32+
}
33+
}
5234

5335
const defaultArgs = {
54-
url: ``,
36+
url: `https://external.com/dog.jpg`,
5537
store: {},
5638
getCache: () => cache,
5739
createNode: jest.fn(),
58-
createNodeId: jest.fn(),
40+
createNodeId: jest.fn(() => String(uuid++)),
5941
reporter,
42+
ext: `.jpg`,
43+
name: `dog-thumbnail`,
6044
}
6145

62-
describe(`basic functionality`, () => {
63-
describe(`non-url`, () => {
64-
it(`short-circuits and resolves`, () => {
65-
const value = createRemoteFileNode({
66-
...defaultArgs,
67-
url: ``,
68-
})
69-
70-
expect(value).rejects.toMatch(
71-
`url passed to createRemoteFileNode is either missing or not a proper web uri: `
72-
)
73-
})
74-
75-
it(`does not increment progress bar total`, () => {
76-
const value = createRemoteFileNode({
77-
...defaultArgs,
78-
url: ``,
79-
})
80-
81-
expect(value).rejects.toMatch(
82-
`url passed to createRemoteFileNode is either missing or not a proper web uri: `
83-
)
46+
it(`should throw an error when an invalid url is given`, () => {
47+
expect(() =>
48+
createRemoteFileNode({
49+
...defaultArgs,
50+
url: ``,
8451
})
85-
})
52+
).toThrowError(
53+
`url passed to createRemoteFileNode is either missing or not a proper web uri: `
54+
)
8655
})
8756

88-
describe(`valid url`, () => {
89-
let uuid = 0
90-
91-
const setup = (args = {}, response = { statusCode: 200 }) => {
92-
const url = `https://images.whatever.com/real-image-trust-me-${uuid}.png`
93-
94-
if (response.statusCode === 404) {
95-
fetchRemoteFile.mockImplementation(({ url }) => {
96-
// eslint-disable-next-line no-throw-literal
97-
throw `failed to process ${url}`
98-
})
99-
}
100-
101-
fetchRemoteFile.mockClear()
102-
103-
createFileNode.mockImplementationOnce(() => {
104-
return {
105-
internal: {},
106-
}
57+
it(`should download and create file node`, async () => {
58+
const node = await createRemoteFileNode(defaultArgs)
59+
60+
expect(node).toMatchObject(
61+
expect.objectContaining({
62+
id: `1`,
63+
extension: `jpg`,
64+
name: `dog-thumbnail`,
65+
base: `dog-thumbnail.jpg`,
66+
url: defaultArgs.url,
67+
internal: expect.objectContaining({
68+
type: `File`,
69+
}),
10770
})
71+
)
72+
})
10873

109-
uuid += 1
74+
it(`passes headers`, async () => {
75+
await createRemoteFileNode({
76+
...defaultArgs,
77+
// bypass cache
78+
url: `${defaultArgs.url}?header`,
79+
httpHeaders: {
80+
"x-test": `test`,
81+
},
82+
})
11083

111-
return createRemoteFileNode({
112-
...defaultArgs,
113-
store: {
114-
getState: jest.fn(() => {
115-
return {
116-
program: {
117-
directory: `__whatever__`,
118-
},
119-
}
120-
}),
84+
expect(fetchRemoteFile).toHaveBeenCalledWith(
85+
expect.objectContaining({
86+
httpHeaders: {
87+
"x-test": `test`,
12188
},
122-
url,
123-
...args,
12489
})
125-
}
90+
)
91+
})
12692

127-
it(`invokes ProgressBar tick`, async () => {
128-
await setup()
93+
it(`passes custom auth header, if defined`, async () => {
94+
const auth = {
95+
htaccess_user: `hunter2`,
96+
htaccess_pass: `*******`,
97+
}
98+
await createRemoteFileNode({
99+
...defaultArgs,
100+
// bypass cache
101+
url: `${defaultArgs.url}?auth`,
102+
auth,
129103
})
130104

131-
describe(`requesting remote image`, () => {
132-
it(`passes correct url`, async () => {
133-
const url = `https://hello.com/image.png`
134-
await setup({
135-
url,
136-
})
137-
138-
expect(fetchRemoteFile).toHaveBeenCalledWith(
139-
expect.objectContaining({
140-
url,
141-
})
142-
)
143-
})
144-
145-
it(`passes headers`, async () => {
146-
await setup()
147-
148-
expect(fetchRemoteFile).toHaveBeenCalledWith(
149-
expect.objectContaining({
150-
httpHeaders: {},
151-
})
152-
)
153-
})
154-
155-
it(`passes custom auth header, if defined`, async () => {
156-
const auth = {
157-
htaccess_user: `hunter2`,
158-
htaccess_pass: `*******`,
159-
}
160-
await setup({
161-
auth,
162-
})
163-
164-
expect(fetchRemoteFile).toHaveBeenCalledWith(
165-
expect.objectContaining({
166-
auth,
167-
})
168-
)
169-
})
170-
171-
it(`passes custom http header, if defined`, async () => {
172-
await setup({
173-
httpHeaders: {
174-
Authorization: `Bearer foobar`,
175-
},
176-
})
177-
178-
expect(fetchRemoteFile).toHaveBeenCalledWith(
179-
expect.objectContaining({
180-
httpHeaders: {
181-
Authorization: `Bearer foobar`,
182-
},
183-
})
184-
)
105+
expect(fetchRemoteFile).toHaveBeenCalledWith(
106+
expect.objectContaining({
107+
auth,
185108
})
109+
)
110+
})
186111

187-
it(`fails when 404 is given`, async () => {
188-
expect.assertions(1)
189-
try {
190-
await setup({}, { statusCode: 404 })
191-
} catch (err) {
192-
expect(err).toEqual(
193-
expect.stringContaining(
194-
`failed to process https://images.whatever.com/real-image-trust-me`
195-
)
196-
)
197-
}
112+
it(`fails when fetchRemoteFile throws an error`, async () => {
113+
expect.assertions(1)
114+
try {
115+
fetchRemoteFile.mockImplementationOnce(({ url }) =>
116+
Promise.reject(new Error(`failed to process ${url}`))
117+
)
118+
await createRemoteFileNode({
119+
...defaultArgs,
120+
// bypass cache
121+
url: `${defaultArgs.url}?error`,
198122
})
199-
})
123+
} catch (err) {
124+
expect(err.message).toEqual(`failed to process ${defaultArgs.url}?error`)
125+
}
200126
})
201127

202128
describe(`validation`, () => {
@@ -223,33 +149,33 @@ describe(`create-remote-file-node`, () => {
223149
})
224150

225151
it(`throws on invalid inputs: cache and getCache undefined`, () => {
226-
expect(() => {
152+
expect(() =>
227153
createRemoteFileNode({
228154
...defaultArgs,
229155
cache: undefined,
230156
getCache: undefined,
231157
})
232-
}).toThrowErrorMatchingInlineSnapshot(
158+
).toThrowErrorMatchingInlineSnapshot(
233159
`"Neither \\"cache\\" or \\"getCache\\" was passed. getCache must be function that return Gatsby cache, \\"cache\\" must be the Gatsby cache, was undefined"`
234160
)
235161
})
236162

237163
it(`doesn't throw when getCache is defined`, () => {
238-
expect(() => {
164+
expect(() =>
239165
createRemoteFileNode({
240166
...defaultArgs,
241167
getCache: () => createMockCache(),
242168
})
243-
}).not.toThrow()
169+
).not.toThrow()
244170
})
245171

246172
it(`doesn't throw when cache is defined`, () => {
247-
expect(() => {
173+
expect(() =>
248174
createRemoteFileNode({
249175
...defaultArgs,
250176
cache: createMockCache(),
251177
})
252-
}).not.toThrow()
178+
).not.toThrow()
253179
})
254180
})
255181
})

packages/gatsby-source-filesystem/src/create-remote-file-node.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,8 @@ module.exports = function createRemoteFileNode({
142142
}
143143

144144
if (!url || isWebUri(url) === undefined) {
145-
return Promise.reject(
146-
new Error(
147-
`url passed to createRemoteFileNode is either missing or not a proper web uri: ${url}`
148-
)
145+
throw new Error(
146+
`url passed to createRemoteFileNode is either missing or not a proper web uri: ${url}`
149147
)
150148
}
151149

0 commit comments

Comments
 (0)