1
1
const path = require ( `path` )
2
- const sharp = require ( `sharp` )
3
2
const fs = require ( `fs-extra` )
4
3
jest . mock ( `../scheduler` )
5
4
@@ -16,6 +15,7 @@ jest.mock(`gatsby/dist/redux/actions`, () => {
16
15
}
17
16
} )
18
17
18
+ const sharp = require ( `sharp` )
19
19
const { scheduleJob } = require ( `../scheduler` )
20
20
scheduleJob . mockReturnValue ( Promise . resolve ( ) )
21
21
fs . ensureDirSync = jest . fn ( )
@@ -483,6 +483,31 @@ describe(`gatsby-plugin-sharp`, () => {
483
483
484
484
expect ( result ) . toMatchSnapshot ( )
485
485
} )
486
+
487
+ it ( `should cache same image` , async ( ) => {
488
+ const file1 = getFileObject ( absolutePath )
489
+ const file2 = getFileObject ( absolutePath )
490
+ const file3 = getFileObject ( absolutePath , `test` , `new-image` )
491
+ // change file of file3
492
+ file3 . base = path . join ( __dirname , `images/144-density.png` )
493
+
494
+ const result = await base64 ( {
495
+ file : file1 ,
496
+ args,
497
+ } )
498
+ const result2 = await base64 ( {
499
+ file : file2 ,
500
+ args,
501
+ } )
502
+ const result3 = await base64 ( {
503
+ file : file3 ,
504
+ args,
505
+ } )
506
+
507
+ // I would like to test sharp being executed but I don't really know how to mock that beast :p
508
+ expect ( result ) . toEqual ( result2 )
509
+ expect ( result ) . not . toEqual ( result3 )
510
+ } )
486
511
} )
487
512
488
513
describe ( `image quirks` , ( ) => {
@@ -578,7 +603,7 @@ describe(`gatsby-plugin-sharp`, () => {
578
603
} )
579
604
} )
580
605
581
- function getFileObject ( absolutePath , name = `test` ) {
606
+ function getFileObject ( absolutePath , name = `test` , contentDigest = `1234` ) {
582
607
const parsedPath = path . parse ( absolutePath )
583
608
return {
584
609
id : `${ absolutePath } absPath of file` ,
@@ -587,7 +612,7 @@ function getFileObject(absolutePath, name = `test`) {
587
612
absolutePath,
588
613
extension : `png` ,
589
614
internal : {
590
- contentDigest : `1234` ,
615
+ contentDigest,
591
616
} ,
592
617
}
593
618
}
0 commit comments