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"
24
3
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
- } )
36
4
const reporter = { }
37
5
38
6
const createRemoteFileNode = require ( `../create-remote-file-node` )
39
- const { createFileNode } = require ( `../create-file-node` )
40
- const { fetchRemoteFile } = require ( `gatsby-core-utils/fetch-remote-file` )
41
7
42
- const createMockCache = ( ) => {
8
+ jest . mock ( `gatsby-core-utils/fetch-remote-file` , ( ) => {
9
+ const path = require ( `path` )
10
+
43
11
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
+ ) ,
47
15
}
48
- }
16
+ } )
49
17
50
18
describe ( `create-remote-file-node` , ( ) => {
51
19
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
+ }
52
34
53
35
const defaultArgs = {
54
- url : `` ,
36
+ url : `https://external.com/dog.jpg ` ,
55
37
store : { } ,
56
38
getCache : ( ) => cache ,
57
39
createNode : jest . fn ( ) ,
58
- createNodeId : jest . fn ( ) ,
40
+ createNodeId : jest . fn ( ( ) => String ( uuid ++ ) ) ,
59
41
reporter,
42
+ ext : `.jpg` ,
43
+ name : `dog-thumbnail` ,
60
44
}
61
45
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 : `` ,
84
51
} )
85
- } )
52
+ ) . toThrowError (
53
+ `url passed to createRemoteFileNode is either missing or not a proper web uri: `
54
+ )
86
55
} )
87
56
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
+ } ) ,
107
70
} )
71
+ )
72
+ } )
108
73
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
+ } )
110
83
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` ,
121
88
} ,
122
- url,
123
- ...args ,
124
89
} )
125
- }
90
+ )
91
+ } )
126
92
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,
129
103
} )
130
104
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,
185
108
} )
109
+ )
110
+ } )
186
111
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` ,
198
122
} )
199
- } )
123
+ } catch ( err ) {
124
+ expect ( err . message ) . toEqual ( `failed to process ${ defaultArgs . url } ?error` )
125
+ }
200
126
} )
201
127
202
128
describe ( `validation` , ( ) => {
@@ -223,33 +149,33 @@ describe(`create-remote-file-node`, () => {
223
149
} )
224
150
225
151
it ( `throws on invalid inputs: cache and getCache undefined` , ( ) => {
226
- expect ( ( ) => {
152
+ expect ( ( ) =>
227
153
createRemoteFileNode ( {
228
154
...defaultArgs ,
229
155
cache : undefined ,
230
156
getCache : undefined ,
231
157
} )
232
- } ) . toThrowErrorMatchingInlineSnapshot (
158
+ ) . toThrowErrorMatchingInlineSnapshot (
233
159
`"Neither \\"cache\\" or \\"getCache\\" was passed. getCache must be function that return Gatsby cache, \\"cache\\" must be the Gatsby cache, was undefined"`
234
160
)
235
161
} )
236
162
237
163
it ( `doesn't throw when getCache is defined` , ( ) => {
238
- expect ( ( ) => {
164
+ expect ( ( ) =>
239
165
createRemoteFileNode ( {
240
166
...defaultArgs ,
241
167
getCache : ( ) => createMockCache ( ) ,
242
168
} )
243
- } ) . not . toThrow ( )
169
+ ) . not . toThrow ( )
244
170
} )
245
171
246
172
it ( `doesn't throw when cache is defined` , ( ) => {
247
- expect ( ( ) => {
173
+ expect ( ( ) =>
248
174
createRemoteFileNode ( {
249
175
...defaultArgs ,
250
176
cache : createMockCache ( ) ,
251
177
} )
252
- } ) . not . toThrow ( )
178
+ ) . not . toThrow ( )
253
179
} )
254
180
} )
255
181
} )
0 commit comments