11import * as React from 'react' ;
22import { create } from 'react-test-renderer' ;
33import { BaseFloatingSuggestions } from './FloatingSuggestions' ;
4- import * as ReactDOM from 'react-dom' ;
5- import * as ReactTestUtils from 'react-dom/test-utils' ;
4+ import { render , fireEvent } from '@testing-library/react' ;
65import type { IFloatingSuggestionItem } from './FloatingSuggestionsItem/FloatingSuggestionsItem.types' ;
76import type { IBaseFloatingSuggestionsProps } from './FloatingSuggestions.types' ;
87
@@ -11,7 +10,6 @@ export interface ISimple {
1110 name : string ;
1211}
1312
14- let container : HTMLDivElement | null ;
1513let _suggestions : IFloatingSuggestionItem < ISimple > [ ] ;
1614
1715const items : ISimple [ ] = [
@@ -25,8 +23,6 @@ const _onSuggestionRemoved = jest.fn();
2523const picker = React . createRef < HTMLDivElement > ( ) ;
2624
2725beforeEach ( ( ) => {
28- container = document . createElement ( 'div' ) ;
29- document . body . appendChild ( container ) ;
3026 _suggestions = [
3127 {
3228 key : '1' ,
@@ -50,11 +46,6 @@ beforeEach(() => {
5046} ) ;
5147
5248afterEach ( ( ) => {
53- if ( container ) {
54- document . body . removeChild ( container ) ;
55- ReactDOM . unmountComponentAtNode ( container ) ;
56- container = null ;
57- }
5849 jest . clearAllMocks ( ) ;
5950} ) ;
6051
@@ -91,32 +82,26 @@ describe('FloatingSuggestions', () => {
9182 } ) ;
9283
9384 it ( 'renders FloatingSuggestions with suggestions visible true' , ( ) => {
94- // Our functional tests need to run against actual DOM for callouts to work,
95- // since callout mount a new react root with ReactDOM.
96- //
97- // see https://github.com/facebook/react/pull/12895
98- ReactTestUtils . act ( ( ) => {
99- ReactDOM . render (
100- < BaseFloatingSuggestions
101- suggestions = { _suggestions }
102- isSuggestionsVisible = { true }
103- componentRef = { picker }
104- targetElement = { null }
105- suggestionsHeaderText = { 'People Test Suggestions' }
106- noResultsFoundText = { 'No Test Suggestions' }
107- onFloatingSuggestionsDismiss = { undefined }
108- showSuggestionRemoveButton = { true }
109- onSuggestionSelected = { _onSuggestionSelected }
110- onRemoveSuggestion = { _onSuggestionRemoved }
111- /> ,
112- container ,
113- ) as unknown as IBaseFloatingSuggestionsProps < ISimple > ;
114- } ) ;
115-
116- const floatingSuggestions = document . querySelector ( '.ms-FloatingSuggestions' ) as HTMLInputElement ;
85+ // Using React Testing Library instead of ReactDOM
86+ render (
87+ < BaseFloatingSuggestions
88+ suggestions = { _suggestions }
89+ isSuggestionsVisible = { true }
90+ componentRef = { picker }
91+ targetElement = { null }
92+ suggestionsHeaderText = { 'People Test Suggestions' }
93+ noResultsFoundText = { 'No Test Suggestions' }
94+ onFloatingSuggestionsDismiss = { undefined }
95+ showSuggestionRemoveButton = { true }
96+ onSuggestionSelected = { _onSuggestionSelected }
97+ onRemoveSuggestion = { _onSuggestionRemoved }
98+ /> ,
99+ ) ;
100+
101+ const floatingSuggestions = document . querySelector ( '.ms-FloatingSuggestions' ) as HTMLElement ;
117102 expect ( floatingSuggestions ) . toBeTruthy ( ) ;
118103
119- const callout = container ?. getElementsByClassName ( '.ms-FloatingSuggestions-callout' ) ;
104+ const callout = document . querySelector ( '.ms-FloatingSuggestions-callout' ) ;
120105 expect ( callout ) . toBeTruthy ( ) ;
121106
122107 const suggestionsList = document . querySelectorAll ( 'div[id^=FloatingSuggestionsItemId]' ) ;
@@ -130,58 +115,53 @@ describe('FloatingSuggestions', () => {
130115 } ) ;
131116
132117 it ( 'renders FloatingSuggestions and updates when suggestions are removed' , ( ) => {
133- ReactTestUtils . act ( ( ) => {
134- ReactDOM . render (
135- < BaseFloatingSuggestions
136- suggestions = { _suggestions }
137- isSuggestionsVisible = { true }
138- componentRef = { picker }
139- targetElement = { null }
140- suggestionsHeaderText = { 'People Test Suggestions' }
141- noResultsFoundText = { 'No Test Suggestions' }
142- onFloatingSuggestionsDismiss = { undefined }
143- showSuggestionRemoveButton = { true }
144- onSuggestionSelected = { _onSuggestionSelected }
145- onRemoveSuggestion = { _onSuggestionRemoved }
146- /> ,
147- container ,
148- ) as unknown as IBaseFloatingSuggestionsProps < ISimple > ;
149- } ) ;
118+ render (
119+ < BaseFloatingSuggestions
120+ suggestions = { _suggestions }
121+ isSuggestionsVisible = { true }
122+ componentRef = { picker }
123+ targetElement = { null }
124+ suggestionsHeaderText = { 'People Test Suggestions' }
125+ noResultsFoundText = { 'No Test Suggestions' }
126+ onFloatingSuggestionsDismiss = { undefined }
127+ showSuggestionRemoveButton = { true }
128+ onSuggestionSelected = { _onSuggestionSelected }
129+ onRemoveSuggestion = { _onSuggestionRemoved }
130+ /> ,
131+ ) ;
150132
151133 const suggestionListButtons = document . querySelectorAll ( '.ms-FloatingSuggestionsItem-itemButton' ) ;
152134 expect ( suggestionListButtons . length ) . toEqual ( 2 ) ;
153- ReactTestUtils . Simulate . click ( suggestionListButtons [ 0 ] ) ;
154- ReactTestUtils . Simulate . click ( suggestionListButtons [ 1 ] ) ;
135+ fireEvent . click ( suggestionListButtons [ 0 ] ) ;
136+ fireEvent . click ( suggestionListButtons [ 1 ] ) ;
155137 expect ( _onSuggestionSelected ) . toHaveBeenCalledTimes ( 2 ) ;
156138
157139 const closeButtons = document . querySelectorAll ( '.ms-FloatingSuggestionsItem-closeButton' ) ;
158140 // There should be 2 close buttons for each suggestion.
159141 expect ( closeButtons . length ) . toEqual ( 2 ) ;
160142
161- ReactTestUtils . Simulate . click ( closeButtons [ 0 ] ) ;
143+ fireEvent . click ( closeButtons [ 0 ] ) ;
162144 // On closing, suggestion removed should be called once.
163145 expect ( _onSuggestionRemoved ) . toHaveBeenCalledTimes ( 1 ) ;
164146 } ) ;
165147
166148 it ( 'shows no suggestions when no suggestions are provided' , ( ) => {
167149 _suggestions = [ ] ;
168- ReactTestUtils . act ( ( ) => {
169- ReactDOM . render (
170- < BaseFloatingSuggestions
171- suggestions = { _suggestions }
172- isSuggestionsVisible = { true }
173- componentRef = { picker }
174- targetElement = { null }
175- suggestionsHeaderText = { 'People Test Suggestions' }
176- noResultsFoundText = { 'No Test Suggestions' }
177- onFloatingSuggestionsDismiss = { undefined }
178- showSuggestionRemoveButton = { true }
179- onSuggestionSelected = { _onSuggestionSelected }
180- onRemoveSuggestion = { _onSuggestionRemoved }
181- /> ,
182- container ,
183- ) as unknown as IBaseFloatingSuggestionsProps < ISimple > ;
184- } ) ;
150+ render (
151+ < BaseFloatingSuggestions
152+ suggestions = { _suggestions }
153+ isSuggestionsVisible = { true }
154+ componentRef = { picker }
155+ targetElement = { null }
156+ suggestionsHeaderText = { 'People Test Suggestions' }
157+ noResultsFoundText = { 'No Test Suggestions' }
158+ onFloatingSuggestionsDismiss = { undefined }
159+ showSuggestionRemoveButton = { true }
160+ onSuggestionSelected = { _onSuggestionSelected }
161+ onRemoveSuggestion = { _onSuggestionRemoved }
162+ /> ,
163+ ) ;
164+
185165 const suggestionsList = document . querySelectorAll ( 'div[id^=FloatingSuggestionsItemId]' ) ;
186166 expect ( suggestionsList . length ) . toEqual ( 0 ) ;
187167
0 commit comments