@@ -6,6 +6,9 @@ import { onPostBuild } from '../src/';
66
77const mocksPath = path . join ( __dirname , 'mocks/html' ) ;
88const tempPath = path . join ( mocksPath , 'temp' ) ;
9+ // Avoid illegal characters in file paths, all operating systems
10+ const replaceRegEx = / [ \W _ ] + / g
11+ const replaceValue = '_'
912
1013async function mkdir ( directoryPath ) {
1114 let dir ;
@@ -36,7 +39,7 @@ describe('onPostBuild', () => {
3639 process . env . CLOUDINARY_API_SECRET = 'abcd1234' ;
3740
3841 const mockFiles = ( await fs . readdir ( mocksPath ) ) . filter ( filePath => filePath . includes ( '.html' ) ) ;
39- const tempTestPath = path . join ( tempPath , expect . getState ( ) . currentTestName . replace ( '/' , '_' ) ) ;
42+ const tempTestPath = path . join ( tempPath , expect . getState ( ) . currentTestName . replace ( replaceRegEx , replaceValue ) ) ;
4043 await mkdir ( tempTestPath ) ;
4144 await Promise . all ( mockFiles . map ( async file => {
4245 await fs . copyFile ( path . join ( mocksPath , file ) , path . join ( tempTestPath , file ) ) ;
@@ -46,7 +49,7 @@ describe('onPostBuild', () => {
4649 afterEach ( async ( ) => {
4750 process . env = ENV_ORIGINAL ;
4851
49- await fs . rm ( path . join ( tempPath , expect . getState ( ) . currentTestName . replace ( '/' , '_' ) ) , { recursive : true , force : true } ) ;
52+ await fs . rm ( path . join ( tempPath , expect . getState ( ) . currentTestName . replace ( replaceRegEx , replaceValue ) ) , { recursive : true , force : true } ) ;
5053 } ) ;
5154
5255 afterAll ( async ( ) => {
@@ -66,7 +69,7 @@ describe('onPostBuild', () => {
6669
6770 const deliveryType = 'fetch' ;
6871
69- const tempTestPath = path . join ( tempPath , expect . getState ( ) . currentTestName . replace ( '/' , '_' ) ) ;
72+ const tempTestPath = path . join ( tempPath , expect . getState ( ) . currentTestName . replace ( replaceRegEx , replaceValue ) ) ;
7073
7174 await onPostBuild ( {
7275 constants : {
@@ -105,7 +108,7 @@ describe('onPostBuild', () => {
105108
106109 const deliveryType = 'fetch' ;
107110
108- const tempTestPath = path . join ( tempPath , expect . getState ( ) . currentTestName . replace ( '/' , '_' ) ) ;
111+ const tempTestPath = path . join ( tempPath , expect . getState ( ) . currentTestName . replace ( replaceRegEx , replaceValue ) ) ;
109112
110113 const maxSize = {
111114 width : 800 ,
@@ -140,4 +143,50 @@ describe('onPostBuild', () => {
140143
141144 } ) ;
142145
146+ describe ( 'loadingStrategy' , ( ) => {
147+ test . each ( [
148+ { loadingStrategy : undefined , expected : 'lazy' } ,
149+ { loadingStrategy : 'lazy' , expected : 'lazy' } ,
150+ { loadingStrategy : 'eager' , expected : 'eager' } ,
151+ ] ) ( 'should use $expected as img loading attribute when netlify.toml loadingStrategy is $loadingStrategy' , async ( { loadingStrategy, expected} ) => {
152+ process . env . CONTEXT = 'production' ;
153+ process . env . NETLIFY_HOST = 'https://netlify-plugin-cloudinary.netlify.app' ;
154+
155+ // Tests to ensure that delivery type of fetch works without API Key and Secret as it should
156+
157+ delete process . env . CLOUDINARY_API_KEY ;
158+ delete process . env . CLOUDINARY_API_SECRET ;
159+
160+ const deliveryType = 'fetch' ;
161+
162+ const tempTestPath = path . join ( tempPath , expect . getState ( ) . currentTestName . replace ( replaceRegEx , replaceValue ) ) ;
163+
164+ const inputs = {
165+ deliveryType,
166+ folder : process . env . SITE_NAME
167+ }
168+
169+ if ( loadingStrategy != undefined ) {
170+ inputs [ 'loadingStrategy' ] = loadingStrategy
171+ }
172+
173+ await onPostBuild ( {
174+ constants : {
175+ PUBLISH_DIR : tempTestPath
176+ } ,
177+ inputs : inputs ,
178+ } ) ;
179+
180+ const files = await fs . readdir ( tempTestPath ) ;
181+
182+ await Promise . all ( files . map ( async file => {
183+ const data = await fs . readFile ( path . join ( tempTestPath , file ) , 'utf-8' ) ;
184+ const dom = new JSDOM ( data ) ;
185+ const images = Array . from ( dom . window . document . querySelectorAll ( 'img' ) ) ;
186+ images . forEach ( image => {
187+ expect ( image . getAttribute ( 'loading' ) ) . toMatch ( expected ) ;
188+ } )
189+ } ) ) ;
190+ } )
191+ } ) ;
143192} ) ;
0 commit comments