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: Correct logic for slotsSchema validation #3550

Merged
merged 1 commit into from
Aug 23, 2024
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 @@ -185,6 +185,19 @@ describe("slotSchema", () => {
expect(result).toBe(false);
});

it("rejects slots which failed to upload", async () => {
const mockSlots = [
{ status: "error" },
{ status: "success" },
] as FileUploadSlot[];

const result = await slotsSchema.isValid(mockSlots, {
context: { fileList: mockFileList },
});

expect(result).toBe(false);
});

it("allows slots with all files uploaded", async () => {
const mockSlots = [
{ status: "success" },
Expand All @@ -197,6 +210,40 @@ describe("slotSchema", () => {

expect(result).toBe(true);
});

it("allows users to proceed if there are no required files", async () => {
const mockSlots: FileUploadSlot[] = [];

const result = await slotsSchema.isValid(mockSlots, {
context: {
fileList: {
required: [],
recommended: [
{ ...mockFileTypes.AlwaysRecommended, slots: [{ id: "123" }] },
],
optional: [{ ...mockFileTypes.NotRequired, slots: [{ id: "456" }] }],
},
},
});

expect(result).toBe(true);
});

it("allows users to proceed if there are no required files, and they have not uploaded any optional or recommended files", async () => {
const mockSlots: FileUploadSlot[] = [];

const result = await slotsSchema.isValid(mockSlots, {
context: {
fileList: {
required: [],
recommended: [{ ...mockFileTypes.AlwaysRecommended }],
optional: [{ ...mockFileTypes.NotRequired }],
},
},
});

expect(result).toBe(true);
});
});

describe("fileLabelSchema", () => {
Expand Down
16 changes: 6 additions & 10 deletions editor.planx.uk/src/@planx/components/FileUploadAndLabel/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ export const slotsSchema = array()
if (!slots) throw new Error("Missing slots for slotsSchema");

const { fileList } = context as SlotsSchemaTestContext;
const allFilesOptional =
!fileList.recommended.length && !fileList.required.length;
const isMinFileUploadCountSatisfied =
allFilesOptional || slots.length > 0;
const noFilesAreRequired = Boolean(!fileList.required.length);
if (noFilesAreRequired) return true;

return isMinFileUploadCountSatisfied;
const isAtLeastOneFileUploaded = slots.length > 0;
return isAtLeastOneFileUploaded;
},
})
.test({
Expand All @@ -101,11 +100,8 @@ export const slotsSchema = array()
name: "errorStatus",
message: "Remove files which failed to upload",
test: (slots?: Array<FileUploadSlot>) => {
return Boolean(
slots &&
slots.length > 0 &&
!slots.some((slot) => slot.status === "error"),
);
const didAnyUploadFail = slots?.some((slot) => slot.status === "error");
return !didAnyUploadFail;
},
});

Expand Down
Loading