Skip to content

Commit af3a85a

Browse files
authored
Merge pull request #224 from ONSdigital/fix-routing-with-list-collector
Fix routing with list collector
2 parents 548bd21 + dc403c9 commit af3a85a

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

src/eq_schema/builders/routing2/newRoutingDestination/index.test.js

+77
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,83 @@ describe("Should build a runner representation of a binary expression", () => {
5151
});
5252
});
5353

54+
it("should build logic rule in questionnaires containing a page without answers", () => {
55+
const expression = buildBinaryExpression(["123"], "OneOf");
56+
const runnerExpression = checkValidRoutingType(expression, {
57+
questionnaireJson: {
58+
sections: [
59+
{
60+
id: "section-1",
61+
title: "Section 1",
62+
folders: [
63+
{
64+
id: "list-collector-folder-1",
65+
listId: "list-1",
66+
pages: [
67+
{
68+
id: "list-collector-qualifier-page-1",
69+
title: "Qualifier page 1",
70+
answers: [
71+
{
72+
id: "qualifier-answer-1",
73+
type: "Radio",
74+
options: [
75+
{
76+
id: "qualifier-option-1",
77+
label: "Yes",
78+
},
79+
{
80+
id: "qualifier-option-2",
81+
label: "No",
82+
},
83+
],
84+
},
85+
],
86+
},
87+
{
88+
id: "list-collector-add-item-page-1",
89+
title: "Add item page 1",
90+
},
91+
{
92+
id: "list-collector-confirmation-page-1",
93+
title: "Confirmation page 1",
94+
answers: [
95+
{
96+
id: "confirmation-answer-1",
97+
type: "Radio",
98+
options: [
99+
{
100+
id: "confirmation-option-1",
101+
label: "Yes",
102+
},
103+
{
104+
id: "confirmation-option-2",
105+
label: "No",
106+
},
107+
],
108+
},
109+
],
110+
},
111+
],
112+
},
113+
],
114+
},
115+
...questionnaireJson.sections,
116+
],
117+
},
118+
});
119+
120+
expect(runnerExpression).toMatchObject({
121+
in: [
122+
{
123+
identifier: "answer1",
124+
source: "answers",
125+
},
126+
["red"],
127+
],
128+
});
129+
});
130+
54131
it("With a radio answer and no selected options", () => {
55132
const expression = buildBinaryExpression(["123", "456"], "Unanswered");
56133
const runnerExpression = checkValidRoutingType(expression, {

src/utils/functions/answerGetters.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const getAnswerById = (ctx, answerId) => {
55
const pages = getPages(ctx);
66
const answers = flatMap(pages, (page) => page.answers);
77

8-
return answers.find((answer) => answer.id === answerId);
8+
const resultAnswer = answers.find(
9+
(answer) => answer && answer.id === answerId
10+
);
11+
12+
return resultAnswer;
913
};
1014

1115
module.exports = { getAnswerById };

src/utils/functions/answerGetters.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,40 @@ describe("getAnswerById", () => {
3434
label: "Answer 2",
3535
});
3636
});
37+
38+
it("should return an answer by id when questionnaire contains pages without answers", () => {
39+
const ctx = {
40+
questionnaireJson: {
41+
sections: [
42+
{
43+
id: "section-1",
44+
folders: [
45+
{
46+
id: "folder-1",
47+
pages: [
48+
{
49+
id: "page-1",
50+
answers: [
51+
{
52+
id: "answer-1",
53+
label: "Answer 1",
54+
},
55+
],
56+
},
57+
{
58+
id: "page-2",
59+
},
60+
],
61+
},
62+
],
63+
},
64+
],
65+
},
66+
};
67+
68+
expect(getAnswerById(ctx, "answer-1")).toMatchObject({
69+
id: "answer-1",
70+
label: "Answer 1",
71+
});
72+
});
3773
});

0 commit comments

Comments
 (0)