11// @flow
22import React from 'react' ;
3- import { render , waitFor } from '@testing-library/react' ;
3+ import { render , waitFor , act } from '@testing-library/react' ;
44import * as PopperJs from '@popperjs/core' ;
55
66// Public API
77import { 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
2025describe ( '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 } ) ;
0 commit comments