@@ -3,8 +3,9 @@ jest.mock('./asset-data');
3
3
4
4
const gatsbyUtilsMocks = {
5
5
reporter : {
6
- error : jest . fn ( ) ,
7
6
panic : jest . fn ( ) ,
7
+ error : jest . fn ( ) ,
8
+ info : jest . fn ( ) ,
8
9
verbose : jest . fn ( ) ,
9
10
} ,
10
11
} ;
@@ -58,18 +59,22 @@ describe('generateCloudinaryAssetSource', () => {
58
59
} ) ;
59
60
60
61
describe ( 'resolveCloudinaryAssetData' , ( ) => {
61
- const source = {
62
+ const sourceWithMetadata = {
62
63
publicId : 'public-id' ,
63
64
cloudName : 'cloud-name' ,
64
65
originalWidth : '600' ,
65
66
originalHeight : '300' ,
66
67
originalFormat : 'jpg' ,
67
68
} ;
68
69
69
- const args = {
70
- transformations : [ 'e_grayscale' ] ,
70
+ const sourceWithoutMeta = {
71
+ publicId : 'public-id' ,
72
+ cloudName : 'cloud-name' ,
71
73
} ;
72
74
75
+ const context = { } ; // Never used
76
+ const info = { } ;
77
+
73
78
beforeEach ( ( ) => {
74
79
getAssetMetadata . mockResolvedValue ( {
75
80
width : 100 ,
@@ -85,13 +90,15 @@ describe('resolveCloudinaryAssetData', () => {
85
90
jest . clearAllMocks ( ) ;
86
91
} ) ;
87
92
88
- it ( 'calls gatsby-plugin-image -> generateImageData' , async ( ) => {
89
- await resolveCloudinaryAssetData ( source , args ) ;
93
+ it ( 'calls gatsby-plugin-image -> generateImageData once' , async ( ) => {
94
+ const args = { } ;
95
+ await resolveCloudinaryAssetData ( sourceWithMetadata , args , context , info ) ;
90
96
expect ( generateImageData ) . toBeCalledTimes ( 1 ) ;
91
97
} ) ;
92
98
93
99
it ( 'calls gatsby-plugin-image -> generateImageData with correct data' , async ( ) => {
94
- await resolveCloudinaryAssetData ( source , args ) ;
100
+ const args = { transformations : [ 'e_grayscale' ] } ;
101
+ await resolveCloudinaryAssetData ( sourceWithMetadata , args , context , info ) ;
95
102
expect ( generateImageData ) . toBeCalledWith ( {
96
103
filename : 'cloud-name>>>public-id' ,
97
104
generateImageSource : _generateCloudinaryAssetSource ,
@@ -109,12 +116,13 @@ describe('resolveCloudinaryAssetData', () => {
109
116
} ) ;
110
117
111
118
it ( 'fetches metadata when not present on source' , async ( ) => {
112
- await resolveCloudinaryAssetData ( source , { } ) ;
113
- await resolveCloudinaryAssetData (
114
- { publicId : 'public-id' , cloudName : 'cloud-name' } ,
115
- { }
116
- ) ;
119
+ const args = { } ;
120
+ await resolveCloudinaryAssetData ( sourceWithMetadata , args , context , info ) ;
121
+ await resolveCloudinaryAssetData ( sourceWithoutMeta , args , context , info ) ;
122
+ // getAssetMetadata should only be called for sourceWithoutMeta
117
123
expect ( getAssetMetadata ) . toBeCalledTimes ( 1 ) ;
124
+ expect ( gatsbyUtilsMocks . reporter . verbose ) . toBeCalledTimes ( 1 ) ;
125
+ // gatsby-plugin-image -> generateImageData should be called for both
118
126
expect ( generateImageData ) . toHaveBeenNthCalledWith ( 2 , {
119
127
filename : 'cloud-name>>>public-id' ,
120
128
generateImageSource : _generateCloudinaryAssetSource ,
@@ -129,7 +137,8 @@ describe('resolveCloudinaryAssetData', () => {
129
137
} ) ;
130
138
131
139
it ( 'fetches and adds correct "blurred" placeholder' , async ( ) => {
132
- await resolveCloudinaryAssetData ( source , { placeholder : 'blurred' } ) ;
140
+ const args = { placeholder : 'blurred' } ;
141
+ await resolveCloudinaryAssetData ( sourceWithMetadata , args , context , info ) ;
133
142
134
143
expect ( getLowResolutionImageURL ) . toBeCalledTimes ( 1 ) ;
135
144
expect ( getUrlAsBase64Image ) . toBeCalledTimes ( 1 ) ;
@@ -149,7 +158,8 @@ describe('resolveCloudinaryAssetData', () => {
149
158
} ) ;
150
159
151
160
it ( 'fetches and adds correct "tracedSVG" placeholder' , async ( ) => {
152
- await resolveCloudinaryAssetData ( source , { placeholder : 'tracedSVG' } ) ;
161
+ const args = { placeholder : 'tracedSVG' } ;
162
+ await resolveCloudinaryAssetData ( sourceWithMetadata , args , context , info ) ;
153
163
154
164
expect ( getAssetAsTracedSvg ) . toBeCalledTimes ( 1 ) ;
155
165
expect ( generateImageData ) . toHaveBeenCalledWith ( {
@@ -167,6 +177,22 @@ describe('resolveCloudinaryAssetData', () => {
167
177
} ) ;
168
178
} ) ;
169
179
180
+ describe ( 'when missing required data' , ( ) => {
181
+ it ( 'calls reporter.error and returns null' , async ( ) => {
182
+ const source = { } ;
183
+ const args = { } ;
184
+ const result = await resolveCloudinaryAssetData (
185
+ source ,
186
+ args ,
187
+ context ,
188
+ info
189
+ ) ;
190
+ expect ( generateImageData ) . toBeCalledTimes ( 0 ) ;
191
+ expect ( gatsbyUtilsMocks . reporter . error ) . toBeCalledTimes ( 1 ) ;
192
+ expect ( result ) . toBe ( null ) ;
193
+ } ) ;
194
+ } ) ;
195
+
170
196
describe ( 'when error fetching asset data' , ( ) => {
171
197
beforeEach ( ( ) => {
172
198
getAssetMetadata . mockImplementation ( ( ) => {
@@ -185,17 +211,23 @@ describe('resolveCloudinaryAssetData', () => {
185
211
jest . clearAllMocks ( ) ;
186
212
} ) ;
187
213
188
- it ( 'reporter.panic on fetching metdata error' , async ( ) => {
189
- await resolveCloudinaryAssetData (
190
- { publicId : 'public-id' , cloudName : 'cloud-name' } ,
191
- { }
214
+ it ( 'calls reporter.error on fetching metadata error and returns null' , async ( ) => {
215
+ const args = { } ;
216
+ const result = await resolveCloudinaryAssetData (
217
+ sourceWithoutMeta ,
218
+ args ,
219
+ context ,
220
+ info
192
221
) ;
193
222
expect ( getAssetMetadata ) . toBeCalledTimes ( 1 ) ;
194
- expect ( gatsbyUtilsMocks . reporter . panic ) . toBeCalledTimes ( 1 ) ;
223
+ expect ( generateImageData ) . toBeCalledTimes ( 0 ) ;
224
+ expect ( gatsbyUtilsMocks . reporter . error ) . toBeCalledTimes ( 1 ) ;
225
+ expect ( result ) . toBe ( null ) ;
195
226
} ) ;
196
227
197
- it ( 'reporter.errror on fetching blurred placeholder error' , async ( ) => {
198
- await resolveCloudinaryAssetData ( source , { placeholder : 'blurred' } ) ;
228
+ it ( 'calls reporter.error on fetching blurred placeholder error' , async ( ) => {
229
+ const args = { placeholder : 'blurred' } ;
230
+ await resolveCloudinaryAssetData ( sourceWithMetadata , args , context , info ) ;
199
231
expect ( getLowResolutionImageURL ) . toBeCalledTimes ( 1 ) ;
200
232
expect ( getUrlAsBase64Image ) . toBeCalledTimes ( 1 ) ;
201
233
expect ( gatsbyUtilsMocks . reporter . error ) . toBeCalledTimes ( 1 ) ;
@@ -214,8 +246,9 @@ describe('resolveCloudinaryAssetData', () => {
214
246
} ) ;
215
247
} ) ;
216
248
217
- it ( 'reporter.errror on fetching tracedSVG placeholder error' , async ( ) => {
218
- await resolveCloudinaryAssetData ( source , { placeholder : 'tracedSVG' } ) ;
249
+ it ( 'calls reporter.error on fetching tracedSVG placeholder error' , async ( ) => {
250
+ const args = { placeholder : 'tracedSVG' } ;
251
+ await resolveCloudinaryAssetData ( sourceWithMetadata , args , context , info ) ;
219
252
expect ( getAssetAsTracedSvg ) . toBeCalledTimes ( 1 ) ;
220
253
expect ( gatsbyUtilsMocks . reporter . error ) . toBeCalledTimes ( 1 ) ;
221
254
expect ( generateImageData ) . toHaveBeenCalledWith ( {
0 commit comments