diff --git a/editor.planx.uk/src/@planx/components/FileUploadAndLabel/model.test.ts b/editor.planx.uk/src/@planx/components/FileUploadAndLabel/model.test.ts index c9b3f4f5fd..33f74b1189 100644 --- a/editor.planx.uk/src/@planx/components/FileUploadAndLabel/model.test.ts +++ b/editor.planx.uk/src/@planx/components/FileUploadAndLabel/model.test.ts @@ -164,6 +164,78 @@ describe("createFileList function", () => { expect(result).toEqual(expected); }); + it("correctly handles unique file names", () => { + const fileTypes: FileType[] = [ + { + fn: "files.documentA", + name: "Document A", + rule: { + fn: "documentA.required", + val: "true", + operator: Operator.Equals, + condition: Condition.RequiredIf, + }, + }, + { + fn: "files.documentB", + name: "Document B", + rule: { + fn: "documentB.recommended", + val: "true", + operator: Operator.Equals, + condition: Condition.RecommendedIf, + }, + }, + { + fn: "files.documentB", + name: "Document B", + rule: { + fn: "documentB.required", + val: "true", + operator: Operator.Equals, + condition: Condition.RequiredIf, + }, + }, + ]; + const passport: Store.passport = { + data: { + "documentA.required": ["true"], + "documentB.recommended": ["true"], + }, + }; + + const expected: FileList = { + required: [ + { + fn: "files.documentA", + name: "Document A", + rule: { + fn: "documentA.required", + val: "true", + operator: Operator.Equals, + condition: Condition.RequiredIf, + }, + }, + ], + recommended: [ + { + fn: "files.documentB", + name: "Document B", + rule: { + fn: "documentB.recommended", + val: "true", + operator: Operator.Equals, + condition: Condition.RecommendedIf, + }, + }, + ], + optional: [], + }; + const result = createFileList({ passport, fileTypes }); + + expect(result).toEqual(expected); + }); + it("handles a complex list of FileTypes", () => { const fileTypes: FileType[] = [ { diff --git a/editor.planx.uk/src/@planx/components/FileUploadAndLabel/model.ts b/editor.planx.uk/src/@planx/components/FileUploadAndLabel/model.ts index 3627028b2e..5b24dea4e9 100644 --- a/editor.planx.uk/src/@planx/components/FileUploadAndLabel/model.ts +++ b/editor.planx.uk/src/@planx/components/FileUploadAndLabel/model.ts @@ -125,8 +125,12 @@ export const createFileList = ({ sortedFileTypes.forEach((fileType) => { const isUnique = !uniqueNames.includes(fileType.name); if (isUnique) { - uniqueNames.push(fileType.name); - populateFileList({ fileList, fileType, passport }); + const isFileTypeAdded = populateFileList({ + fileList, + fileType, + passport, + }); + if (isFileTypeAdded) uniqueNames.push(fileType.name); } }); return fileList; @@ -139,6 +143,10 @@ const sortFileTypes = (fileTypes: FileType[]): FileType[] => { return sortedFileTypes; }; +/** + * Populate file list based on condition + * @returns true if file added, false if not + */ const populateFileList = ({ fileList, fileType, @@ -147,27 +155,33 @@ const populateFileList = ({ fileList: FileList; fileType: FileType; passport: Store.passport; -}) => { +}): boolean => { switch (fileType.rule.condition) { case Condition.AlwaysRequired: fileList.required.push(fileType); - break; + return true; + case Condition.AlwaysRecommended: fileList.recommended.push(fileType); - break; + return true; + case Condition.RequiredIf: if (isRuleMet(passport, fileType.rule)) { fileList.required.push(fileType); + return true; } - break; + return false; + case Condition.RecommendedIf: if (isRuleMet(passport, fileType.rule)) { fileList.recommended.push(fileType); + return true; } - break; + return false; + case Condition.NotRequired: fileList.optional.push(fileType); - break; + return true; } };