@@ -7,24 +7,57 @@ 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 = ( ) => {
15+ const result =
1616 uploadcareLoader ( {
1717 src,
1818 width : 0 ,
1919 quality : 80
2020 } ) ;
21- } ;
2221
23- expect ( t ) . toThrow (
24- `Failed to parse "${ src } " in "uploadcareLoader", Uploadcare loader doesn't support relative images.`
25- ) ;
22+ expect ( result ) . toEqual ( src ) ;
23+
24+ removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' ) ;
25+ } ) ;
26+
27+ test ( "The loader builds up a relative 'src' to the absolute URL and passes it to the proxy in Production environment" , ( ) => {
28+ addEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' , 'test-public-key' ) ;
29+ addEnvVar ( 'NODE_ENV' , 'production' ) ;
30+
31+ // When the base path is not set through env vars.
32+
33+ const src = '/relative/image.jpg' ;
34+
35+ let result =
36+ uploadcareLoader ( {
37+ src,
38+ width : 0 ,
39+ quality : 80
40+ } ) ;
41+
42+ expect ( result ) . toEqual ( src ) ;
43+
44+ // When the base path is set through env vars.
45+
46+ const basePath = 'https://example.com' ;
47+ addEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_APP_BASE_PATH' , basePath ) ;
48+
49+ result =
50+ uploadcareLoader ( {
51+ src,
52+ width : 0 ,
53+ quality : 80
54+ } ) ;
55+
56+ expect ( result ) . toEqual ( `${ basePath } ${ src } ` ) ;
2657
58+ removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_APP_BASE_PATH' ) ;
2759 removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' ) ;
60+ removeEnvVar ( 'NODE_ENV' ) ;
2861 } ) ;
2962
3063 test ( 'The loader requires either a public key or a custom proxy domain' , ( ) => {
@@ -117,7 +150,7 @@ describe('uploadcareLoader', () => {
117150 removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_TRANSFORMATION_PARAMETERS' ) ;
118151 } ) ;
119152
120- test ( "The loader doesn't process SVG and GIF" , ( ) => {
153+ test ( "The loader doesn't process SVG and GIF (absolute url) " , ( ) => {
121154 const src = 'https:/example.com/image.svg' ;
122155
123156 addEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' , 'test-public-key' ) ;
@@ -128,7 +161,23 @@ describe('uploadcareLoader', () => {
128161 quality : 80
129162 } ) ;
130163
131- expect ( result ) . toBe ( `https://test-public-key.ucr.io${ src } ` ) ;
164+ expect ( result ) . toBe ( src ) ;
165+
166+ removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' ) ;
167+ } ) ;
168+
169+ test ( "The loader doesn't process SVG and GIF (relative url)" , ( ) => {
170+ const src = '/image.svg' ;
171+
172+ addEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' , 'test-public-key' ) ;
173+
174+ const result = uploadcareLoader ( {
175+ src,
176+ width : 500 ,
177+ quality : 80
178+ } ) ;
179+
180+ expect ( result ) . toBe ( src ) ;
132181
133182 removeEnvVar ( 'NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY' ) ;
134183 } ) ;
0 commit comments