Skip to content

Commit

Permalink
Fix test issues revealed by React 17 work
Browse files Browse the repository at this point in the history
  • Loading branch information
ecraig12345 committed Feb 18, 2021
1 parent 3b7379f commit 5e28f09
Show file tree
Hide file tree
Showing 26 changed files with 538 additions and 374 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Fix test issues",
"packageName": "@fluentui/react",
"email": "elcraig@microsoft.com",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Fix test issues",
"packageName": "@fluentui/react-experiments",
"email": "elcraig@microsoft.com",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Fix test issues",
"packageName": "@fluentui/react-hooks",
"email": "elcraig@microsoft.com",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Fix test issues",
"packageName": "@fluentui/react-tabs",
"email": "elcraig@microsoft.com",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Add new safeRender API (wrapping ReactDOM.render) and add attach option to safeMount.",
"packageName": "@fluentui/test-utilities",
"email": "elcraig@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Fix test issues",
"packageName": "@fluentui/utilities",
"email": "elcraig@microsoft.com",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { create, act } from 'react-test-renderer';
import { create } from 'react-test-renderer';
import { BaseFloatingSuggestions } from './FloatingSuggestions';
import * as ReactDOM from 'react-dom';
import { IFloatingSuggestionItem } from './FloatingSuggestionsItem/FloatingSuggestionsItem.types';
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('FloatingSuggestions', () => {
// since callout mount a new react root with ReactDOM.
//
// see https://github.com/facebook/react/pull/12895
act(() => {
ReactTestUtils.act(() => {
(ReactDOM.render(
<BaseFloatingSuggestions
suggestions={_suggestions}
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('FloatingSuggestions', () => {
});

it('renders FloatingSuggestions and updates when suggestions are removed', () => {
act(() => {
ReactTestUtils.act(() => {
(ReactDOM.render(
<BaseFloatingSuggestions
suggestions={_suggestions}
Expand Down Expand Up @@ -165,7 +165,7 @@ describe('FloatingSuggestions', () => {

it('shows no suggestions when no suggestions are provided', () => {
_suggestions = [];
act(() => {
ReactTestUtils.act(() => {
(ReactDOM.render(
<BaseFloatingSuggestions
suggestions={_suggestions}
Expand Down
5 changes: 4 additions & 1 deletion packages/react-hooks/src/useUnmount.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as ReactTestUtils from 'react-dom/test-utils';
import { mount } from 'enzyme';
import { useUnmount } from './useUnmount';

Expand All @@ -17,7 +18,9 @@ describe('useUnmount', () => {
expect(onUnmount).toBeCalledTimes(0);
const wrapper = mount(<TestComponent />);
expect(onUnmount).toBeCalledTimes(0);
wrapper.unmount();
ReactTestUtils.act(() => {
wrapper.unmount();
});
expect(onUnmount).toBeCalledTimes(1);
});
});
1 change: 1 addition & 0 deletions packages/react-tabs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@fluentui/common-styles": "^1.0.0-beta.17",
"@fluentui/eslint-plugin": "^1.0.0-beta.2",
"@fluentui/react-conformance": "^1.0.0",
"@fluentui/test-utilities": "^8.0.0-beta.8",
"@types/enzyme": "3.10.3",
"@types/enzyme-adapter-react-16": "1.0.3",
"@types/react": "16.9.42",
Expand Down
117 changes: 72 additions & 45 deletions packages/react-tabs/src/components/Tabs/Tabs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { create } from '@fluentui/utilities/lib/test';
import { mount } from 'enzyme';
import { resetIds } from '@fluentui/utilities';
import { safeMount, safeCreate } from '@fluentui/test-utilities';
import { Tabs, TabItem, TabsImperativeHandle } from './index';
import { isConformant } from '../../common/isConformant';

Expand All @@ -10,15 +9,18 @@ describe('Tabs', () => {
// Resetting ids to create predictability in generated ids.
resetIds();
});

it('renders tabs as links correctly', () => {
const component = create(
safeCreate(
<Tabs>
<TabItem headerText="Test Link 1" />
<TabItem headerText="" />
</Tabs>,
component => {
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
},
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

isConformant({
Expand All @@ -29,31 +31,33 @@ describe('Tabs', () => {
it('can be focused', () => {
const tabsRef = React.createRef<TabsImperativeHandle>();

mount(
<Tabs componentRef={tabsRef}>
<TabItem headerText="Link 1" />
<TabItem headerText="Link 2" />
</Tabs>,
);

// Instruct FocusZone to treat all elements as visible.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(HTMLElement.prototype as any).isVisible = true;

try {
expect(tabsRef.current).toBeTruthy();
safeMount(
<Tabs componentRef={tabsRef}>
<TabItem headerText="Link 1" />
<TabItem headerText="Link 2" />
</Tabs>,
() => {
try {
expect(tabsRef.current).toBeTruthy();

tabsRef.current!.focus();
expect(document.activeElement).toBeTruthy();
expect(document.activeElement!.textContent?.trim()).toEqual('Link 1');
} finally {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (HTMLElement.prototype as any).isVisible;
}
tabsRef.current!.focus();
expect(document.activeElement).toBeTruthy();
expect(document.activeElement!.textContent?.trim()).toEqual('Link 1');
} finally {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (HTMLElement.prototype as any).isVisible;
}
},
true /* attached, for focus tests */,
);
});

it('supports JSX expressions', () => {
const component = create(
safeCreate(
<Tabs defaultSelectedKey="1">
<TabItem headerText="Test Link 1">
<div>This is item 1</div>
Expand All @@ -63,79 +67,102 @@ describe('Tabs', () => {
<div>This is Item 3</div>
</TabItem>
</Tabs>,
component => {
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
},
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders large tabs correctly', () => {
const component = create(
safeCreate(
<Tabs tabSize="large">
<TabItem headerText="Test Link 1" />
<TabItem headerText="" />
</Tabs>,
component => {
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
},
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders tab format correctly', () => {
const component = create(
safeCreate(
<Tabs tabFormat="tabs">
<TabItem headerText="Test Link 1" />
<TabItem headerText="" />
</Tabs>,
component => {
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
},
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders large tab format correctly', () => {
const component = create(
safeCreate(
<Tabs tabFormat="tabs" tabSize="large">
<TabItem headerText="Test Link 1" />
<TabItem headerText="" />
</Tabs>,
component => {
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
},
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders Tabs correctly with custom className', () => {
const component = create(
safeCreate(
<Tabs className="specialClassName">
<TabItem headerText="Test Link 1" className="specialClassName" />
<TabItem headerText="Test Link 2" />
</Tabs>,
component => {
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
},
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders Tabs correctly with icon, text and count', () => {
const component = create(
safeCreate(
<Tabs>
<TabItem itemCount={12} />
<TabItem headerText="Test Link" itemCount={12} />
<TabItem headerText="Text with icon" itemIcon="Recent" />
</Tabs>,
component => {
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
},
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders Tabs correctly when itemCount is a string', () => {
const component = create(
safeCreate(
<Tabs>
<TabItem headerText="test" />
<TabItem headerText="Test Link" itemCount="20+" />
</Tabs>,
component => {
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
},
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders Tabs with overflow', () => {
const component = create(
safeCreate(
<Tabs overflowBehavior="menu">
<TabItem headerText="Test 1" />
<TabItem headerText="Test 2" />
</Tabs>,
component => {
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
},
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});
4 changes: 3 additions & 1 deletion packages/react/__mocks__/@fluentui/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class MockAsync extends Async {
}

public dispose() {
clearTimeout(this._timeoutId);
if (this._timeoutId) {
clearTimeout(this._timeoutId);
}
this._timeoutId = null;

super.dispose();
Expand Down
13 changes: 6 additions & 7 deletions packages/react/src/components/ComboBox/ComboBox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as ReactTestUtils from 'react-dom/test-utils';
import * as renderer from 'react-test-renderer';
import { KeyCodes, resetIds } from '../../Utilities';
import { ComboBox } from './ComboBox';
Expand Down Expand Up @@ -196,7 +195,7 @@ describe('ComboBox', () => {

// open combobox
const buttonElement = container.root.findByType('button');
ReactTestUtils.act(() => {
renderer.act(() => {
buttonElement?.props?.onClick();
});

Expand All @@ -222,7 +221,7 @@ describe('ComboBox', () => {

// open combobox
const buttonElement = container.root.findByType('button');
ReactTestUtils.act(() => {
renderer.act(() => {
buttonElement?.props?.onClick();
});

Expand Down Expand Up @@ -708,7 +707,7 @@ describe('ComboBox', () => {
node => node.type === 'button' && node.props?.className?.includes?.('ms-ComboBox'),
);

ReactTestUtils.act(() => {
renderer.act(() => {
buttonElement?.props?.onClick?.();
});

Expand Down Expand Up @@ -904,7 +903,7 @@ describe('ComboBox', () => {
const onItemClickMock = jest.fn();
safeCreate(<ComboBox options={DEFAULT_OPTIONS} onItemClick={onItemClickMock} />, container => {
const caretElement = findNodeWithClass(container, 'ms-ComboBox-CaretDown-button');
ReactTestUtils.act(() => {
renderer.act(() => {
caretElement?.props?.onClick?.();
});
const button = findNodeWithClass(container, 'ms-ComboBox-option', true);
Expand Down Expand Up @@ -963,13 +962,13 @@ describe('ComboBox', () => {

// Open combobox
const buttonElement = comboBoxRoot.findByType('button');
ReactTestUtils.act(() => {
renderer.act(() => {
buttonElement?.props?.onClick();
});
expect(onMenuOpenMock.mock.calls.length).toBe(1);

// Close combobox
ReactTestUtils.act(() => {
renderer.act(() => {
buttonElement?.props?.onClick();
});
expect(onMenuDismissedMock.mock.calls.length).toBe(1);
Expand Down
Loading

0 comments on commit 5e28f09

Please sign in to comment.