From 9f591f35416afe05995f37338545f95a0af0a4c0 Mon Sep 17 00:00:00 2001 From: Jose Manuel Serrano Amaut <a20122128@pucp.pe> Date: Tue, 23 Jan 2024 07:30:03 -0500 Subject: [PATCH] [FIX]: Fix url validation for uploading and autouploading --- package.json | 2 +- src/Dropzone/components/dropzone/Dropzone.tsx | 2 +- src/FileInputButton/FileInputButton.tsx | 2 +- src/index.ts | 9 ++--- src/utils/url.utils.ts | 2 +- tests/Dropzone.test.tsx | 34 +++++++++++++++++++ 6 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 tests/Dropzone.test.tsx diff --git a/package.json b/package.json index 076d8d3..4547d2b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@files-ui/react", - "version": "1.2.1", + "version": "1.2.2", "description": "UI components for file uploads with React js", "main": "./build/index.js", "module": "./build/index.es.js", diff --git a/src/Dropzone/components/dropzone/Dropzone.tsx b/src/Dropzone/components/dropzone/Dropzone.tsx index a130948..15a7b19 100644 --- a/src/Dropzone/components/dropzone/Dropzone.tsx +++ b/src/Dropzone/components/dropzone/Dropzone.tsx @@ -269,7 +269,7 @@ const Dropzone: React.FC<DropzoneProps> = (props: DropzoneProps) => { // flag is already true or there isnt files //url was not provided - if (isUploading || localFiles.length === 0 || !url) { + if (isUploading || localFiles.length === 0 || !shouldUpload) { setIsUploading(false); return; } diff --git a/src/FileInputButton/FileInputButton.tsx b/src/FileInputButton/FileInputButton.tsx index 07ddc27..3806434 100644 --- a/src/FileInputButton/FileInputButton.tsx +++ b/src/FileInputButton/FileInputButton.tsx @@ -207,7 +207,7 @@ const FileInputButton: React.FC<FileInputButtonProps> = ( // flag is already true or there isnt files //url was not provided - if (isUploading || localFiles.length === 0 || !url) { + if (isUploading || localFiles.length === 0 || !shouldUpload) { setIsUploading(false); return; } diff --git a/src/index.ts b/src/index.ts index fcccdfa..746ee0e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -58,10 +58,9 @@ export { JsonParseResponse, NAMED_COLORS, NO_XHR_PROVIDED_ERROR, - NamedColor, SyntheticFile, + SyntheticFile, TIMEOUT_ERROR_RESPONSE, UNEXPECTED_ERROR_RESPONSE, - UploadPromiseResponse, ValidateErrorEnglish, ValidateErrorFrench, ValidateErrorItalian, @@ -128,7 +127,7 @@ export { sleepTransition, unableToUploadResult, unexpectedErrorUploadResult, -} from "@files-ui/core" +} from "@files-ui/core"; export type { ExtFile, @@ -145,7 +144,9 @@ export type { FileValidatorProps, FunctionLabel, LocalLabels, - Method + Method, + NamedColor, + UploadPromiseResponse } from "@files-ui/core" diff --git a/src/utils/url.utils.ts b/src/utils/url.utils.ts index 17877c2..ce9dcba 100644 --- a/src/utils/url.utils.ts +++ b/src/utils/url.utils.ts @@ -5,5 +5,5 @@ export const isThereValidUrl = ( urlFunction?: Function, extFileList?: ExtFile[] ): boolean => { - return ExtFileInstance.someValidUrl(extFileList) && url && url.length && urlFunction != undefined; + return ExtFileInstance.someValidUrl(extFileList || []) || urlFunction != undefined || (url != undefined && url.length > 0); } \ No newline at end of file diff --git a/tests/Dropzone.test.tsx b/tests/Dropzone.test.tsx new file mode 100644 index 0000000..e0e2143 --- /dev/null +++ b/tests/Dropzone.test.tsx @@ -0,0 +1,34 @@ +import "@testing-library/jest-dom"; +import React from "react"; +import { Dropzone } from "../src"; + +import { cleanup, fireEvent, render, screen } from "@testing-library/react"; + +test("Validate label text must be 'Drop yor files here...'", () => { + render(<Dropzone> Drop yor files here...</Dropzone>); + expect(screen.getByText("Drop yor files here...")).toBeInTheDocument(); +}); + +describe("Dropzone actionButtons", () => { + test.each([ + [{ uploadButton: { onClick: console.log } }, false], + [{ uploadButton: { onClick: console.log, disabled: false } }, false], + [{ uploadButton: { onClick: console.log, disabled: true } }, true], + [{ deleteButton: { onClick: console.log } }, false], + [{ deleteButton: { onClick: console.log, disabled: false } }, false], + [{ deleteButton: { onClick: console.log, disabled: true } }, true], + + // abortButton and cleanButton need more interaction + ])("disabled %s -> %s", (config, expected) => { + const { container } = render( + <Dropzone actionButtons={{ position: "after", ...config }} />, + ); + expect( + ( + container.querySelector( + ".files-ui-buttons-container button", + ) as HTMLInputElement + ).disabled, + ).toBe(expected); + }); +}); -- GitLab