Enable file upload form
Enable error boundary to catch React errors (#7) Fix warning for link noreferrer Include authorization header with file upload
This commit is contained in:
@@ -24,10 +24,10 @@ export const FileUploadProvider = ({ children }) => {
|
||||
done();
|
||||
};
|
||||
|
||||
const upload = async (files) => {
|
||||
const upload = async (files, accessToken) => {
|
||||
try {
|
||||
if (!files || files.length === 0) throw new Error("No file provided");
|
||||
|
||||
if (!files || files.length === 0) throw new Error("File required");
|
||||
if (!accessToken || accessToken.length === 0) throw new Error("Access token required")
|
||||
const file = files[0].file;
|
||||
const filename = file.name;
|
||||
|
||||
@@ -37,7 +37,7 @@ export const FileUploadProvider = ({ children }) => {
|
||||
setStatus(`Uploading ${filename}`);
|
||||
setCancelUpload(getCancelToken());
|
||||
|
||||
const { data } = await uploadFile(file, setProgress, cancelUpload);
|
||||
const { data } = await uploadFile(file, accessToken, setProgress, cancelUpload);
|
||||
if (data.message) throw new Error(`${data.error}. ${data.message}`);
|
||||
const url = ((data && data.link) ? data.link : "No URL available");
|
||||
setLinkURL(url);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
jest.mock("../../services/uploadFile");
|
||||
|
||||
import {uploadFile, getCancelToken, setUploadFileResponse, setUploadFileDelay, getIssuedCancelToken } from "../../services/uploadFile"
|
||||
import { setUploadFileDelay } from "../../services/uploadFile"
|
||||
import { FileUploadProvider, useFileUploadContext } from "../Contexts/FileUploadContext";
|
||||
import {render, cleanup, screen, fireEvent, waitFor} from "@testing-library/react"
|
||||
|
||||
@@ -9,6 +9,8 @@ describe("FileUploadContext", () => {
|
||||
beforeEach(() => {
|
||||
const TestComp = () => {
|
||||
const { progress, uploading, status, linkURL, upload, cancel } = useFileUploadContext();
|
||||
const TEST_FILE = [{ file: { name: "test.jpg" }}];
|
||||
const TEST_ACCESSTOKEN = "ACCESSTOKEN";
|
||||
return (
|
||||
<>
|
||||
<div data-testid="uploading">{uploading.toString()}</div>
|
||||
@@ -16,7 +18,8 @@ describe("FileUploadContext", () => {
|
||||
<div data-testid="status">{status}</div>
|
||||
<div data-testid="linkURL">{linkURL}</div>
|
||||
<button data-testid="uploadNoFile" onClick={() => upload()}/>
|
||||
<button data-testid="upload" onClick={() => upload([{ file: { name: "test.jpg" }}])}/>
|
||||
<button data-testid="uploadNoToken" onClick={() => upload(TEST_FILE)}/>
|
||||
<button data-testid="upload" onClick={() => upload(TEST_FILE, TEST_ACCESSTOKEN)}/>
|
||||
<button data-testid="cancel" onClick={() => cancel()}/>
|
||||
</>
|
||||
);
|
||||
@@ -39,7 +42,15 @@ describe("FileUploadContext", () => {
|
||||
fireEvent.click(screen.getByTestId("uploadNoFile"));
|
||||
expect(screen.getByTestId("uploading").innerHTML).toEqual("false");
|
||||
expect(screen.getByTestId("progress").innerHTML).toEqual("-1");
|
||||
expect(screen.getByTestId("status").innerHTML).toEqual("Error occured: No file provided");
|
||||
expect(screen.getByTestId("status").innerHTML).toEqual("Error occured: File required");
|
||||
expect(screen.getByTestId("linkURL").innerHTML).toEqual("");
|
||||
})
|
||||
|
||||
it("Upload no access token", async () => {
|
||||
fireEvent.click(screen.getByTestId("uploadNoToken"));
|
||||
expect(screen.getByTestId("uploading").innerHTML).toEqual("false");
|
||||
expect(screen.getByTestId("progress").innerHTML).toEqual("-1");
|
||||
expect(screen.getByTestId("status").innerHTML).toEqual("Error occured: Access token required");
|
||||
expect(screen.getByTestId("linkURL").innerHTML).toEqual("");
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user