Skip to content

Commit

Permalink
Pull in latest changes from eq-publisher.
Browse files Browse the repository at this point in the history
  • Loading branch information
samiwel committed Nov 16, 2018
1 parent 5b67988 commit 59c87b4
Show file tree
Hide file tree
Showing 11 changed files with 955 additions and 697 deletions.
66 changes: 66 additions & 0 deletions eq-publisher/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

# Created by https://www.gitignore.io/api/node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# End of https://www.gitignore.io/api/node

.vscode
12 changes: 12 additions & 0 deletions eq-publisher/src/api/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ exports.getQuestionnaire = `
}
}
}
confirmation {
id
title
positive {
label
description
}
negative {
label
description
}
}
answers {
...answerFragment
... on MultipleChoiceAnswer {
Expand Down
24 changes: 20 additions & 4 deletions eq-publisher/src/eq_schema/Block.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const Question = require("./Question");
const RoutingRule = require("./RoutingRule");
const RoutingDestination = require("./RoutingDestination");
const { getInnerHTMLWithPiping } = require("../utils/HTMLUtils");
const convertPipes = require("../utils/convertPipes");
const { get, isNil, remove, isEmpty } = require("lodash");
const { flow, getOr, last, map, some } = require("lodash/fp");

Expand All @@ -14,6 +16,12 @@ const getLastPage = flow(
last
);

const processPipedText = ctx =>
flow(
convertPipes(ctx),
getInnerHTMLWithPiping
);

const isLastPageInSection = (page, ctx) =>
flow(
getOr([], "sections"),
Expand All @@ -27,7 +35,11 @@ class Block {
this.type = this.convertPageType(page.pageType);
this.questions = this.buildQuestions(page, ctx);

if (!isLastPageInSection(page, ctx) && !isNil(page.routingRuleSet)) {
if (
!isLastPageInSection(page, ctx) &&
!isNil(page.routingRuleSet) &&
isNil(page.confirmation)
) {
// eslint-disable-next-line camelcase
this.routing_rules = this.buildRoutingRules(
page.routingRuleSet,
Expand All @@ -38,12 +50,16 @@ class Block {
}
}

static buildIntroBlock({ introductionTitle, introductionContent }, groupId) {
static buildIntroBlock(
{ introductionTitle, introductionContent },
groupId,
ctx
) {
return {
type: "Interstitial",
id: `group${groupId}-introduction`,
title: introductionTitle || "",
description: introductionContent || ""
title: processPipedText(ctx)(introductionTitle) || "",
description: processPipedText(ctx)(introductionContent) || ""
};
}

Expand Down
65 changes: 65 additions & 0 deletions eq-publisher/src/eq_schema/Block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ describe("Block", () => {
expect(block.title).toBeUndefined();
});

it("should not build routing rules when there is a confirmation page", () => {
const block = new Block(
createBlockJSON({
confirmation: {
id: "2",
title: "<p>Are you sure?</p>"
},
routingRuleSet: { id: "2" }
}),
ctx
);

expect(block.routingRuleSet).toBeUndefined();
});

describe("conversion of page types", () => {
it("should convert QuestionPage to Questionnaire", () => {
const block = new Block(
Expand Down Expand Up @@ -72,4 +87,54 @@ describe("Block", () => {
expect(isLastPageInSection({ id: "3" }, questionnaire)).toBe(false);
});
});

describe("piping", () => {
const createPipe = ({
id = 123,
type = "TextField",
text = "foo",
pipeType = "answers"
} = {}) =>
`<span data-piped="${pipeType}" data-id="${id}" data-type="${type}">${text}</span>`;

const createContext = (
metadata = [{ id: "123", type: "Text", key: "my_metadata" }]
) => ({
questionnaireJson: {
metadata
}
});

it("should handle piped values in title", () => {
// noinspection JSAnnotator
let introduction = {
introductionTitle: createPipe(),
introductionContent: ""
};

const introBlock = Block.buildIntroBlock(
introduction,
0,
createContext()
);

expect(introBlock.title).toEqual("{{ answers['answer123'] }}");
});

it("should handle piped values in description", () => {
// noinspection JSAnnotator
let introduction = {
introductionTitle: "",
introductionContent: createPipe()
};

const introBlock = Block.buildIntroBlock(
introduction,
0,
createContext()
);

expect(introBlock.description).toEqual("{{ answers['answer123'] }}");
});
});
});
26 changes: 23 additions & 3 deletions eq-publisher/src/eq_schema/Group.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable camelcase */
const Block = require("./Block");
const { getInnerHTML } = require("../utils/HTMLUtils");
const { isEmpty, reject } = require("lodash");
const { isEmpty, reject, flatten } = require("lodash");
const {
buildAuthorConfirmationQuestion
} = require("./builders/confirmationPage/ConfirmationPage");

class Group {
constructor(id, title, pages, introduction, ctx) {
Expand Down Expand Up @@ -35,9 +38,26 @@ class Group {
}

buildBlocks(pages, groupId, introduction, ctx) {
const blocks = pages.map(page => new Block(page, groupId, ctx));
const blocks = flatten(
pages.map(page => {
const block = new Block(page, groupId, ctx);
if (page.confirmation) {
return [
block,
buildAuthorConfirmationQuestion(
page,
groupId,
page.routingRuleSet,
ctx
)
];
}
return block;
})
);

if (introduction.introductionEnabled) {
return [Block.buildIntroBlock(introduction, groupId), ...blocks];
return [Block.buildIntroBlock(introduction, groupId, ctx), ...blocks];
}
return blocks;
}
Expand Down
Loading

0 comments on commit 59c87b4

Please sign in to comment.