Skip to content

Commit 993ba6c

Browse files
committed
Cover with unit tests all cases of findAnswerForQuestion
1 parent 3317ec7 commit 993ba6c

File tree

2 files changed

+261
-391
lines changed

2 files changed

+261
-391
lines changed

src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ export function mapResponseToForm(resource: QuestionnaireResponse, questionnaire
388388

389389
function findAnswersForQuestionsRecursive(linkId: string, values?: FormItems): any | null {
390390
// TODO: specify types for returning value
391+
// TODO: pass Questionnaire structure to make code robust
391392
if (values && _.has(values, linkId)) {
392393
return values[linkId];
393394
}
@@ -435,6 +436,8 @@ function findAnswersForQuestionsRecursive(linkId: string, values?: FormItems): a
435436
);
436437
}
437438

439+
// TODO: deprecate usage of this function because it relies on internals
440+
// TODO: pass Questionnaire structure to make code robust, currently it uses isNaN that might work falsy when linkId is number
438441
export function findAnswersForQuestion(linkId: string, parentPath: string[], values: FormItems): FormAnswerItems[] {
439442
if (linkId === ITEM_KEY) {
440443
return [];
@@ -448,11 +451,12 @@ export function findAnswersForQuestion(linkId: string, parentPath: string[], val
448451

449452
// Find answers in parent groups (including repeatable)
450453
// They might have either 'items' of the group or number of the repeatable group in path
454+
// TODO: using isNaN might return invalid value for linkId like '0'
451455
if (part === 'items' || !isNaN(part as any)) {
456+
// TODO: specify type FormItems, and handle group's linkId
452457
const parentGroup = _.get(values, [...p, part]);
453458

454459
if (typeof parentGroup === 'object' && linkId in parentGroup) {
455-
// TODO: specify type
456460
return cleanFormAnswerItems(parentGroup[linkId]);
457461
}
458462
}

0 commit comments

Comments
 (0)