Skip to content

Commit ec46572

Browse files
committed
feature: importComponent test
1 parent a181d87 commit ec46572

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {
2+
render,
3+
screen,
4+
act,
5+
waitForElementToBeRemoved,
6+
} from "@testing-library/react";
7+
import "@testing-library/jest-dom";
8+
9+
import { importComponent } from "../importComponent";
10+
11+
function renderComponent() {
12+
const sleep = (n: number) => new Promise<void>((r) => setTimeout(r, n));
13+
14+
function MockComponent() {
15+
return <h1>Mock Component!</h1>;
16+
}
17+
18+
const LazyComponent = importComponent(async () => {
19+
await sleep(100);
20+
21+
return {
22+
default: MockComponent,
23+
};
24+
});
25+
26+
return render(<LazyComponent />);
27+
}
28+
29+
describe("importComponent", () => {
30+
test("renders loader", () => {
31+
act(() => {
32+
renderComponent();
33+
});
34+
35+
expect(screen.getByText(/Loading/)).toBeInTheDocument();
36+
});
37+
38+
test("renders component", async () => {
39+
await act(async () => {
40+
renderComponent();
41+
});
42+
43+
await waitForElementToBeRemoved(() => screen.getByText(/Loading/));
44+
45+
expect(screen.getByText(/Mock Component!/)).toBeInTheDocument();
46+
});
47+
});

0 commit comments

Comments
 (0)