Skip to content

Commit

Permalink
fix: Correct logic for slotsSchema validation (#3550)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Aug 23, 2024
1 parent 66241e4 commit daae386
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
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

0 comments on commit daae386

Please sign in to comment.