-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
utils.js
388 lines (359 loc) · 9.14 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
jest.mock(`gatsby/reporter`)
const { calculateImageSizes } = require(`../utils`)
const reporter = require(`gatsby/reporter`)
const sharp = require(`sharp`)
const file = {
absolutePath: `~/Usr/gatsby-sites/src/img/photo.png`,
}
const imgDimensions = {
width: 1200,
height: 800,
}
describe(`calculateImageSizes (fixed)`, () => {
it(`should throw if width is less than 1`, () => {
const args = {
layout: `fixed`,
width: 0,
file,
imgDimensions,
}
const getSizes = () => calculateImageSizes(args)
expect(getSizes).toThrow()
})
it(`should throw if height is less than 1`, () => {
const args = {
layout: `fixed`,
height: -50,
file,
imgDimensions,
}
const getSizes = () => calculateImageSizes(args)
expect(getSizes).toThrow()
})
it(`should return the original width of the image when only width is provided`, () => {
const args = {
layout: `fixed`,
width: 600,
file,
imgDimensions,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toContain(600)
})
it(`should return the original width of the image when only height is provided`, () => {
const args = {
layout: `fixed`,
height: 500,
file,
imgDimensions,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toContain(500 * (imgDimensions.width / imgDimensions.height))
})
it(`should create images of different sizes based on pixel densities with a given width`, () => {
const args = {
layout: `fixed`,
width: 120,
file,
imgDimensions,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toEqual([120, 240])
})
it(`should create images of different sizes based on pixel densities with a given height`, () => {
const args = {
layout: `fixed`,
height: 80,
file,
imgDimensions,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toEqual([120, 240])
})
})
describe(`calculateImageSizes (fullWidth & constrained)`, () => {
it(`should throw if width is less than 1`, () => {
const args = {
layout: `constrained`,
width: 0,
file,
imgDimensions,
}
const getSizes = () => calculateImageSizes(args)
expect(getSizes).toThrow()
})
it(`should throw if height is less than 1`, () => {
const args = {
layout: `constrained`,
height: -50,
file,
imgDimensions,
}
const getSizes = () => calculateImageSizes(args)
expect(getSizes).toThrow()
})
it(`should include the original size of the image when width is passed`, () => {
const args = {
layout: `constrained`,
width: 400,
file,
imgDimensions,
reporter,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toContain(400)
})
it(`should include the original size of the image when height is passed`, () => {
const args = {
layout: `fullWidth`,
height: 300,
file,
imgDimensions,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toContain(450)
})
it(`should create images of different sizes (0.25x, 0.5x, 1x, 2x) from a width`, () => {
const args = {
layout: `fullWidth`,
width: 320,
file,
imgDimensions,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toEqual([80, 160, 320, 640])
})
it(`should create images of different sizes (0.25x, 0.5x, 1x) without any defined size provided`, () => {
const args = {
layout: `fullWidth`,
file,
imgDimensions,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toEqual([200, 400, 800])
})
it(`should return sizes of provided breakpoints in fullWidth`, () => {
const breakpoints = [50, 70, 150, 250, 300]
const width = 500
const args = {
layout: `fullWidth`,
width,
breakpoints,
file,
imgDimensions,
reporter,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toEqual([50, 70, 150, 250, 300])
})
it(`should include provided width along with breakpoints in constrained`, () => {
const breakpoints = [50, 70, 150, 250, 300]
const width = 500
const args = {
layout: `constrained`,
width,
breakpoints,
file,
imgDimensions,
reporter,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toEqual([50, 70, 150, 250, 300, 500])
})
it(`should reject any breakpoints larger than the original width`, () => {
const breakpoints = [
50,
70,
150,
250,
1200,
1800, // shouldn't be included, larger than original width
]
const width = 1500 // also shouldn't be included
const args = {
layout: `fullWidth`,
width,
breakpoints,
file,
imgDimensions,
reporter,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toEqual([50, 70, 150, 250, 1200])
})
it(`should add the original width instead of larger breakpoints`, () => {
const breakpoints = [
50,
70,
150,
250,
1800, // shouldn't be included, larger than original width
]
const width = 1300
const args = {
layout: `fullWidth`,
width,
breakpoints,
file,
imgDimensions,
reporter,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toEqual([50, 70, 150, 250, 1200])
})
it(`should ignore outputPixelDensities when breakpoints are passed in`, () => {
const breakpoints = [400, 800] // should find these
const width = 500
const args = {
layout: `fullWidth`,
width,
outputPixelDensities: [2, 4], // and ignore these, ie [1000, 2000]
breakpoints,
file,
imgDimensions,
reporter,
}
const { sizes } = calculateImageSizes(args)
expect(sizes).toEqual([400, 800])
})
it(`should adjust fullWidth sizes according to fit type`, () => {
const imgDimensions = {
width: 2810,
height: 1360,
}
const outputPixelDensities = [1]
const testsCases = [
{ args: { width: 20, height: 20 }, result: [20, 20] },
{
args: {
width: 20,
height: 20,
transformOptions: { fit: sharp.fit.fill },
},
result: [20, 20],
},
{
args: {
width: 20,
height: 20,
transformOptions: { fit: sharp.fit.inside },
},
result: [20, 10],
},
{
args: {
width: 20,
height: 20,
transformOptions: { fit: sharp.fit.outside },
},
result: [41, 20],
},
{ args: { width: 200, height: 200 }, result: [200, 200] },
{
args: {
width: 200,
height: 200,
transformOptions: { fit: sharp.fit.fill },
},
result: [200, 200],
},
{
args: {
width: 200,
height: 200,
transformOptions: { fit: sharp.fit.inside },
},
result: [200, 97],
},
{
args: {
width: 200,
height: 200,
transformOptions: { fit: sharp.fit.outside },
},
result: [413, 200],
},
]
testsCases.forEach(({ args, result }) => {
const { presentationWidth, presentationHeight } = calculateImageSizes({
...args,
file,
outputPixelDensities,
reporter,
imgDimensions,
layout: `fullWidth`,
})
expect([presentationWidth, presentationHeight]).toEqual(result)
})
})
it(`should adjust fixed sizes according to fit type`, () => {
const imgDimensions = {
width: 2810,
height: 1360,
}
const outputPixelDensities = [1]
const testsCases = [
{ args: { width: 20, height: 20 }, result: [20, 20] },
{
args: {
width: 20,
height: 20,
transformOptions: { fit: sharp.fit.fill },
},
result: [20, 20],
},
{
args: {
width: 20,
height: 20,
transformOptions: { fit: sharp.fit.inside },
},
result: [20, 10],
},
{
args: {
width: 20,
height: 20,
transformOptions: { fit: sharp.fit.outside },
},
result: [41, 20],
},
{ args: { width: 200, height: 200 }, result: [200, 200] },
{
args: {
width: 200,
height: 200,
transformOptions: { fit: sharp.fit.fill },
},
result: [200, 200],
},
{
args: {
width: 200,
height: 200,
transformOptions: { fit: sharp.fit.inside },
},
result: [200, 97],
},
{
args: {
width: 200,
height: 200,
transformOptions: { fit: sharp.fit.outside },
},
result: [413, 200],
},
]
testsCases.forEach(({ args, result }) => {
const { presentationWidth, presentationHeight } = calculateImageSizes({
...args,
file,
outputPixelDensities,
reporter,
imgDimensions,
layout: `fixed`,
})
expect([presentationWidth, presentationHeight]).toEqual(result)
})
})
})