Skip to content

Commit f71032d

Browse files
authored
Fix test issues discovered while trying to test React 17 (take 2) (microsoft#21032)
1 parent 08bb967 commit f71032d

18 files changed

+331
-278
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "none",
3+
"comment": "Fix test issues",
4+
"packageName": "@fluentui/react",
5+
"email": "elcraig@microsoft.com",
6+
"dependentChangeType": "none"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "none",
3+
"comment": "Fix test issues",
4+
"packageName": "@fluentui/react-experiments",
5+
"email": "elcraig@microsoft.com",
6+
"dependentChangeType": "none"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "none",
3+
"comment": "Fix test issues",
4+
"packageName": "@fluentui/react-hooks",
5+
"email": "elcraig@microsoft.com",
6+
"dependentChangeType": "none"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Simplify safeMount attach API",
4+
"packageName": "@fluentui/test-utilities",
5+
"email": "elcraig@microsoft.com",
6+
"dependentChangeType": "none"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "none",
3+
"comment": "Fix test issues",
4+
"packageName": "@fluentui/utilities",
5+
"email": "elcraig@microsoft.com",
6+
"dependentChangeType": "none"
7+
}

packages/react-experiments/src/components/FloatingSuggestionsComposite/FloatingSuggestionsComposite.test.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { create, act } from 'react-test-renderer';
2+
import { create } from 'react-test-renderer';
33
import { BaseFloatingSuggestions } from './FloatingSuggestions';
44
import * as ReactDOM from 'react-dom';
55
import * as ReactTestUtils from 'react-dom/test-utils';
@@ -95,7 +95,7 @@ describe('FloatingSuggestions', () => {
9595
// since callout mount a new react root with ReactDOM.
9696
//
9797
// see https://github.com/facebook/react/pull/12895
98-
act(() => {
98+
ReactTestUtils.act(() => {
9999
(ReactDOM.render(
100100
<BaseFloatingSuggestions
101101
suggestions={_suggestions}
@@ -130,7 +130,7 @@ describe('FloatingSuggestions', () => {
130130
});
131131

132132
it('renders FloatingSuggestions and updates when suggestions are removed', () => {
133-
act(() => {
133+
ReactTestUtils.act(() => {
134134
(ReactDOM.render(
135135
<BaseFloatingSuggestions
136136
suggestions={_suggestions}
@@ -165,7 +165,7 @@ describe('FloatingSuggestions', () => {
165165

166166
it('shows no suggestions when no suggestions are provided', () => {
167167
_suggestions = [];
168-
act(() => {
168+
ReactTestUtils.act(() => {
169169
(ReactDOM.render(
170170
<BaseFloatingSuggestions
171171
suggestions={_suggestions}

packages/react-hooks/src/useUnmount.test.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import * as ReactTestUtils from 'react-dom/test-utils';
23
import { mount } from 'enzyme';
34
import { useUnmount } from './useUnmount';
45

@@ -17,7 +18,9 @@ describe('useUnmount', () => {
1718
expect(onUnmount).toBeCalledTimes(0);
1819
const wrapper = mount(<TestComponent />);
1920
expect(onUnmount).toBeCalledTimes(0);
20-
wrapper.unmount();
21+
ReactTestUtils.act(() => {
22+
wrapper.unmount();
23+
});
2124
expect(onUnmount).toBeCalledTimes(1);
2225
});
2326
});

packages/react/__mocks__/@fluentui/utilities.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class MockAsync extends Async {
3636
}
3737

3838
public dispose() {
39-
clearTimeout(this._timeoutId);
39+
if (this._timeoutId) {
40+
clearTimeout(this._timeoutId);
41+
}
4042
this._timeoutId = undefined;
4143

4244
super.dispose();

packages/react/src/common/testUtilities.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function delay(millisecond: number): Promise<void> {
2828
/**
2929
* Mounts the element attached to a child of document.body. This is primarily for tests involving
3030
* event handlers (which don't work right unless the element is attached).
31+
* @deprecated Use `safeMount` from `@fluentui/test-utilities` instead
3132
*/
3233
export function mountAttached<C extends Component, P = C['props'], S = C['state']>(
3334
element: React.ReactElement<P>,

packages/react/src/components/ChoiceGroup/ChoiceGroup.test.tsx

+19-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as renderer from 'react-test-renderer';
55

66
import { ChoiceGroup } from './ChoiceGroup';
77
import { merge, resetIds } from '../../Utilities';
8-
import { mountAttached } from '../../common/testUtilities';
8+
import { safeMount } from '@fluentui/test-utilities';
99
import { isConformant } from '../../common/isConformant';
1010
import type { IChoiceGroupOption, IChoiceGroup, IChoiceGroupProps } from './ChoiceGroup.types';
1111

@@ -253,30 +253,34 @@ describe('ChoiceGroup', () => {
253253
it('can focus the checked option', () => {
254254
// This test has to mount the element to the document since ChoiceGroup.focus() uses document.getElementById()
255255
const choiceGroupRef = React.createRef<IChoiceGroup>();
256-
choiceGroup = mountAttached(
256+
safeMount(
257257
<ChoiceGroup options={TEST_OPTIONS} defaultSelectedKey="1" componentRef={choiceGroupRef} />,
258+
choiceGroup2 => {
259+
const option = choiceGroup2.getDOMNode().querySelector(CHOICE_QUERY_SELECTOR) as HTMLInputElement;
260+
const focusSpy = jest.spyOn(option, 'focus');
261+
262+
choiceGroupRef.current!.focus();
263+
expect(focusSpy).toHaveBeenCalled();
264+
},
265+
true /* attach */,
258266
);
259-
260-
const option = choiceGroup.getDOMNode().querySelector(CHOICE_QUERY_SELECTOR) as HTMLInputElement;
261-
const focusSpy = jest.spyOn(option, 'focus');
262-
263-
choiceGroupRef.current!.focus();
264-
expect(focusSpy).toHaveBeenCalled();
265267
});
266268

267269
it('can focus the first enabled option', () => {
268270
const choiceGroupRef = React.createRef<IChoiceGroup>();
269-
choiceGroup = mountAttached(
271+
safeMount(
270272
<ChoiceGroup
271273
options={[{ key: '0', text: 'disabled', disabled: true }, ...TEST_OPTIONS]}
272274
componentRef={choiceGroupRef}
273275
/>,
276+
choiceGroup2 => {
277+
const option = choiceGroup2.getDOMNode().querySelectorAll(CHOICE_QUERY_SELECTOR)![1] as HTMLInputElement;
278+
const focusSpy = jest.spyOn(option, 'focus');
279+
280+
choiceGroupRef.current!.focus();
281+
expect(focusSpy).toHaveBeenCalled();
282+
},
283+
true /* attach */,
274284
);
275-
276-
const option = choiceGroup.getDOMNode().querySelectorAll(CHOICE_QUERY_SELECTOR)![1] as HTMLInputElement;
277-
const focusSpy = jest.spyOn(option, 'focus');
278-
279-
choiceGroupRef.current!.focus();
280-
expect(focusSpy).toHaveBeenCalled();
281285
});
282286
});

0 commit comments

Comments
 (0)