Files
ota-admin-portal/src/components/Contexts/FileUploadContext.test.jsx
John Wu e51450426e Merge Development to Main (#36)
* Fix sign up form bug

* Add run.sh to run setup and run web app

* Output node version

* Update readme with run.sh

* Fix file upload form to handle ota_update service

* Enable file upload form

Enable error boundary to catch React errors (#7)
Fix warning for link noreferrer
Include authorization header with file upload

* Remove default localhost settings (#8)

* Remove default localhost settings
Replace with deployment settings

* Fix for upload data format

* Fix test data for last commit

* Fix json link format and remove localhost default settings (#10)

* Remove default localhost settings
Replace with deployment settings

* Fix for upload data format

* Fix test data for last commit

* Fix link data format

* Fix link json again (#12)

Use id token instead of access token

* nginx things

* Web Worker Sign Out and Use Go API (#13)

* Calculate checksum and send with file upload

* Limit file upload and display rejected file error

* Add sign in timeout

* Check auth token structure before setting
Clean up

* Use web worker timer to sign out
Remove checksum
Point to Go ota update

* Remove checksum dependency

* Use compute auth service and fix static code analyzer warnings (#15)

* Clean up formatting

* Use new compute_auth service
Implment SSO
Implement token refresh
Clean up unit tests

* Fix unit tests

* Fix auth test
Fix warnings

* Update default settings for compute_auth

* Change main UI layout and add VINs to add and upload forms (#16)

* Add new upload update package form
Add new add vehicle form
Add new side menu layout
Add new toolbar layout
Update and add unit tests

* Enable add get and add vehicles

* Integration issues with ota_update service

* Update get vehicle JSON format

* Fix related unit test
Add release notes field

* Add StatusContext to display error and status messages

* Handle api error json (#18)

* Handle api error json

* Fix get vehicles error handling
Update .env.template

* Fix signout refresh (#20)

* Merge to main (#17)

* Fix sign up form bug

* Add run.sh to run setup and run web app

* Output node version

* Update readme with run.sh

* Fix file upload form to handle ota_update service

* Enable file upload form

Enable error boundary to catch React errors (#7)
Fix warning for link noreferrer
Include authorization header with file upload

* Remove default localhost settings (#8)

* Remove default localhost settings
Replace with deployment settings

* Fix for upload data format

* Fix test data for last commit

* Fix json link format and remove localhost default settings (#10)

* Remove default localhost settings
Replace with deployment settings

* Fix for upload data format

* Fix test data for last commit

* Fix link data format

* Fix link json again (#12)

Use id token instead of access token

* nginx things

* Web Worker Sign Out and Use Go API (#13)

* Calculate checksum and send with file upload

* Limit file upload and display rejected file error

* Add sign in timeout

* Check auth token structure before setting
Clean up

* Use web worker timer to sign out
Remove checksum
Point to Go ota update

* Remove checksum dependency

* Use compute auth service and fix static code analyzer warnings (#15)

* Clean up formatting

* Use new compute_auth service
Implment SSO
Implement token refresh
Clean up unit tests

* Fix unit tests

* Fix auth test
Fix warnings

* Update default settings for compute_auth

* Change main UI layout and add VINs to add and upload forms (#16)

* Add new upload update package form
Add new add vehicle form
Add new side menu layout
Add new toolbar layout
Update and add unit tests

* Enable add get and add vehicles

* Integration issues with ota_update service

* Update get vehicle JSON format

* Fix related unit test
Add release notes field

* Add StatusContext to display error and status messages

* Handle api error json (#18)

* Handle api error json

* Fix get vehicles error handling
Update .env.template

Co-authored-by: Rafi Greenberg <rgreenberg@fiskerinc.com>

* Fix sign out and refresh

* Check for bad json

Co-authored-by: Rafi Greenberg <rgreenberg@fiskerinc.com>

* Add role checks (#21)

* Add role checks

* Remove moved Roles enum

* Add package updates, car updates, and vehicle screens (#25)

* Car table update (#27)

* Add Datadog RUM (#28)

* fix run.sh

* Add updates by car screen and modal popup (#29)

* CEC-180 Cache Control (#30)

* Set cache expire to 1 day
Add snapshot tests for new screens

* Fix table pagniation random ids for snapshot tests

* Auto reload on chunk load error

* OTA Admin Portal => Admin Portal

* CEC-179 Car download progress (#32)

* Display download progress

* Change default

* Fix

* Fix

* Update readme

* Update readme and defaults
Fix Dockerfile

* CEC-179 Car update progress build fix (#33)

* Display download progress

* Change default

* Fix

* Fix

* Update readme

* Update readme and defaults
Fix Dockerfile

* Fix build

* Undo Docker changes (#34)

Co-authored-by: Rafi Greenberg <rgreenberg@fiskerinc.com>
Co-authored-by: Roger Standridge <rstandridge@fiskerinc.com>
2021-04-30 15:55:27 -07:00

156 lines
4.5 KiB
JavaScript

jest.mock("../../services/uploadFile");
import {
render,
cleanup,
screen,
fireEvent,
waitFor,
} from "@testing-library/react";
import { useState } from "react";
import { setUploadFileDelay } from "../../services/uploadFile";
import {
FileUploadProvider,
useFileUploadContext,
} from "../Contexts/FileUploadContext";
import { StatusProvider, useStatusContext } from "../Contexts/StatusContext";
const checkState = (uploading, progress, status, linkURL, message) => {
expect(screen.getByTestId("uploading").innerHTML).toEqual(uploading);
expect(screen.getByTestId("progress").innerHTML).toEqual(progress);
expect(screen.getByTestId("status").innerHTML).toEqual(status);
expect(screen.getByTestId("linkURL").innerHTML).toEqual(linkURL);
expect(screen.getByTestId("message").innerHTML).toEqual(message);
};
describe("FileUploadContext", () => {
beforeEach(() => {
const TestComp = () => {
const {
progress,
uploading,
status,
upload,
cancel,
} = useFileUploadContext();
const { message, setMessage } = useStatusContext();
const [link, setLink] = useState(null);
const TEST_FILE = [{ name: "test.jpg", size: 0 }];
const TEST_ACCESSTOKEN = "ACCESSTOKEN";
const TEST_FORMDATA = {
packagename: "TEST",
version: "VERSION",
vehicles: ["VIN"],
description: "TEST DESC",
releasenotes: "http://releasenotes.com",
};
const exec = async (form, token, file) => {
try {
const data = await upload(form, token, file);
if (data.link) {
setLink(data.link);
}
} catch (e) {
setMessage(e.message);
}
};
return (
<>
<div data-testid="uploading">{uploading.toString()}</div>
<div data-testid="progress">{progress.toString()}</div>
<div data-testid="status">{status}</div>
<div data-testid="message">{message}</div>
<div data-testid="linkURL">{link}</div>
<button
data-testid="uploadNoFile"
onClick={() => {
exec(TEST_FORMDATA, TEST_ACCESSTOKEN, null);
}}
/>
<button
data-testid="uploadNoToken"
onClick={() => {
exec(TEST_FORMDATA, null, TEST_FILE);
}}
/>
<button
data-testid="uploadNoFormData"
onClick={() => {
exec({}, TEST_ACCESSTOKEN, TEST_FILE);
}}
/>
<button
data-testid="upload"
onClick={() => exec(TEST_FORMDATA, TEST_ACCESSTOKEN, TEST_FILE)}
/>
<button data-testid="cancel" onClick={() => cancel()} />
</>
);
};
render(
<StatusProvider>
<FileUploadProvider>
<TestComp />
</FileUploadProvider>
</StatusProvider>
);
});
afterEach(() => {
cleanup();
});
it("Initial state", async () => {
checkState("false", "0", "", "", "");
});
it("Upload no file", async () => {
fireEvent.click(screen.getByTestId("uploadNoFile"));
await waitFor(() =>
expect(screen.getByTestId("message").innerHTML).not.toBe("")
);
checkState("false", "0", "", "", "File required");
});
it("Upload no access token", async () => {
fireEvent.click(screen.getByTestId("uploadNoToken"));
await waitFor(() =>
expect(screen.getByTestId("message").innerHTML).not.toBe("")
);
checkState("false", "0", "", "", "Access token required");
});
it("Upload no form data", async () => {
fireEvent.click(screen.getByTestId("uploadNoFormData"));
await waitFor(() =>
expect(screen.getByTestId("message").innerHTML).not.toBe("")
);
checkState("false", "0", "", "", "Package name required");
});
it("Upload file", async () => {
fireEvent.click(screen.getByTestId("upload"));
await waitFor(() =>
expect(screen.getByTestId("progress").innerHTML).toEqual("100")
);
checkState("true", "100", "Uploaded test.jpg", "CLOUDFRONT_URL", "");
});
it("Cancel upload", async () => {
setUploadFileDelay(true);
fireEvent.click(screen.getByTestId("upload"));
await waitFor(() =>
expect(screen.getByTestId("progress").innerHTML).toEqual("50")
);
checkState("true", "50", "Uploading test.jpg", "", "");
fireEvent.click(screen.getByTestId("cancel"));
await waitFor(() =>
expect(screen.getByTestId("progress").innerHTML).toEqual("0")
);
checkState("false", "0", "Upload cancelled", "", "");
});
});