@@ -146,6 +146,7 @@ describe('isValidFHIR', () => {
146
146
test ( 'Should return true when provided valid FHIR resources' , ( ) => {
147
147
expect ( isValidFHIR ( validResource ) ) . toEqual ( true ) ;
148
148
} ) ;
149
+
149
150
test ( 'Should return false when provided invalid FHIR resources' , ( ) => {
150
151
expect ( isValidFHIR ( invalidResource ) ) . toEqual ( false ) ;
151
152
} ) ;
@@ -155,7 +156,42 @@ describe('invalidResourcesFromBundle', () => {
155
156
test ( 'Should return an empty array when all resources are valid' , ( ) => {
156
157
expect ( invalidResourcesFromBundle ( emptyBundle ) ) . toEqual ( [ ] ) ;
157
158
} ) ;
158
- test ( 'Should return an array of all invalid resources when they exist' , ( ) => {
159
+
160
+ test ( 'Should return an error for each invalid resource' , ( ) => {
161
+ const secondInvalidResource = {
162
+ ...invalidResource ,
163
+ id : 'secondInvalidResource' ,
164
+ } ;
165
+
166
+ const invalidBundleWithTwoResources = {
167
+ resourceType : 'Bundle' ,
168
+ entry : [
169
+ {
170
+ resource : invalidResource ,
171
+ } ,
172
+ {
173
+ resource : secondInvalidResource ,
174
+ } ,
175
+ ] ,
176
+ } ;
177
+
178
+ const response = invalidResourcesFromBundle ( invalidBundleWithTwoResources ) ;
179
+
180
+ const invalidResourceId = `${ invalidResource . resourceType } -${ invalidResource . id } ` ;
181
+ const invalidResourceId2 = `${ secondInvalidResource . resourceType } -${ secondInvalidResource . id } ` ;
182
+
183
+ expect ( response ) . toHaveLength ( 2 ) ;
184
+ expect ( response ) . toEqual ( expect . arrayContaining ( [
185
+ expect . objectContaining ( {
186
+ failureId : invalidResourceId ,
187
+ } ) ,
188
+ expect . objectContaining ( {
189
+ failureId : invalidResourceId2 ,
190
+ } ) ,
191
+ ] ) ) ;
192
+ } ) ;
193
+
194
+ test ( 'Should return detailed error for invalid resource' , ( ) => {
159
195
const invalidBundle = { ...bundleWithOneEntry } ;
160
196
invalidBundle . entry [ 0 ] . resource = invalidResource ;
161
197
// This is dependent on implementation details intrinsic to invalidResourcesFromBundle
@@ -182,4 +218,30 @@ describe('invalidResourcesFromBundle', () => {
182
218
} ,
183
219
] ) ;
184
220
} ) ;
221
+
222
+ test ( 'Should return multiple errors if present within the same resource' , ( ) => {
223
+ // invalidResource already has an invalid gender enum value
224
+ const invalidResourceWithTwoProps = {
225
+ ...invalidResource ,
226
+ birthDate : 'not-a-real-date' ,
227
+ } ;
228
+
229
+ const invalidBundle = {
230
+ resourceType : 'Bundle' ,
231
+ entry : [
232
+ {
233
+ resource : invalidResourceWithTwoProps ,
234
+ } ,
235
+ ] ,
236
+ } ;
237
+
238
+ const response = invalidResourcesFromBundle ( invalidBundle ) ;
239
+
240
+ expect ( response ) . toHaveLength ( 1 ) ;
241
+
242
+ const [ invalidResponseObj ] = response ;
243
+
244
+ expect ( invalidResponseObj . errors ) . toBeDefined ( ) ;
245
+ expect ( invalidResponseObj . errors ) . toHaveLength ( 2 ) ;
246
+ } ) ;
185
247
} ) ;
0 commit comments