Skip to content

Commit

Permalink
Merge pull request #3091 from ONSdigital/EAR-2064-importing-double-space
Browse files Browse the repository at this point in the history
EAR 2064 importing double space
  • Loading branch information
farres1 authored Feb 26, 2024
2 parents a0c23bf + 2c3169a commit 0e9be1d
Show file tree
Hide file tree
Showing 19 changed files with 1,668 additions and 726 deletions.
9 changes: 6 additions & 3 deletions eq-author-api/schema/resolvers/importing.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
remapAllNestedIds,
getSectionsByIds,
} = require("./utils");
const removeExtraSpaces = require("../../utils/removeExtraSpaces");

const createFolder = require("../../src/businessLogic/createFolder");

Expand Down Expand Up @@ -45,6 +46,7 @@ module.exports = {
}

pages.forEach((page) => {
removeExtraSpaces(page);
if (page.answers.length === 1) {
if (page.answers[0].repeatingLabelAndInputListId) {
page.answers[0].repeatingLabelAndInputListId = "";
Expand Down Expand Up @@ -121,13 +123,14 @@ module.exports = {
);
}

let sectionsWithoutLogic = [];
const strippedSections = [];

// Re-create UUIDs, strip QCodes, routing and skip conditions from imported pages
// Keep piping intact for now - will show "[Deleted answer]" to users when piped ID not resolvable

sourceSections.forEach((section) => {
remapAllNestedIds(section);
removeExtraSpaces(section);
section.displayConditions = null;
section.questionnaireId = ctx.questionnaire.id;
section.folders.forEach((folder) => {
Expand Down Expand Up @@ -163,7 +166,7 @@ module.exports = {
if (section.repeatingSectionListId) {
section.repeatingSectionListId = "";
}
sectionsWithoutLogic.push(section);
strippedSections.push(section);
});

const section = getSectionById(ctx, sectionId);
Expand All @@ -173,7 +176,7 @@ module.exports = {
);
}

destinationSections.splice(insertionIndex, 0, ...sectionsWithoutLogic);
destinationSections.splice(insertionIndex, 0, ...strippedSections);
ctx.questionnaire.hub = ctx.questionnaire.sections.length > 1;
return destinationSections;
}
Expand Down
36 changes: 36 additions & 0 deletions eq-author-api/utils/removeExtraSpaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const removeExtraSpaces = (inputData) => {
// Does not remove extra spaces from inputData if inputData is null or undefined
if (inputData !== null && inputData !== undefined) {
// If inputData is a string, remove extra spaces
if (typeof inputData === "string") {
// If inputData starts and end with HTML tags, remove extra spaces from the content inside the tags, including leading and trailing spaces
if (/^<[^>]+>.*<\/[^>]+>$/.test(inputData)) {
const startTags = inputData.substring(0, inputData.indexOf(">") + 1);
const endTags = inputData.substring(inputData.lastIndexOf("<"));
const inputDataContent = inputData.substring(
startTags.length,
inputData.lastIndexOf("<")
);
inputData = `${startTags}${inputDataContent
.replace(/\s+/g, " ")
.trim()}${endTags}`;
} else {
inputData = inputData.replace(/\s+/g, " ").trim();
}
}
// If inputData is an array, recursively call removeExtraSpaces to remove extra spaces from each of its items
else if (Array.isArray(inputData)) {
inputData = inputData.map((item) => removeExtraSpaces(item));
}
// If inputData is an object, loop through each of its keys and recursively call removeExtraSpaces to remove extra spaces from each of its values
else if (typeof inputData === "object") {
Object.keys(inputData).forEach((key) => {
inputData[key] = removeExtraSpaces(inputData[key]);
});
}
}

return inputData;
};

module.exports = removeExtraSpaces;
107 changes: 107 additions & 0 deletions eq-author-api/utils/removeExtraSpaces.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const removeExtraSpaces = require("./removeExtraSpaces");

describe("removeExtraSpaces", () => {
it("should remove extra spaces from string values", () => {
const sectionTitleWithExtraSpaces = " Section 1 ";

const sectionTitleWithoutExtraSpaces = removeExtraSpaces(
sectionTitleWithExtraSpaces
);

expect(sectionTitleWithoutExtraSpaces).toEqual("Section 1");
});

it("should remove extra spaces from string values with HTML tags", () => {
const sectionTitleWithExtraSpaces = "<p> Section 1 </p>";

const sectionTitleWithoutExtraSpaces = removeExtraSpaces(
sectionTitleWithExtraSpaces
);

expect(sectionTitleWithoutExtraSpaces).toEqual("<p>Section 1</p>");
});

it("should remove extra spaces from arrays of strings", () => {
const answerLabelsWithExtraSpaces = [
"Answer 1 ",
" Answer 2",
" Answer 3 ",
];

const answerLabelsWithoutExtraSpaces = removeExtraSpaces(
answerLabelsWithExtraSpaces
);

expect(answerLabelsWithoutExtraSpaces).toEqual([
"Answer 1",
"Answer 2",
"Answer 3",
]);
});

it("should remove extra spaces from all string values in object", () => {
const sectionWithExtraSpaces = {
id: "section-with-extra-spaces-1",
title: " Section with extra spaces 1 ",
folders: [
{
id: "folder-with-extra-spaces-1",
alias: " Folder with extra spaces 1",
pages: [
{
id: "page-with-extra-spaces-1",
title: "Page with extra spaces 1 ",
answers: [
{
id: "answer-with-extra-spaces-1",
label: " Answer with extra spaces 1",
},
],
},
],
},
],
};

const sectionWithoutExtraSpaces = removeExtraSpaces(sectionWithExtraSpaces);

expect(sectionWithoutExtraSpaces).toMatchObject({
id: "section-with-extra-spaces-1",
title: "Section with extra spaces 1",
folders: [
{
id: "folder-with-extra-spaces-1",
alias: "Folder with extra spaces 1",
pages: [
{
id: "page-with-extra-spaces-1",
title: "Page with extra spaces 1",
answers: [
{
id: "answer-with-extra-spaces-1",
label: "Answer with extra spaces 1",
},
],
},
],
},
],
});
});

it("should remove extra spaces from arrays of strings in objects", () => {
const page = {
id: "page-1",
title: " Page 1",
answerLabels: ["Answer 1", "Answer 2 ", "Answer 3"], // Mock value to test array of strings in an object - `answerLabels` is not an attribute of `page` in Author
};

const pageWithoutExtraSpaces = removeExtraSpaces(page);

expect(pageWithoutExtraSpaces).toMatchObject({
id: "page-1",
title: "Page 1",
answerLabels: ["Answer 1", "Answer 2", "Answer 3"],
});
});
});
Loading

0 comments on commit 0e9be1d

Please sign in to comment.