Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EAR-2439 mutually exclusive logic not applied when mutually exclusive answer has only one option #219

Merged
merged 12 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/eq_schema/builders/basicQuestionnaireJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@ const questionnaireJson = {
},
],
},
{
id: "3",
title: "<p>Section 3</p>",
folders: [
{
id: "folder-3",
enabled: false,
pages: [
{
id: "4",
title: "<p>Page 4</p>",
pageType: "QuestionPage",
routingRuleSet: null,
confirmation: null,
answers: [
{
id: "5",
type: "Number",
label: "Answer 5",
},
{
id: "6",
type: "MutuallyExclusive",
options: [
{
id: "exclusive-option-1",
label: "Not known",
},
],
},
],
},
],
},
],
},
],
};

Expand Down
33 changes: 24 additions & 9 deletions src/eq_schema/builders/routing2/newRoutingDestination/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { getValueSource } = require("../../valueSource");
const { getListFromAll } = require("../../../../utils/functions/listGetters");

const { flatMap, filter, find } = require("lodash");
const { getAnswerById } = require("../../../../utils/functions/answerGetters");

const authorConditions = {
UNANSWERED: "Unanswered",
Expand Down Expand Up @@ -122,16 +123,30 @@ const buildAnswerObject = (
return SelectedOptions;
}

if (condition === "OneOf") {
const swapOptionValues = ([optionValues[0], optionValues[1]] = [
optionValues[1],
optionValues[0],
]);
const SelectedOptions = {
[routingConditionConversion(condition)]: swapOptionValues,
};
const leftSideAnswer = getAnswerById(ctx, left.answerId);

return SelectedOptions;
if (condition === "OneOf") {
if (
leftSideAnswer &&
leftSideAnswer.type === "MutuallyExclusive" &&
leftSideAnswer.options.length === 1
) {
const SelectedOptions = {
[routingConditionConversion("AllOf")]: optionValues,
};

return SelectedOptions;
} else {
const swapOptionValues = ([optionValues[0], optionValues[1]] = [
optionValues[1],
optionValues[0],
]);
const SelectedOptions = {
[routingConditionConversion(condition)]: swapOptionValues,
};

return SelectedOptions;
}
}

const SelectedOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,36 @@ describe("Should build a runner representation of a binary expression", () => {
});
});

describe("With mutually exclusive answers", () => {
it("should return all of condition for mutually exclusive answers with one option", () => {
const expression = {
left: {
answerId: "6",
type: "Answer",
},
condition: "OneOf",
right: {
type: "SelectedOptions",
optionIds: ["exclusive-option-1"],
},
};

const runnerExpression = checkValidRoutingType(expression, {
questionnaireJson,
});

expect(runnerExpression).toMatchObject({
"all-in": [
["Not known"],
{
identifier: "answer6",
source: "answers",
},
],
});
});
});

describe("With metadata", () => {
describe("Text metadata", () => {
it("should return correct metadata object for text metadata", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("Translation of a routing destination", () => {
logical: "NextPage",
};
expect(
translateRoutingDestination(authorDestination, "3", { questionnaireJson })
translateRoutingDestination(authorDestination, "4", { questionnaireJson })
).toMatchObject({ group: "confirmation-group" });
});

Expand All @@ -66,7 +66,7 @@ describe("Translation of a routing destination", () => {
logical: "NextPage",
};
expect(
translateRoutingDestination(authorDestination, "3", {
translateRoutingDestination(authorDestination, "4", {
questionnaireJson: questionnaireJsonWithSummary,
})
).toMatchObject({ group: "summary-group" });
Expand Down
11 changes: 11 additions & 0 deletions src/utils/functions/answerGetters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { getPages } = require("./pageGetters");
const { flatMap } = require("lodash");

const getAnswerById = (ctx, answerId) => {
const pages = getPages(ctx);
const answers = flatMap(pages, (page) => page.answers);

return answers.find((answer) => answer.id === answerId);
};

module.exports = { getAnswerById };
37 changes: 37 additions & 0 deletions src/utils/functions/answerGetters.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { getAnswerById } = require("./answerGetters");

describe("getAnswerById", () => {
it("should return an answer by id", () => {
const ctx = {
questionnaireJson: {
sections: [
{
folders: [
{
pages: [
{
answers: [
{
id: "1",
label: "Answer 1",
},
{
id: "2",
label: "Answer 2",
},
],
},
],
},
],
},
],
},
};

expect(getAnswerById(ctx, "2")).toMatchObject({
id: "2",
label: "Answer 2",
});
});
});
Loading