Skip to content

Commit b6d604a

Browse files
committed
Cover mapResponseToForm
1 parent f470979 commit b6d604a

File tree

3 files changed

+296
-9
lines changed

3 files changed

+296
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sdc-qrf",
33
"license": "MIT",
4-
"version": "1.0.0-alpha.2-fhir.11",
4+
"version": "1.0.0-alpha.2-fhir.12",
55
"scripts": {
66
"test": "vitest --watch=false",
77
"test:watch": "vitest --watch",

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ function mapResponseToFormRecursive(
370370
[linkId]: answers.map(({ value, subItems }) => ({
371371
question: text,
372372
value,
373-
items: mapResponseToFormRecursive(subItems, question.item ?? []),
373+
...(question.item?.length ? { items: mapResponseToFormRecursive(subItems, question.item ?? []) } : {}),
374374
})),
375375
};
376376
}, populateItemKey({}));

tests/utils.test.ts

Lines changed: 294 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, test } from 'vitest';
1+
import { describe, expect, test, vi } from 'vitest';
22

33
import { allergiesQuestionnaire } from './resources/questionnaire';
44
import {
@@ -33,6 +33,299 @@ import {
3333
import { FCEQuestionnaire, FCEQuestionnaireItem } from '../src/fce.types';
3434
import { AnswerValue, FormAnswerItems, FormItems, ItemContext, QuestionnaireResponseFormData } from '../src/types';
3535

36+
vi.mock('uuid', () => ({
37+
v4: () => 'itemkey',
38+
}));
39+
40+
describe('mapResponseToForm ', () => {
41+
const questionnaire: FCEQuestionnaire = {
42+
resourceType: 'Questionnaire',
43+
status: 'active',
44+
item: [
45+
{
46+
linkId: 'required-repeatable-group',
47+
type: 'group',
48+
repeats: true,
49+
required: true,
50+
item: [
51+
{
52+
linkId: 'required-question-1',
53+
type: 'text',
54+
required: true,
55+
},
56+
{
57+
linkId: 'non-required-question-1',
58+
type: 'text',
59+
required: true,
60+
},
61+
],
62+
},
63+
{
64+
linkId: 'required-non-repeatable-group',
65+
type: 'group',
66+
required: true,
67+
item: [
68+
{
69+
linkId: 'required-question-2',
70+
type: 'text',
71+
required: true,
72+
},
73+
{
74+
linkId: 'non-required-question-2',
75+
type: 'text',
76+
required: true,
77+
},
78+
],
79+
},
80+
{
81+
linkId: 'non-required-repeatable-group',
82+
type: 'group',
83+
repeats: true,
84+
item: [
85+
{
86+
linkId: 'required-question-3',
87+
type: 'text',
88+
required: true,
89+
},
90+
{
91+
linkId: 'non-required-question-3',
92+
type: 'text',
93+
required: true,
94+
},
95+
],
96+
},
97+
{
98+
linkId: 'non-required-non-repeatable-group',
99+
type: 'group',
100+
item: [
101+
{
102+
linkId: 'required-question-4',
103+
type: 'text',
104+
required: true,
105+
},
106+
{
107+
linkId: 'non-required-question-4',
108+
type: 'text',
109+
required: true,
110+
},
111+
],
112+
},
113+
114+
{
115+
linkId: 'required-question-5',
116+
type: 'text',
117+
required: true,
118+
},
119+
{
120+
linkId: 'non-required-question-5',
121+
type: 'text',
122+
required: true,
123+
},
124+
],
125+
};
126+
127+
test('works correctly for empty QR', () => {
128+
const initialQR: QuestionnaireResponse = {
129+
resourceType: 'QuestionnaireResponse',
130+
status: 'completed',
131+
};
132+
const formItems = mapResponseToForm(initialQR, questionnaire);
133+
expect(formItems).toStrictEqual({
134+
_itemKey: 'itemkey',
135+
'required-repeatable-group': {
136+
items: [
137+
{
138+
_itemKey: 'itemkey',
139+
},
140+
],
141+
question: undefined,
142+
},
143+
});
144+
});
145+
test('works correctly for filled QR', () => {
146+
const initialQR: QuestionnaireResponse = {
147+
resourceType: 'QuestionnaireResponse',
148+
status: 'completed',
149+
item: [
150+
{
151+
linkId: 'required-repeatable-group',
152+
item: [
153+
{
154+
linkId: 'required-question-1',
155+
answer: [{ valueString: 'ok' }],
156+
},
157+
{
158+
linkId: 'non-required-question-1',
159+
answer: [{ valueString: 'ok' }],
160+
},
161+
],
162+
},
163+
{
164+
linkId: 'required-non-repeatable-group',
165+
166+
item: [
167+
{
168+
linkId: 'required-question-2',
169+
answer: [{ valueString: 'ok' }],
170+
},
171+
{
172+
linkId: 'non-required-question-2',
173+
answer: [{ valueString: 'ok' }],
174+
},
175+
],
176+
},
177+
{
178+
linkId: 'non-required-repeatable-group',
179+
180+
item: [
181+
{
182+
linkId: 'required-question-3',
183+
answer: [{ valueString: 'ok' }],
184+
},
185+
{
186+
linkId: 'non-required-question-3',
187+
answer: [{ valueString: 'ok' }],
188+
},
189+
],
190+
},
191+
{
192+
linkId: 'non-required-non-repeatable-group',
193+
item: [
194+
{
195+
linkId: 'required-question-4',
196+
answer: [{ valueString: 'ok' }],
197+
},
198+
{
199+
linkId: 'non-required-question-4',
200+
answer: [{ valueString: 'ok' }],
201+
},
202+
],
203+
},
204+
{
205+
linkId: 'required-question-5',
206+
answer: [{ valueString: 'ok' }],
207+
},
208+
{
209+
linkId: 'non-required-question-5',
210+
answer: [{ valueString: 'ok' }],
211+
},
212+
],
213+
};
214+
const formItems = mapResponseToForm(initialQR, questionnaire);
215+
expect(formItems).toStrictEqual({
216+
_itemKey: 'itemkey',
217+
'required-repeatable-group': {
218+
items: [
219+
{
220+
_itemKey: 'itemkey',
221+
'non-required-question-1': [
222+
{
223+
question: undefined,
224+
value: {
225+
string: 'ok',
226+
},
227+
},
228+
],
229+
'required-question-1': [
230+
{
231+
question: undefined,
232+
value: {
233+
string: 'ok',
234+
},
235+
},
236+
],
237+
},
238+
],
239+
question: undefined,
240+
},
241+
'required-non-repeatable-group': {
242+
items: {
243+
_itemKey: 'itemkey',
244+
'non-required-question-2': [
245+
{
246+
question: undefined,
247+
value: {
248+
string: 'ok',
249+
},
250+
},
251+
],
252+
'required-question-2': [
253+
{
254+
question: undefined,
255+
value: {
256+
string: 'ok',
257+
},
258+
},
259+
],
260+
},
261+
question: undefined,
262+
},
263+
'non-required-repeatable-group': {
264+
items: [
265+
{
266+
_itemKey: 'itemkey',
267+
'non-required-question-3': [
268+
{
269+
question: undefined,
270+
value: {
271+
string: 'ok',
272+
},
273+
},
274+
],
275+
'required-question-3': [
276+
{
277+
question: undefined,
278+
value: {
279+
string: 'ok',
280+
},
281+
},
282+
],
283+
},
284+
],
285+
question: undefined,
286+
},
287+
'non-required-non-repeatable-group': {
288+
items: {
289+
_itemKey: 'itemkey',
290+
'non-required-question-4': [
291+
{
292+
question: undefined,
293+
value: {
294+
string: 'ok',
295+
},
296+
},
297+
],
298+
'required-question-4': [
299+
{
300+
question: undefined,
301+
value: {
302+
string: 'ok',
303+
},
304+
},
305+
],
306+
},
307+
question: undefined,
308+
},
309+
'non-required-question-5': [
310+
{
311+
question: undefined,
312+
value: {
313+
string: 'ok',
314+
},
315+
},
316+
],
317+
'required-question-5': [
318+
{
319+
question: undefined,
320+
value: {
321+
string: 'ok',
322+
},
323+
},
324+
],
325+
});
326+
});
327+
});
328+
36329
test('Transform required repeatable groups with/without answers', () => {
37330
const questionnaire: FCEQuestionnaire = {
38331
resourceType: 'Questionnaire',
@@ -206,7 +499,6 @@ test('Transform nested repeatable-groups', () => {
206499
item: [
207500
{
208501
linkId: 'nested-answer',
209-
210502
answer: [
211503
{
212504
valueString: 'nested answer for the first group 1',
@@ -379,39 +671,34 @@ test('Transform removes missing answers', () => {
379671
items: {
380672
'question-1': [
381673
{
382-
items: {},
383674
value: {
384675
string: 'ok',
385676
},
386677
},
387678
],
388679
'question-2': [
389680
{
390-
items: {},
391681
value: {
392682
string: 'ok',
393683
},
394684
},
395685
],
396686
'question-3': [
397687
{
398-
items: {},
399688
value: {
400689
string: 'ok',
401690
},
402691
},
403692
],
404693
'question-4': [
405694
{
406-
items: {},
407695
value: {
408696
string: 'ok',
409697
},
410698
},
411699
],
412700
'question-5': [
413701
{
414-
items: {},
415702
value: {
416703
string: 'ok',
417704
},

0 commit comments

Comments
 (0)