Skip to content

Commit

Permalink
Merge pull request #115 from ONSdigital/EAR-1788-piping-radio-answer-…
Browse files Browse the repository at this point in the history
…dynamic-options

Ear 1788 piping radio answer dynamic options
  • Loading branch information
sudeepkunhis authored Sep 28, 2022
2 parents c22869d + 662a71c commit 9ab88ad
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
38 changes: 25 additions & 13 deletions src/utils/convertPipes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cheerio = require("cheerio");
const { flatMap, includes, compact } = require("lodash");
const { flatMap, includes, compact, find } = require("lodash");
const { unescapePiping, removeDash } = require("../HTMLUtils");
const { placeholderObjectBuilder } = require("./PlaceholderObjectBuilder");

Expand Down Expand Up @@ -63,19 +63,31 @@ const PIPE_TYPES = {
}
},
getType: ({ type }) => type,
getFallback: ({ properties, id, type, advancedProperties }) => {
if (!(type === "DateRange") || !advancedProperties) {
return null;
getFallback: ({ properties, id, type, options, advancedProperties }) => {
if (type === "Radio" && options) {
const dynamicOption = find(options, { dynamicAnswer: true });
if (dynamicOption && dynamicOption.dynamicAnswerID) {
return {
source: "answers",
identifier: `answer${dynamicOption.dynamicAnswerID}`,
};
}
}
if (!properties || !properties.fallback || !properties.fallback.enabled) {
return null;
if (
type === "DateRange" &&
advancedProperties &&
properties &&
properties.fallback &&
properties.fallback.enabled
) {
return {
source: "metadata",
identifier: id.endsWith("from")
? properties.fallback.start
: properties.fallback.end,
};
}
return {
source: "metadata",
identifier: id.endsWith("from")
? properties.fallback.start
: properties.fallback.end,
};
return null;
},
},
metadata: {
Expand Down Expand Up @@ -130,7 +142,7 @@ const getPipedData = (store) => (element, ctx) => {
const identifier =
elementData.type === "DateRange"
? pipeConfig.render(elementData)
: pipeConfig.render(entity);
: pipeConfig.render(entity);

const answerType = pipeConfig.getType(entity);

Expand Down
48 changes: 43 additions & 5 deletions src/utils/convertPipes/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const createTransformation = (
],
});

const createCheckboxTransformation = ({ placeholder, transform }, extra) => ({
const createAlternateTransformation = ({ placeholder, transform }, extra) => ({
placeholder,
transforms: [
{
Expand Down Expand Up @@ -64,12 +64,17 @@ const createContext = (metadata = []) => ({
{ id: `3`, label: "!It's Q3?", type: "DateRange" },
{
id: `4`,
label: "#Q4Don'tDoIt",
label: "#Q4Don'tDoIt",
type: "Date",
properties: { format: "dd/mm/yyyy" },
},
{ id: `5`, label: "label of excellence", type: "Number" },
{ id: `6`, label: "BACWards 6q", type: "Unit", properties: { unit: "Kilometres" } },
{
id: `6`,
label: "BACWards 6q",
type: "Unit",
properties: { unit: "Kilometres" },
},
{
id: `7`,
label: "Q7 Checkbox Options?",
Expand All @@ -89,6 +94,18 @@ const createContext = (metadata = []) => ({
},
],
},
{
id: `8`,
type: "Radio",
label: "Q8 Radio Options",
options: [
{
id: `FavouriteFruit`,
dynamicAnswer: true,
dynamicAnswerID: `7`,
},
],
},
],
},
{
Expand Down Expand Up @@ -168,7 +185,7 @@ describe("convertPipes", () => {
}),
createTransformation({
placeholder: "untitled_answer",
identifier: "answer2",
identifier: "answer2",
source: "answers",
argument: "number",
transform: "format_currency",
Expand Down Expand Up @@ -277,7 +294,7 @@ describe("convertPipes", () => {
expect(convertPipes(createContext())(html)).toEqual(
createWrapper(
"{Q7_Checkbox_Options}",
createCheckboxTransformation(
createAlternateTransformation(
{
placeholder: "Q7_Checkbox_Options",
transform: "concatenate_list",
Expand All @@ -294,6 +311,27 @@ describe("convertPipes", () => {
);
});

it("should pipe dynamic radio answers", () => {
const html = createPipe({ id: "8" });
expect(convertPipes(createContext())(html)).toEqual(
createWrapper(
"{Q8_Radio_Options}",
createAlternateTransformation(
{
placeholder: "Q8_Radio_Options",
transform: "first_non_empty_item",
},
{
items: [
{ source: "answers", identifier: "answer8" },
{ source: "answers", identifier: "answer7" },
],
}
)
)
);
});

// Put in when Unit in runner
// it("should format Unit answers with `unit`", () => {
// const html = createPipe({ id: "6" });
Expand Down

0 comments on commit 9ab88ad

Please sign in to comment.