@planning-inspectorate/dynamic-forms 3.3.1 → 3.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/components/manage-list/question.js +5 -2
- package/src/components/manage-list/question.test.js +2 -0
- package/src/components/unit-option-entry/question.js +3 -0
- package/src/controller.js +7 -0
- package/src/controller.test.js +7 -1
- package/src/questions/options-question.js +3 -0
- package/src/components/utils/nunjucks.js +0 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Question } from '#question';
|
|
2
2
|
import { Uuid } from '#src/lib/uuid.js';
|
|
3
|
-
import
|
|
3
|
+
import nunjucks from 'nunjucks';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @typedef {Object} ManageListQuestionParameters
|
|
@@ -107,7 +107,10 @@ export default class ManageListQuestion extends Question {
|
|
|
107
107
|
if (answer && Array.isArray(answer)) {
|
|
108
108
|
if (this.#showAnswersInSummary) {
|
|
109
109
|
const answers = answer.map((a) => this.#formatItemAnswers(a));
|
|
110
|
-
|
|
110
|
+
// note: nunjucks.render uses the last configured environment
|
|
111
|
+
// so we assume here that it is the one used by the main application and
|
|
112
|
+
// is configured for dynamic-forms and govuk components
|
|
113
|
+
formattedAnswer = nunjucks.render('components/manage-list/answer-summary-list.njk', { answers });
|
|
111
114
|
} else if (answer.length > 0) {
|
|
112
115
|
formattedAnswer = `${answer.length} ${this.title}`;
|
|
113
116
|
}
|
|
@@ -91,6 +91,8 @@ describe('components/manage-list/question', () => {
|
|
|
91
91
|
});
|
|
92
92
|
|
|
93
93
|
it('should support showing all answers in the format list answer for summary', (ctx) => {
|
|
94
|
+
// nunjucks.render uses the last environment configured, so make sure one is configured
|
|
95
|
+
configureNunjucksTestEnv();
|
|
94
96
|
const { q } = questionWithManageQuestions(ctx, { showAnswersInSummary: true });
|
|
95
97
|
const answerForSummary = q.formatAnswerForSummary('section-1', mockJourney(), [{}, {}, {}]);
|
|
96
98
|
assert.strictEqual(answerForSummary.length, 1);
|
|
@@ -112,6 +112,9 @@ export default class UnitOptionEntryQuestion extends Question {
|
|
|
112
112
|
conditionalField.value = payload ? payload[conditionalField.fieldName] : existingValue;
|
|
113
113
|
|
|
114
114
|
optionData.conditional = {
|
|
115
|
+
// note: nunjucks.render uses the last configured environment
|
|
116
|
+
// so we assume here that it is the one used by the main application and
|
|
117
|
+
// is configured for dynamic-forms and govuk components
|
|
115
118
|
html: nunjucks.render(`./components/conditional/unit.njk`, {
|
|
116
119
|
payload,
|
|
117
120
|
...conditionalField,
|
package/src/controller.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { SECTION_STATUS } from './section.js';
|
|
2
2
|
import questionUtils from './components/utils/question-utils.js';
|
|
3
3
|
import { answerObjectForManageListSaving } from '#src/components/manage-list/utils.js';
|
|
4
|
+
import { booleanToYesNoValue } from '#src/components/boolean/question.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* @typedef {import('./journey/journey.js').Journey} Journey
|
|
@@ -258,6 +259,12 @@ export function buildSave(saveData, redirectToTaskListOnSuccess) {
|
|
|
258
259
|
answers = answerObjectForManageListSaving(journeyResponse, manageListQuestion, req.params);
|
|
259
260
|
}
|
|
260
261
|
for (const [k, v] of Object.entries(data?.answers || {})) {
|
|
262
|
+
if (typeof v === 'boolean') {
|
|
263
|
+
// boolean answers are saved at booleans, but stored in session and
|
|
264
|
+
// checked by display logic as yes/no
|
|
265
|
+
answers[k] = booleanToYesNoValue(v);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
261
268
|
answers[k] = v;
|
|
262
269
|
}
|
|
263
270
|
// move to the next question
|
package/src/controller.test.js
CHANGED
|
@@ -6,6 +6,7 @@ import { Journey } from './journey/journey.js';
|
|
|
6
6
|
import { SECTION_STATUS } from './section.js';
|
|
7
7
|
|
|
8
8
|
import { mockReq, mockRes } from '#test/utils/utils.js';
|
|
9
|
+
import { BOOLEAN_OPTIONS } from '#src/components/boolean/question.js';
|
|
9
10
|
|
|
10
11
|
const res = mockRes();
|
|
11
12
|
const mockBaseUrl = '/manage-appeals/questionnaire';
|
|
@@ -501,7 +502,10 @@ describe('dynamic-form/controller', () => {
|
|
|
501
502
|
...sampleQuestionObj
|
|
502
503
|
};
|
|
503
504
|
sampleQuestionObjWithActions.getDataToSave.mock.mockImplementationOnce(() => ({
|
|
504
|
-
answers: {
|
|
505
|
+
answers: {
|
|
506
|
+
[sampleQuestionObj.fieldName]: 'my-answer',
|
|
507
|
+
aBooleanToTest: true
|
|
508
|
+
}
|
|
505
509
|
}));
|
|
506
510
|
|
|
507
511
|
req.params = {
|
|
@@ -528,6 +532,8 @@ describe('dynamic-form/controller', () => {
|
|
|
528
532
|
assert.strictEqual(res.redirect.mock.calls[0].arguments[0], expectedUrl);
|
|
529
533
|
// journey response should be edited if redirecting to next question
|
|
530
534
|
assert.strictEqual(res.locals.journeyResponse.answers?.sampleFieldName, 'my-answer');
|
|
535
|
+
// booleans must be strings in journey response
|
|
536
|
+
assert.strictEqual(res.locals.journeyResponse.answers?.aBooleanToTest, BOOLEAN_OPTIONS.YES);
|
|
531
537
|
});
|
|
532
538
|
|
|
533
539
|
it('should redirect to task list if configured', async () => {
|
|
@@ -100,6 +100,9 @@ export default class OptionsQuestion extends Question {
|
|
|
100
100
|
conditionalField.value = answers[conditionalField.fieldName] || '';
|
|
101
101
|
|
|
102
102
|
optionData.conditional = {
|
|
103
|
+
// note: nunjucks.render uses the last configured environment
|
|
104
|
+
// so we assume here that it is the one used by the main application and
|
|
105
|
+
// is configured for dynamic-forms and govuk components
|
|
103
106
|
html: nunjucks.render(`./components/conditional/${conditionalField.type}.njk`, {
|
|
104
107
|
payload,
|
|
105
108
|
...conditionalField,
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import nunjucks from 'nunjucks';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Returns a nunjucks environment configured with the `src` folder
|
|
6
|
-
* @returns {import('nunjucks').Environment}
|
|
7
|
-
*/
|
|
8
|
-
export function nunjucksEnv() {
|
|
9
|
-
const utilsDir = path.dirname(new URL(import.meta.url).pathname);
|
|
10
|
-
const srcDir = path.resolve(utilsDir, '..', '..');
|
|
11
|
-
|
|
12
|
-
return nunjucks.configure([srcDir], {
|
|
13
|
-
// output with dangerous characters are escaped automatically
|
|
14
|
-
autoescape: true,
|
|
15
|
-
// automatically remove trailing newlines from a block/tag
|
|
16
|
-
trimBlocks: true,
|
|
17
|
-
// automatically remove leading whitespace from a block/tag
|
|
18
|
-
lstripBlocks: true
|
|
19
|
-
});
|
|
20
|
-
}
|