wimi/test/App.test.tsx

48 lines
1.4 KiB
TypeScript
Raw 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", () => {
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(() => {
global.chrome = undefined;
});
describe("when the request is loading", () => {
2023-09-29 20:25:24 +02:00
beforeEach(() => { global.fetch = loading; });
afterEach(() => { global.fetch = undefined; });
2023-09-29 20:24:08 +02:00
test("loading text is rendered", () => {
render(<App/>);
expect(screen.getByText("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; });
afterEach(() => { global.fetch = undefined; });
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; });
afterEach(() => { global.fetch = undefined; });
2023-09-29 20:24:08 +02:00
test("error is rendered", async () => {
await act(() => render(<App/>));
expect(screen.getByTestId("error")).toBeInTheDocument();
});
});
});