-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
index.js
714 lines (602 loc) · 20.4 KB
/
index.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
const path = require(`path`)
const fs = require(`fs-extra`)
jest.mock(`async/queue`, () => () => {
return {
push: jest.fn(),
}
})
jest.mock(`gatsby/dist/redux/actions`, () => {
return {
actions: {
createJobV2: jest.fn().mockReturnValue(Promise.resolve()),
},
}
})
const sharp = require(`sharp`)
fs.ensureDirSync = jest.fn()
fs.existsSync = jest.fn().mockReturnValue(false)
const decodeBase64PngAsRaw = async png =>
sharp(Buffer.from(png.replace(`data:image/png;base64,`, ``), `base64`))
.raw()
.toBuffer()
const {
base64,
generateBase64,
fluid,
fixed,
queueImageResizing,
getImageSize,
getImageSizeAsync,
stats,
setActions,
} = require(`../`)
const {
getPluginOptionsDefaults,
setPluginOptions,
} = require(`../plugin-options`)
jest.mock(`gatsby/reporter`, () => {
return {
log: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
activityTimer: () => {
return {
start: jest.fn(),
setStatus: jest.fn(),
end: jest.fn(),
}
},
}
})
describe(`gatsby-plugin-sharp`, () => {
const args = {
duotone: false,
grayscale: false,
rotate: false,
}
const absolutePath = path.join(__dirname, `images/test.png`)
const file = getFileObject(absolutePath)
// used to find all breakpoints in a srcSet string
const findAllBreakpoints = srcSet => {
// RegEx to find all occurrences of 'Xw', where 'X' can be any int
const regEx = /[0-9]+w/g
return srcSet.match(regEx)
}
describe(`queueImageResizing`, () => {
let actions
beforeEach(() => {
actions = {
createJobV2: jest.fn().mockReturnValue(Promise.resolve()),
}
setActions(actions)
})
it(`should round height when auto-calculated createJobV2`, () => {
// Resize 144-density.png (281x136) with a 3px width
const result = queueImageResizing({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args: { width: 3 },
})
// Width should be: w = (3 * 136) / 281 = 1.451957295
// We expect value to be rounded to 1
expect(result.height).toBe(1)
expect(actions.createJobV2).toHaveBeenCalledTimes(1)
expect(actions.createJobV2).toMatchSnapshot()
})
it(`file name works with spaces & special characters createJobV2`, async () => {
// test name encoding with various characters
const testName = `spaces and '"@#$%^&,`
const queueResult = queueImageResizing({
file: getFileObject(
path.join(__dirname, `images/144-density.png`),
testName
),
args: { width: 3 },
})
const queueResultName = path.parse(queueResult.src).name
// decoding to check for outputting same name
expect(decodeURIComponent(queueResultName)).toBe(testName)
// regex for special characters above and spaces
// testname should match, the queue result should not
expect(testName.match(/[!@#$^&," ]/)).not.toBe(false)
expect(queueResultName.match(/[!@#$^&," ]/)).not.toBe(true)
expect(actions.createJobV2).toHaveBeenCalledTimes(1)
expect(actions.createJobV2).toMatchSnapshot()
})
// re-enable when image processing on demand is implemented
it.skip(`should process immediately when asked`, async () => {
const result = queueImageResizing({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args: { width: 3 },
})
await result.finishedPromise
expect(actions.createJobV2).toMatchSnapshot()
})
})
describe(`fluid`, () => {
const actions = {}
beforeEach(() => {
actions.createJobV2 = jest.fn().mockReturnValue(Promise.resolve())
setActions(actions)
})
it(`includes responsive image properties, e.g. sizes, srcset, etc.`, async () => {
const { base64, ...result } = await fluid({ file })
expect(actions.createJobV2).toHaveBeenCalledTimes(1)
expect(result).toMatchSnapshot()
const rawPixelData = await decodeBase64PngAsRaw(base64)
expect(rawPixelData.toString(`hex`)).toMatchSnapshot()
})
it(`adds pathPrefix if defined`, async () => {
const pathPrefix = `/blog`
const result = await fluid({
file,
args: {
pathPrefix,
},
})
expect(result.src.indexOf(pathPrefix)).toBe(0)
expect(result.srcSet.indexOf(pathPrefix)).toBe(0)
expect(actions.createJobV2).toMatchSnapshot()
})
it(`keeps original file name`, async () => {
const result = await fluid({
file,
})
expect(path.parse(result.src).name).toBe(file.name)
expect(path.parse(result.srcSet).name).toBe(file.name)
expect(actions.createJobV2).toMatchSnapshot()
})
it(`does not change the arguments object it is given`, async () => {
const args = { maxWidth: 400 }
await fluid({
file,
args,
})
expect(args).toEqual({ maxWidth: 400 })
expect(actions.createJobV2).toMatchSnapshot()
})
it(`infers the maxWidth if only maxHeight is given`, async () => {
const args = { maxHeight: 20 }
const result = await fluid({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args,
})
expect(result.presentationWidth).toEqual(41)
expect(actions.createJobV2).toMatchSnapshot()
})
it(`calculate height based on width when maxWidth & maxHeight are present`, async () => {
const testsCases = [
{ args: { maxWidth: 20, maxHeight: 20 }, result: [20, 20] },
{
args: { maxWidth: 20, maxHeight: 20, fit: sharp.fit.fill },
result: [20, 20],
},
{
args: { maxWidth: 20, maxHeight: 20, fit: sharp.fit.inside },
result: [20, 10],
},
{
args: { maxWidth: 20, maxHeight: 20, fit: sharp.fit.outside },
result: [41, 20],
},
{ args: { maxWidth: 200, maxHeight: 200 }, result: [200, 200] },
{
args: { maxWidth: 200, maxHeight: 200, fit: sharp.fit.fill },
result: [200, 200],
},
{
args: { maxWidth: 200, maxHeight: 200, fit: sharp.fit.inside },
result: [200, 97],
},
{
args: { maxWidth: 200, maxHeight: 200, fit: sharp.fit.outside },
result: [413, 200],
},
]
const fileObject = getFileObject(
path.join(__dirname, `images/144-density.png`)
)
for (const testCase of testsCases) {
actions.createJobV2.mockClear()
const result = await fluid({
file: fileObject,
args: testCase.args,
})
expect(actions.createJobV2.mock.calls).toMatchSnapshot()
expect(result.presentationWidth).toEqual(testCase.result[0])
expect(result.presentationHeight).toEqual(testCase.result[1])
}
})
it(`should throw if maxWidth is less than 1`, async () => {
const args = { maxWidth: 0 }
const result = fluid({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args,
})
await expect(result).rejects.toThrow()
expect(actions.createJobV2).toMatchSnapshot()
})
it(`accepts srcSet breakpoints`, async () => {
const srcSetBreakpoints = [50, 70, 150, 250]
const args = { srcSetBreakpoints }
const result = await fluid({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args,
})
// width of the image tested
const originalWidth = 281
const expected = srcSetBreakpoints.map(size => `${size}w`)
// add the original size of `144-density.png`
expected.push(`${originalWidth}w`)
const actual = findAllBreakpoints(result.srcSet)
// should contain all requested sizes as well as the original size
expect(actual).toEqual(expect.arrayContaining(expected))
expect(actions.createJobV2).toMatchSnapshot()
})
it(`should throw on srcSet breakpoints less than 1`, async () => {
const srcSetBreakpoints = [50, 0]
const args = { srcSetBreakpoints }
const result = fluid({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args,
})
await expect(result).rejects.toThrow()
expect(actions.createJobV2).toMatchSnapshot()
})
it(`ensure maxWidth is in srcSet breakpoints`, async () => {
const srcSetBreakpoints = [50, 70, 150]
const maxWidth = 200
const args = {
maxWidth,
srcSetBreakpoints,
}
const result = await fluid({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args,
})
expect(result.srcSet).toEqual(expect.stringContaining(`${maxWidth}w`))
expect(actions.createJobV2).toMatchSnapshot()
})
it(`reject any breakpoints larger than the original width`, async () => {
const srcSetBreakpoints = [
50,
70,
150,
250,
300, // this shouldn't be in the output as it's wider than the original
]
const maxWidth = 500 // this also shouldn't be in the output
const args = {
maxWidth,
srcSetBreakpoints,
}
const result = await fluid({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args,
})
// width of the image tested
const originalWidth = 281
const expected = srcSetBreakpoints
// filter out the widths that are larger than the source image width
.filter(size => size < originalWidth)
.map(size => `${size}w`)
// add the original size of `144-density.png`
expected.push(`${originalWidth}w`)
const actual = findAllBreakpoints(result.srcSet)
// should contain all requested sizes as well as the original size
expect(actual).toEqual(expect.arrayContaining(expected))
// should contain no other sizes
expect(actual.length).toEqual(expected.length)
expect(actions.createJobV2).toMatchSnapshot()
})
it(`prevents duplicate breakpoints`, async () => {
const srcSetBreakpoints = [50, 50, 100, 100, 100, 250, 250]
const maxWidth = 100
const args = {
maxWidth,
srcSetBreakpoints,
}
const result = await fluid({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args,
})
const originalWidth = 281
const expected = [`50w`, `100w`, `250w`, `${originalWidth}w`]
const actual = findAllBreakpoints(result.srcSet)
expect(actual).toEqual(expect.arrayContaining(expected))
expect(actual.length).toEqual(expected.length)
expect(actions.createJobV2).toMatchSnapshot()
})
})
describe(`fixed`, () => {
const actions = {}
beforeEach(() => {
actions.createJobV2 = jest.fn().mockReturnValue(Promise.resolve())
setActions(actions)
console.warn = jest.fn()
})
it(`does not warn when the requested width is equal to the image width`, async () => {
const args = { width: 1 }
const result = await fixed({
file,
args,
})
expect(result.width).toEqual(1)
expect(console.warn).toHaveBeenCalledTimes(0)
expect(actions.createJobV2).toMatchSnapshot()
})
it(`warns when the requested width is greater than the image width`, async () => {
const { width } = await sharp(file.absolutePath).metadata()
const args = { width: width * 2 }
const result = await fixed({
file,
args,
})
expect(result.width).toEqual(width)
expect(console.warn).toHaveBeenCalledTimes(1)
expect(actions.createJobV2).toMatchSnapshot()
})
it(`correctly infers the width when only the height is given`, async () => {
const args = { height: 10 }
const result = await fixed({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args,
})
expect(result.width).toEqual(21)
expect(actions.createJobV2).toMatchSnapshot()
})
})
describe(`base64`, () => {
it(`converts image to base64`, async () => {
const { src, ...result } = await base64({
file,
args,
})
expect(result).toMatchSnapshot()
const rawPixelData = await decodeBase64PngAsRaw(src)
expect(rawPixelData.toString(`hex`)).toMatchSnapshot()
})
it(`should cache same image`, async () => {
const file1 = getFileObject(absolutePath)
const file2 = getFileObject(absolutePath)
const file3 = getFileObject(absolutePath, `test`, `new-image`)
// change file of file3
file3.base = path.join(__dirname, `images/144-density.png`)
const result = await base64({
file: file1,
args,
})
const result2 = await base64({
file: file2,
args,
})
const result3 = await base64({
file: file3,
args,
})
// I would like to test sharp being executed but I don't really know how to mock that beast :p
expect(result).toEqual(result2)
expect(result).not.toEqual(result3)
})
// Matches base64 string in snapshot, converts to jpg to force use of bg
// Testing pixel colour for a match would be better
it(`should support option: 'background'`, async () => {
const result = await generateBase64({
file: getFileObject(path.join(__dirname, `images/alphatest.png`)),
args: {
background: `#ff0000`,
toFormatBase64: `jpg`,
},
})
expect(result).toMatchSnapshot()
})
describe(`should support option: 'base64Width'`, () => {
// Uses 'generateBase64()` directly to avoid `base64()` caching affecting results.
it(`should support a configurable width`, async () => {
const result = await generateBase64({
file,
args: { base64Width: 42 },
})
expect(result.width).toEqual(42)
})
it(`should support a configurable default width`, async () => {
setPluginOptions({ base64Width: 32 })
const result = await generateBase64({
file,
args,
})
expect(result.width).toEqual(32)
setPluginOptions(getPluginOptionsDefaults())
})
it(`width via arg overrides global default`, async () => {
setPluginOptions({ base64Width: 32 })
const result = await generateBase64({
file,
args: { base64Width: 42 },
})
expect(result.width).toEqual(42)
setPluginOptions(getPluginOptionsDefaults())
})
})
describe(`should support options: 'toFormatBase64' and 'forceBase64Format'`, () => {
it(`should support a different image format for base64`, async () => {
const result = await generateBase64({
file,
args: { toFormatBase64: `webp` },
})
expect(result.src).toEqual(
expect.stringMatching(/^data:image\/webp;base64/)
)
})
it(`should support a configurable default base64 image format`, async () => {
setPluginOptions({ forceBase64Format: `webp` })
const result = await generateBase64({
file,
args,
})
expect(result.src).toEqual(
expect.stringMatching(/^data:image\/webp;base64/)
)
setPluginOptions(getPluginOptionsDefaults())
})
it(`image format via arg overrides global default`, async () => {
setPluginOptions({ forceBase64Format: `png` })
const result = await generateBase64({
file,
args: { toFormatBase64: `webp` },
})
expect(result.src).toEqual(
expect.stringMatching(/^data:image\/webp;base64/)
)
setPluginOptions(getPluginOptionsDefaults())
})
})
})
describe(`image quirks`, () => {
// issue https:/nodeca/probe-image-size/issues/20
it(`handles padding bytes correctly`, () => {
const result = getImageSize(
getFileObject(path.join(__dirname, `images/padding-bytes.jpg`))
)
expect(result).toMatchSnapshot()
})
it(`handles padding bytes correctly in async version`, async () => {
const result = await getImageSizeAsync(
getFileObject(path.join(__dirname, `images/padding-bytes.jpg`))
)
expect(result).toMatchInlineSnapshot(`
Object {
"hUnits": "px",
"height": 1000,
"mime": "image/jpeg",
"type": "jpg",
"wUnits": "px",
"width": 746,
}
`)
})
it(`handles really wide aspect ratios for blurred placeholder`, async () => {
const result = await base64({
file: getFileObject(
path.join(__dirname, `images/wide-aspect-ratio.png`),
`wide-aspect-ratio`,
`1000x10`
),
args,
})
expect(result.width).toEqual(20)
expect(result.height).toEqual(1)
})
})
describe(`tracedSVG`, () => {
it(`doesn't always run`, async () => {
const args = {
maxWidth: 100,
width: 100,
tracedSVG: { color: `#FF0000` },
}
let result = await fixed({
file,
args,
})
expect(result.tracedSVG).toBeUndefined()
result = await fluid({
file,
args,
})
expect(result.tracedSVG).toBeUndefined()
})
it(`runs on demand (and falls back to blurred)`, async () => {
const args = {
maxWidth: 100,
width: 100,
generateTracedSVG: true,
tracedSVG: { color: `#FF0000` },
base64: false,
}
const { tracedSVG: fixedTracedSVG, ...fixedSvg } = await fixed({
file,
args,
})
expect(fixedSvg).toMatchSnapshot(`fixed`)
expect(fixedTracedSVG).toMatch(`data:image/png;base64`)
expect(fixedTracedSVG).not.toMatch(`data:image/svg+xml`)
const fixedRawPixelData = await decodeBase64PngAsRaw(fixedTracedSVG)
expect(fixedRawPixelData.toString(`hex`)).toMatchSnapshot()
const { tracedSVG: fluidTracedSVG, ...fluidSvg } = await fluid({
file,
args,
})
expect(fluidSvg).toMatchSnapshot(`fluid`)
expect(fluidTracedSVG).toMatch(`data:image/png;base64`)
expect(fluidTracedSVG).not.toMatch(`data:image/svg+xml`)
const fluidRawPixelData = await decodeBase64PngAsRaw(fluidTracedSVG)
expect(fluidRawPixelData.toString(`hex`)).toMatchSnapshot()
})
})
describe(`duotone`, () => {
const args = {
maxWidth: 100,
width: 100,
duotone: { highlight: `#ffffff`, shadow: `#cccccc`, opacity: 50 },
}
it(`fixed`, async () => {
const { base64, ...result } = await fixed({ file, args })
expect(result).toMatchSnapshot()
const rawPixelData = await decodeBase64PngAsRaw(base64)
expect(rawPixelData.toString(`hex`)).toMatchSnapshot()
})
it(`fluid`, async () => {
const { base64, ...result } = await fluid({ file, args })
expect(result).toMatchSnapshot()
const rawPixelData = await decodeBase64PngAsRaw(base64)
expect(rawPixelData.toString(`hex`)).toMatchSnapshot()
})
it(`creates two different images for different duotone settings`, async () => {
const testName = `duotone-digest-test`
const firstImage = await fluid({
file: getFileObject(path.join(__dirname, `images/test.png`), testName),
args: {
maxWidth: 100,
width: 100,
duotone: { highlight: `#BBFFE6`, shadow: `#51758D` },
},
})
const secondImage = await fluid({
file: getFileObject(path.join(__dirname, `images/test.png`), testName),
args: {
maxWidth: 100,
width: 100,
duotone: { highlight: `#F1D283`, shadow: `#000000` },
},
})
expect(firstImage.src).not.toEqual(secondImage.src)
})
})
describe(`stats`, () => {
it(`determines if the image is transparent, based on the presence and use of alpha channel`, async () => {
const result = await stats({ file, args })
expect(result).toMatchSnapshot()
expect(result.isTransparent).toEqual(false)
const alphaResult = await stats({
file: getFileObject(path.join(__dirname, `images/alphatest.png`)),
args,
})
expect(alphaResult).toMatchSnapshot()
expect(alphaResult.isTransparent).toEqual(true)
})
})
})
function getFileObject(absolutePath, name = `test`, contentDigest = `1234`) {
const parsedPath = path.parse(absolutePath)
return {
id: `${absolutePath} absPath of file`,
name: name,
base: parsedPath.base,
absolutePath,
extension: `png`,
internal: {
contentDigest,
},
}
}