Skip to content

Commit 7ece085

Browse files
author
Matthew Gramigna
committed
Add more tests for error cases
1 parent 510c8b5 commit 7ece085

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

test/helpers/fhirUtils.test.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ describe('isValidFHIR', () => {
146146
test('Should return true when provided valid FHIR resources', () => {
147147
expect(isValidFHIR(validResource)).toEqual(true);
148148
});
149+
149150
test('Should return false when provided invalid FHIR resources', () => {
150151
expect(isValidFHIR(invalidResource)).toEqual(false);
151152
});
@@ -155,7 +156,42 @@ describe('invalidResourcesFromBundle', () => {
155156
test('Should return an empty array when all resources are valid', () => {
156157
expect(invalidResourcesFromBundle(emptyBundle)).toEqual([]);
157158
});
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', () => {
159195
const invalidBundle = { ...bundleWithOneEntry };
160196
invalidBundle.entry[0].resource = invalidResource;
161197
// This is dependent on implementation details intrinsic to invalidResourcesFromBundle
@@ -182,4 +218,30 @@ describe('invalidResourcesFromBundle', () => {
182218
},
183219
]);
184220
});
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+
});
185247
});

0 commit comments

Comments
 (0)