Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 76822a2

Browse files
committed
test:fix act() warnings
1 parent dc99885 commit 76822a2

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/Popper.test.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
// @flow
22
import React from 'react';
3-
import { render, waitFor } from '@testing-library/react';
3+
import { render, waitFor, act } from '@testing-library/react';
44
import * as PopperJs from '@popperjs/core';
55

66
// Public API
77
import { Popper } from '.';
88

9-
const renderPopper = (props) =>
10-
render(
11-
<Popper {...props}>
12-
{({ ref, style, placement, arrowProps }) => (
13-
<div ref={ref} style={style} data-placement={placement}>
14-
<div {...arrowProps} />
15-
</div>
16-
)}
17-
</Popper>
18-
);
9+
const renderPopper = async (props): any => {
10+
let result;
11+
await act(async () => {
12+
result = await render(
13+
<Popper {...props}>
14+
{({ ref, style, placement, arrowProps }) => (
15+
<div ref={ref} style={style} data-placement={placement}>
16+
<div {...arrowProps} />
17+
</div>
18+
)}
19+
</Popper>
20+
);
21+
});
22+
return result;
23+
};
1924

2025
describe('Popper component', () => {
2126
it('renders the expected markup', async () => {
2227
const referenceElement = document.createElement('div');
28+
2329
const { asFragment } = await renderPopper({ referenceElement });
2430

2531
await waitFor(() => {
2632
expect(asFragment()).toMatchSnapshot();
2733
});
2834
});
2935

30-
it('handles changing refs gracefully', () => {
36+
it('handles changing refs gracefully', async () => {
3137
const referenceElement = document.createElement('div');
38+
3239
expect(() =>
3340
render(
3441
<Popper referenceElement={referenceElement}>
@@ -44,18 +51,22 @@ describe('Popper component', () => {
4451
</Popper>
4552
)
4653
).not.toThrow();
54+
55+
await waitFor(() => {});
4756
});
4857

4958
it('accepts a ref function', async () => {
5059
const myRef = jest.fn();
5160
const referenceElement = document.createElement('div');
52-
await render(
61+
62+
render(
5363
<Popper referenceElement={referenceElement} innerRef={myRef}>
5464
{({ ref, style, placement }) => (
5565
<div ref={ref} style={style} data-placement={placement} />
5666
)}
5767
</Popper>
5868
);
69+
5970
await waitFor(() => {
6071
expect(myRef).toBeCalled();
6172
});
@@ -64,13 +75,15 @@ describe('Popper component', () => {
6475
it('accepts a ref object', async () => {
6576
const myRef = React.createRef();
6677
const referenceElement = document.createElement('div');
67-
await render(
78+
79+
render(
6880
<Popper referenceElement={referenceElement} innerRef={myRef}>
6981
{({ ref, style, placement }) => (
7082
<div ref={ref} style={style} data-placement={placement} />
7183
)}
7284
</Popper>
7385
);
86+
7487
await waitFor(() => {
7588
expect(myRef.current).toBeDefined();
7689
});
@@ -98,7 +111,7 @@ describe('Popper component', () => {
98111
});
99112
});
100113

101-
fit(`should update placement when property is changed`, async () => {
114+
it(`should update placement when property is changed`, async () => {
102115
const referenceElement = document.createElement('div');
103116

104117
const Component = ({ placement }) => (
@@ -116,13 +129,11 @@ describe('Popper component', () => {
116129
</Popper>
117130
);
118131

119-
const { rerender, getByTestId } = await render(
120-
<Component placement="top" />
121-
);
132+
const { rerender, getByTestId } = render(<Component placement="top" />);
122133

123134
expect(getByTestId('placement').textContent).toBe('top');
124135

125-
await rerender(<Component placement="bottom" />);
136+
await waitFor(() => rerender(<Component placement="bottom" />));
126137

127138
expect(getByTestId('placement').textContent).toBe('bottom');
128139
});

src/usePopper.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { renderHook } from '@testing-library/react-hooks';
1+
import { renderHook, act } from '@testing-library/react-hooks';
22
import * as PopperJs from '@popperjs/core';
33

44
// Public API
@@ -30,7 +30,9 @@ describe('userPopper', () => {
3030
{ initialProps: { referenceElement, popperElement } }
3131
);
3232

33-
rerender({ referenceElement, popperElement });
33+
await act(async () => {
34+
await rerender({ referenceElement, popperElement });
35+
});
3436

3537
await wait(() => {
3638
expect(spy).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)