Convert DataLoader test to use React Testing Library
This commit is contained in:
parent
f7e7ab1c37
commit
dd15b13a4c
2 changed files with 25 additions and 39 deletions
25
src/components/data-loader/DataLoader.test.tsx
Normal file
25
src/components/data-loader/DataLoader.test.tsx
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import { render, waitFor } from "@testing-library/react";
|
||||||
|
import React from "react";
|
||||||
|
import { MockAdminClient } from "../../stories/MockAdminClient";
|
||||||
|
import { DataLoader } from "./DataLoader";
|
||||||
|
|
||||||
|
describe("DataLoader", () => {
|
||||||
|
it("loads the data and renders the result", async () => {
|
||||||
|
const loader = () => Promise.resolve(["a", "b"]);
|
||||||
|
const { container } = render(
|
||||||
|
<MockAdminClient>
|
||||||
|
<DataLoader loader={loader}>
|
||||||
|
{(result) => (
|
||||||
|
<div>
|
||||||
|
{result.map((value) => (
|
||||||
|
<i key={value}>{value}</i>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</DataLoader>
|
||||||
|
</MockAdminClient>
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => expect(container.textContent).toEqual("ab"));
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,39 +0,0 @@
|
||||||
import React from "react";
|
|
||||||
import { DataLoader } from "../DataLoader";
|
|
||||||
import { act } from "@testing-library/react";
|
|
||||||
import { render, unmountComponentAtNode } from "react-dom";
|
|
||||||
import { MockAdminClient } from "../../../stories/MockAdminClient";
|
|
||||||
|
|
||||||
let container: HTMLDivElement;
|
|
||||||
beforeEach(() => {
|
|
||||||
container = document.createElement("div");
|
|
||||||
document.body.appendChild(container);
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
unmountComponentAtNode(container);
|
|
||||||
container.remove();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("<DataLoader />", () => {
|
|
||||||
it("render", async () => {
|
|
||||||
const loader = () => Promise.resolve(["a", "b"]);
|
|
||||||
await act(async () => {
|
|
||||||
render(
|
|
||||||
<MockAdminClient>
|
|
||||||
<DataLoader loader={loader}>
|
|
||||||
{(result) => (
|
|
||||||
<div>
|
|
||||||
{result.map((d, i) => (
|
|
||||||
<i key={i}>{d}</i>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</DataLoader>
|
|
||||||
</MockAdminClient>,
|
|
||||||
container
|
|
||||||
);
|
|
||||||
});
|
|
||||||
expect(container.textContent).toBe("ab");
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Reference in a new issue