wimi/test/App.test.tsx

51 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2023-09-29 20:24:08 +02:00
import React from "react";
2023-09-29 20:25:24 +02:00
import '@testing-library/jest-dom';
import { render, screen, act } from "@testing-library/react";
2023-09-29 20:24:08 +02:00
import { App } from "~/components/App";
import { getMessage } from "./mocks/chrome.i18n";
import { success, error, loading } from "./mocks/fetch";
describe("App.tsx", () => {
2024-08-03 10:01:20 +02:00
const globalChrome = global.chrome;
const globalFetch = global.fetch;
2023-09-29 20:24:08 +02:00
beforeEach(() => {
2023-09-29 20:25:24 +02:00
const chrome: any = { i18n: { getMessage } };
2023-09-29 20:24:08 +02:00
global.chrome = chrome;
});
afterEach(() => {
2024-08-03 10:01:20 +02:00
global.chrome = globalChrome;
2023-09-29 20:24:08 +02:00
});
describe("when the request is loading", () => {
2023-09-29 20:25:24 +02:00
beforeEach(() => { global.fetch = loading; });
2024-08-03 10:01:20 +02:00
afterEach(() => { global.fetch = globalFetch; });
2023-09-29 20:24:08 +02:00
test("loading text is rendered", () => {
render(<App/>);
2024-09-01 22:14:01 +02:00
expect(screen.getByTestId("loading")).toBeInTheDocument();
2023-09-29 20:25:24 +02:00
});
2023-09-29 20:24:08 +02:00
});
describe("when the request is a success", () => {
2023-09-29 20:25:24 +02:00
beforeEach(() => { global.fetch = success; });
2024-08-03 10:01:20 +02:00
afterEach(() => { global.fetch = globalFetch; });
2023-09-29 20:24:08 +02:00
test("response is rendered", async () => {
await act(() => render(<App/>));
expect(screen.getByTestId("response")).toBeInTheDocument();
});
});
describe("when the request throws an error", () => {
2023-09-29 20:25:24 +02:00
beforeEach(() => { global.fetch = error; });
2024-08-03 10:01:20 +02:00
afterEach(() => { global.fetch = globalFetch; });
2023-09-29 20:24:08 +02:00
test("error is rendered", async () => {
await act(() => render(<App/>));
expect(screen.getByTestId("error")).toBeInTheDocument();
});
});
});