@@ -32,113 +32,123 @@ describe('bundler-utils', () => {
3232 describe ( 'module' , ( ) => {
3333 describe ( 'createModuleString' , ( ) => {
3434 it ( 'should select default variant by format and width' , ( ) => {
35- const module = createModuleString (
36- {
35+ const module = createModuleString ( {
36+ select : {
3737 format : 'webp' ,
3838 width : 320
3939 } ,
40- [ createEntry ( 'jpg' , 320 ) , createEntry ( 'webp' , 320 ) , createEntry ( 'webp' , 640 ) ]
41- )
40+ srcSet : [ createEntry ( 'jpg' , 320 ) , createEntry ( 'webp' , 320 ) , createEntry ( 'webp' , 640 ) ]
41+ } )
4242
4343 expect ( module ) . toContain ( 'const url = (__webpack_public_path__) + "image@320w.webp";' )
4444 } )
4545
4646 it ( 'should select default variant by multiplier' , ( ) => {
47- const module = createModuleString (
48- {
47+ const module = createModuleString ( {
48+ select : {
4949 format : 'jpg' ,
5050 width : 0.5
5151 } ,
52- [ createEntry ( 'jpg' , 640 , 1 ) , createEntry ( 'jpg' , 320 , 0.5 ) ]
53- )
52+ srcSet : [ createEntry ( 'jpg' , 640 , 1 ) , createEntry ( 'jpg' , 320 , 0.5 ) ]
53+ } )
5454
5555 expect ( module ) . toContain ( 'const url = (__webpack_public_path__) + "image@320w.jpg";' )
5656 } )
5757
5858 it ( 'should select default variant by id' , ( ) => {
59- const module = createModuleString (
60- {
59+ const module = createModuleString ( {
60+ select : {
6161 id : 'webp640'
6262 } ,
63- [ createEntry ( 'jpg' , 320 ) , createEntry ( 'webp' , 640 ) ]
64- )
63+ srcSet : [ createEntry ( 'jpg' , 320 ) , createEntry ( 'webp' , 640 ) ]
64+ } )
6565
6666 expect ( module ) . toContain ( 'const url = (__webpack_public_path__) + "image@640w.webp";' )
6767 } )
6868
6969 it ( 'should fall back to first variant' , ( ) => {
70- const module = createModuleString (
71- {
70+ const module = createModuleString ( {
71+ select : {
7272 format : 'avif' ,
7373 width : 5000
7474 } ,
75- [ createEntry ( 'jpg' , 320 ) , createEntry ( 'webp' , 640 ) ]
76- )
75+ srcSet : [ createEntry ( 'jpg' , 320 ) , createEntry ( 'webp' , 640 ) ]
76+ } )
7777
7878 expect ( module ) . toContain ( 'const url = (__webpack_public_path__) + "image@320w.jpg";' )
7979 } )
8080
8181 it ( 'should create empty module without variants' , ( ) => {
82- const module = createModuleString (
83- {
82+ const module = createModuleString ( {
83+ select : {
8484 format : 'jpg' ,
8585 width : 640
8686 } ,
87- [ ]
88- )
87+ srcSet : [ ]
88+ } )
8989
9090 expect ( module ) . toContain ( "const url = '';" )
9191 expect ( module ) . toContain ( 'const src = null;' )
9292 expect ( module ) . toContain ( 'export const srcSet = [];' )
9393 } )
9494
9595 it ( 'should reuse url and src references for default variant' , ( ) => {
96- const module = createModuleString (
97- {
96+ const module = createModuleString ( {
97+ select : {
9898 format : 'jpg' ,
9999 width : 320
100100 } ,
101- [ createEntry ( 'jpg' , 320 ) , createEntry ( 'webp' , 320 ) ]
102- )
101+ srcSet : [ createEntry ( 'jpg' , 320 ) , createEntry ( 'webp' , 320 ) ]
102+ } )
103103
104104 expect ( module ) . toContain ( 'url: url' )
105105 expect ( module ) . toContain ( 'export const srcSet = [src, {' )
106106 expect ( module ) . toContain ( '"jpg320": url' )
107107 } )
108108
109109 it ( 'should emit placeholder export' , ( ) => {
110- const module = createModuleString (
111- {
110+ const module = createModuleString ( {
111+ select : {
112112 format : 'jpg' ,
113113 width : 320
114114 } ,
115- [ createEntry ( 'jpg' , 320 ) ] ,
116- 'data:image/webp;base64,abc'
117- )
115+ srcSet : [ createEntry ( 'jpg' , 320 ) ] ,
116+ placeholder : 'data:image/webp;base64,abc'
117+ } )
118118
119119 expect ( module ) . toContain ( 'export const placeholder = "data:image/webp;base64,abc";' )
120120 } )
121121
122+ it ( 'should narrow the variant format for a typescript module' , ( ) => {
123+ const module = createModuleString ( {
124+ select : { } ,
125+ srcSet : [ createEntry ( 'jpg' , 320 ) ] ,
126+ typescript : true
127+ } )
128+
129+ expect ( module ) . toContain ( 'format: "jpg" as const,' )
130+ } )
131+
122132 it ( 'should emit undefined placeholder without data-url' , ( ) => {
123- const module = createModuleString (
124- {
133+ const module = createModuleString ( {
134+ select : {
125135 format : 'jpg' ,
126136 width : 320
127137 } ,
128- [ createEntry ( 'jpg' , 320 ) ]
129- )
138+ srcSet : [ createEntry ( 'jpg' , 320 ) ]
139+ } )
130140
131141 expect ( module ) . toContain ( 'export const placeholder = undefined;' )
132142 } )
133143
134144 it ( 'should map ids to urls' , ( ) => {
135- const module = createModuleString (
136- {
145+ const module = createModuleString ( {
146+ select : {
137147 format : 'jpg' ,
138148 width : 320
139149 } ,
140- [ createEntry ( 'jpg' , 320 ) , createEntry ( 'webp' , 640 ) ]
141- )
150+ srcSet : [ createEntry ( 'jpg' , 320 ) , createEntry ( 'webp' , 640 ) ]
151+ } )
142152
143153 expect ( module ) . toContain ( '"webp640": (__webpack_public_path__) + "image@640w.webp"' )
144154 } )
0 commit comments