@@ -7,24 +7,54 @@ describe('uploadcareLoader', () => {
77 cleanup ( ) ;
88 } ) ;
99
10- test ( "The loader validates the 'src' parameter " , ( ) => {
10+ test ( "The loader returns relative 'src' AS IS in Development environment " , ( ) => {
1111 addEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' , 'test-public-key' ) ;
1212
1313 const src = '/relative/image.jpg' ;
1414
15- const t = ( ) => {
16- uploadcareLoader ( {
17- src,
18- width : 0 ,
19- quality : 80
20- } ) ;
21- } ;
15+ const result = uploadcareLoader ( {
16+ src,
17+ width : 0 ,
18+ quality : 80
19+ } ) ;
2220
23- expect ( t ) . toThrow (
24- `Failed to parse "${ src } " in "uploadcareLoader", Uploadcare loader doesn't support relative images.`
25- ) ;
21+ expect ( result ) . toEqual ( src ) ;
22+
23+ removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' ) ;
24+ } ) ;
25+
26+ test ( "The loader builds up a relative 'src' to the absolute URL and passes it to the proxy in Production environment" , ( ) => {
27+ addEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' , 'test-public-key' ) ;
28+ addEnvVar ( 'NODE_ENV' , 'production' ) ;
29+
30+ // When the base path is not set through env vars.
31+
32+ const src = '/relative/image.jpg' ;
33+
34+ let result = uploadcareLoader ( {
35+ src,
36+ width : 0 ,
37+ quality : 80
38+ } ) ;
2639
40+ expect ( result ) . toEqual ( src ) ;
41+
42+ // When the base path is set through env vars.
43+
44+ const basePath = 'https://example.com' ;
45+ addEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_APP_BASE_PATH' , basePath ) ;
46+
47+ result = uploadcareLoader ( {
48+ src,
49+ width : 0 ,
50+ quality : 80
51+ } ) ;
52+
53+ expect ( result ) . toEqual ( `${ basePath } ${ src } ` ) ;
54+
55+ removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_APP_BASE_PATH' ) ;
2756 removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' ) ;
57+ removeEnvVar ( 'NODE_ENV' ) ;
2858 } ) ;
2959
3060 test ( 'The loader requires either a public key or a custom proxy domain' , ( ) => {
@@ -117,7 +147,7 @@ describe('uploadcareLoader', () => {
117147 removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_TRANSFORMATION_PARAMETERS' ) ;
118148 } ) ;
119149
120- test ( "The loader doesn't process SVG and GIF" , ( ) => {
150+ test ( "The loader doesn't process SVG and GIF (absolute url) " , ( ) => {
121151 const src = 'https:/example.com/image.svg' ;
122152
123153 addEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' , 'test-public-key' ) ;
@@ -128,7 +158,23 @@ describe('uploadcareLoader', () => {
128158 quality : 80
129159 } ) ;
130160
131- expect ( result ) . toBe ( `https://test-public-key.ucr.io${ src } ` ) ;
161+ expect ( result ) . toBe ( src ) ;
162+
163+ removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' ) ;
164+ } ) ;
165+
166+ test ( "The loader doesn't process SVG and GIF (relative url)" , ( ) => {
167+ const src = '/image.svg' ;
168+
169+ addEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' , 'test-public-key' ) ;
170+
171+ const result = uploadcareLoader ( {
172+ src,
173+ width : 500 ,
174+ quality : 80
175+ } ) ;
176+
177+ expect ( result ) . toBe ( src ) ;
132178
133179 removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' ) ;
134180 } ) ;
0 commit comments