Skip to content

Commit a406b5f

Browse files
committed
fix error in test
1 parent 645dace commit a406b5f

File tree

1 file changed

+66
-44
lines changed

1 file changed

+66
-44
lines changed

src/test/with_floating.test.tsx

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,54 @@
1-
import { render } from "@testing-library/react";
1+
import { render, waitFor } from "@testing-library/react";
22
import React from "react";
33

44
import withFloating from "../with_floating";
55

66
import type { FloatingProps } from "../with_floating";
77

88
// Mock @floating-ui/react
9-
const mockUseFloating = jest.fn(() => ({
10-
placement: "bottom",
11-
strategy: "absolute",
12-
middlewareData: {},
13-
x: 0,
14-
y: 0,
15-
isPositioned: true,
16-
update: jest.fn(),
17-
floatingStyles: { position: "absolute" as const, top: 0, left: 0 },
18-
refs: {
19-
reference: { current: null },
20-
floating: { current: null },
21-
setFloating: jest.fn(),
22-
setReference: jest.fn(),
23-
},
24-
elements: {
25-
reference: null,
26-
floating: null,
27-
domReference: null,
28-
},
29-
context: {},
9+
jest.mock("@floating-ui/react", () => ({
10+
useFloating: jest.fn(() => ({
11+
placement: "bottom",
12+
strategy: "absolute",
13+
middlewareData: {},
14+
x: 0,
15+
y: 0,
16+
isPositioned: true,
17+
update: jest.fn(),
18+
floatingStyles: { position: "absolute" as const, top: 0, left: 0 },
19+
refs: {
20+
reference: { current: null },
21+
floating: { current: null },
22+
setFloating: jest.fn(),
23+
setReference: jest.fn(),
24+
},
25+
elements: {
26+
reference: null,
27+
floating: null,
28+
domReference: null,
29+
},
30+
context: {},
31+
})),
32+
arrow: jest.fn(() => ({ name: "arrow", fn: jest.fn() })),
33+
offset: jest.fn(() => ({ name: "offset", fn: jest.fn() })),
34+
flip: jest.fn(() => ({ name: "flip", fn: jest.fn() })),
35+
autoUpdate: jest.fn(),
3036
}));
3137

32-
const mockArrow = jest.fn(() => ({ name: "arrow", fn: jest.fn() }));
33-
const mockOffset = jest.fn(() => ({ name: "offset", fn: jest.fn() }));
34-
const mockFlip = jest.fn(() => ({ name: "flip", fn: jest.fn() }));
35-
const mockAutoUpdate = jest.fn();
36-
37-
jest.mock("@floating-ui/react", () => ({
38+
// Get the mocked functions
39+
const {
3840
useFloating: mockUseFloating,
3941
arrow: mockArrow,
4042
offset: mockOffset,
4143
flip: mockFlip,
4244
autoUpdate: mockAutoUpdate,
43-
}));
45+
} = jest.requireMock("@floating-ui/react") as {
46+
useFloating: jest.Mock;
47+
arrow: jest.Mock;
48+
offset: jest.Mock;
49+
flip: jest.Mock;
50+
autoUpdate: jest.Mock;
51+
};
4452

4553
interface TestComponentProps extends FloatingProps {
4654
testProp?: string;
@@ -59,9 +67,10 @@ const TestComponent: React.FC<TestComponentProps> = ({
5967
);
6068

6169
describe("withFloating", () => {
62-
it("wraps component and provides popperProps", () => {
70+
it("wraps component and provides popperProps", async () => {
6371
const WrappedComponent = withFloating(TestComponent);
6472
const { container } = render(<WrappedComponent />);
73+
await waitFor(() => expect(container).toBeTruthy());
6574

6675
expect(
6776
container.querySelector('[data-testid="test-component"]'),
@@ -71,28 +80,31 @@ describe("withFloating", () => {
7180
).toBe("bottom");
7281
});
7382

74-
it("passes through original props", () => {
83+
it("passes through original props", async () => {
7584
const WrappedComponent = withFloating(TestComponent);
7685
const { container } = render(<WrappedComponent testProp="custom-value" />);
86+
await waitFor(() => expect(container).toBeTruthy());
7787

7888
const testComponent = container.querySelector(
7989
'[data-testid="test-component"]',
8090
);
8191
expect(testComponent?.getAttribute("data-test-prop")).toBe("custom-value");
8292
});
8393

84-
it("provides arrowRef in popperProps", () => {
94+
it("provides arrowRef in popperProps", async () => {
8595
const WrappedComponent = withFloating(TestComponent);
8696
const { container } = render(<WrappedComponent />);
97+
await waitFor(() => expect(container).toBeTruthy());
8798

8899
expect(
89100
container.querySelector('[data-testid="arrow-ref"]')?.textContent,
90101
).toBe("no-ref");
91102
});
92103

93-
it("sets hidePopper to true by default", () => {
104+
it("sets hidePopper to true by default", async () => {
94105
const WrappedComponent = withFloating(TestComponent);
95106
render(<WrappedComponent />);
107+
await waitFor(() => expect(mockUseFloating).toHaveBeenCalled());
96108

97109
expect(mockUseFloating).toHaveBeenCalledWith(
98110
expect.objectContaining({
@@ -101,9 +113,10 @@ describe("withFloating", () => {
101113
);
102114
});
103115

104-
it("respects hidePopper prop when set to false", () => {
116+
it("respects hidePopper prop when set to false", async () => {
105117
const WrappedComponent = withFloating(TestComponent);
106118
render(<WrappedComponent hidePopper={false} />);
119+
await waitFor(() => expect(mockUseFloating).toHaveBeenCalled());
107120

108121
expect(mockUseFloating).toHaveBeenCalledWith(
109122
expect.objectContaining({
@@ -112,9 +125,10 @@ describe("withFloating", () => {
112125
);
113126
});
114127

115-
it("passes popperPlacement to useFloating", () => {
128+
it("passes popperPlacement to useFloating", async () => {
116129
const WrappedComponent = withFloating(TestComponent);
117130
render(<WrappedComponent popperPlacement="top" />);
131+
await waitFor(() => expect(mockUseFloating).toHaveBeenCalled());
118132

119133
expect(mockUseFloating).toHaveBeenCalledWith(
120134
expect.objectContaining({
@@ -123,9 +137,10 @@ describe("withFloating", () => {
123137
);
124138
});
125139

126-
it("includes default middleware", () => {
140+
it("includes default middleware", async () => {
127141
const WrappedComponent = withFloating(TestComponent);
128142
render(<WrappedComponent />);
143+
await waitFor(() => expect(mockUseFloating).toHaveBeenCalled());
129144

130145
expect(mockFlip).toHaveBeenCalledWith({ padding: 15 });
131146
expect(mockOffset).toHaveBeenCalledWith(10);
@@ -141,10 +156,11 @@ describe("withFloating", () => {
141156
);
142157
});
143158

144-
it("includes custom popperModifiers", () => {
159+
it("includes custom popperModifiers", async () => {
145160
const customModifier = { name: "custom", fn: jest.fn() };
146161
const WrappedComponent = withFloating(TestComponent);
147162
render(<WrappedComponent popperModifiers={[customModifier]} />);
163+
await waitFor(() => expect(mockUseFloating).toHaveBeenCalled());
148164

149165
expect(mockUseFloating).toHaveBeenCalledWith(
150166
expect.objectContaining({
@@ -155,10 +171,11 @@ describe("withFloating", () => {
155171
);
156172
});
157173

158-
it("passes popperProps to useFloating", () => {
174+
it("passes popperProps to useFloating", async () => {
159175
const customProps = { strategy: "fixed" as const };
160176
const WrappedComponent = withFloating(TestComponent);
161177
render(<WrappedComponent popperProps={customProps} />);
178+
await waitFor(() => expect(mockUseFloating).toHaveBeenCalled());
162179

163180
expect(mockUseFloating).toHaveBeenCalledWith(
164181
expect.objectContaining({
@@ -167,9 +184,10 @@ describe("withFloating", () => {
167184
);
168185
});
169186

170-
it("sets whileElementsMounted to autoUpdate", () => {
187+
it("sets whileElementsMounted to autoUpdate", async () => {
171188
const WrappedComponent = withFloating(TestComponent);
172189
render(<WrappedComponent />);
190+
await waitFor(() => expect(mockUseFloating).toHaveBeenCalled());
173191

174192
expect(mockUseFloating).toHaveBeenCalledWith(
175193
expect.objectContaining({
@@ -204,17 +222,21 @@ describe("withFloating", () => {
204222
);
205223
});
206224

207-
it("handles hidePopper boolean correctly", () => {
225+
it("handles hidePopper boolean correctly", async () => {
208226
const WrappedComponent = withFloating(TestComponent);
209227

210228
const { rerender } = render(<WrappedComponent hidePopper={true} />);
211-
expect(mockUseFloating).toHaveBeenCalledWith(
212-
expect.objectContaining({ open: false }),
229+
await waitFor(() =>
230+
expect(mockUseFloating).toHaveBeenCalledWith(
231+
expect.objectContaining({ open: false }),
232+
),
213233
);
214234

215235
rerender(<WrappedComponent hidePopper={false} />);
216-
expect(mockUseFloating).toHaveBeenCalledWith(
217-
expect.objectContaining({ open: true }),
236+
await waitFor(() =>
237+
expect(mockUseFloating).toHaveBeenCalledWith(
238+
expect.objectContaining({ open: true }),
239+
),
218240
);
219241
});
220242
});

0 commit comments

Comments
 (0)