From 97d08b6402640f20a7dd3bb5f20502fea0865957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 23 Aug 2024 07:21:45 +0100 Subject: [PATCH] fix: Correct logic for `slotsSchema` validation --- .../FileUploadAndLabel/schema.test.ts | 47 +++++++++++++++++++ .../components/FileUploadAndLabel/schema.ts | 16 +++---- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/editor.planx.uk/src/@planx/components/FileUploadAndLabel/schema.test.ts b/editor.planx.uk/src/@planx/components/FileUploadAndLabel/schema.test.ts index 6c3cc85b0b..990c93788f 100644 --- a/editor.planx.uk/src/@planx/components/FileUploadAndLabel/schema.test.ts +++ b/editor.planx.uk/src/@planx/components/FileUploadAndLabel/schema.test.ts @@ -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" }, @@ -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", () => { diff --git a/editor.planx.uk/src/@planx/components/FileUploadAndLabel/schema.ts b/editor.planx.uk/src/@planx/components/FileUploadAndLabel/schema.ts index 25d82a238b..1b6df3530b 100644 --- a/editor.planx.uk/src/@planx/components/FileUploadAndLabel/schema.ts +++ b/editor.planx.uk/src/@planx/components/FileUploadAndLabel/schema.ts @@ -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({ @@ -101,11 +100,8 @@ export const slotsSchema = array() name: "errorStatus", message: "Remove files which failed to upload", test: (slots?: Array) => { - return Boolean( - slots && - slots.length > 0 && - !slots.some((slot) => slot.status === "error"), - ); + const didAnyUploadFail = slots?.some((slot) => slot.status === "error"); + return !didAnyUploadFail; }, });