diff --git a/src/utils/convertPipes/index.js b/src/utils/convertPipes/index.js index bcca931a..d5a77fcc 100644 --- a/src/utils/convertPipes/index.js +++ b/src/utils/convertPipes/index.js @@ -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"); @@ -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: { @@ -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); diff --git a/src/utils/convertPipes/index.test.js b/src/utils/convertPipes/index.test.js index a36672e1..224a54d1 100644 --- a/src/utils/convertPipes/index.test.js +++ b/src/utils/convertPipes/index.test.js @@ -32,7 +32,7 @@ const createTransformation = ( ], }); -const createCheckboxTransformation = ({ placeholder, transform }, extra) => ({ +const createAlternateTransformation = ({ placeholder, transform }, extra) => ({ placeholder, transforms: [ { @@ -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?", @@ -89,6 +94,18 @@ const createContext = (metadata = []) => ({ }, ], }, + { + id: `8`, + type: "Radio", + label: "Q8 Radio Options", + options: [ + { + id: `FavouriteFruit`, + dynamicAnswer: true, + dynamicAnswerID: `7`, + }, + ], + }, ], }, { @@ -168,7 +185,7 @@ describe("convertPipes", () => { }), createTransformation({ placeholder: "untitled_answer", - identifier: "answer2", + identifier: "answer2", source: "answers", argument: "number", transform: "format_currency", @@ -277,7 +294,7 @@ describe("convertPipes", () => { expect(convertPipes(createContext())(html)).toEqual( createWrapper( "{Q7_Checkbox_Options}", - createCheckboxTransformation( + createAlternateTransformation( { placeholder: "Q7_Checkbox_Options", transform: "concatenate_list", @@ -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" });