Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle unique file list correctly #2328

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
{
Expand Down
30 changes: 22 additions & 8 deletions editor.planx.uk/src/@planx/components/FileUploadAndLabel/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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;
}
};

Expand Down
Loading