Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Make remaining testing-library tests StrictMode compatible #26924

Merged
merged 3 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 32 additions & 29 deletions packages/material-ui/src/Select/Select.test.js
Expand Up @@ -20,9 +20,18 @@ import Divider from '@material-ui/core/Divider';
import classes from './selectClasses';

describe('<Select />', () => {
/**
* @type {ReturnType<typeof useFakeTimers>}
*/
let clock;
beforeEach(() => {
clock = useFakeTimers();
});
afterEach(() => {
clock.restore();
});
const mount = createMount();
// StrictModeViolation: triggers "not wrapped in act()" warnings from timers.
const render = createClientRender({ strict: false });
const render = createClientRender();

describeConformanceV5(<Select value="" />, () => ({
classes,
Expand Down Expand Up @@ -150,7 +159,9 @@ describe('<Select />', () => {
</Select>,
);
const trigger = screen.getByRole('button');
trigger.focus();
act(() => {
trigger.focus();
});

fireEvent.keyDown(trigger, { key });
expect(screen.getByRole('listbox', { hidden: false })).not.to.equal(null);
Expand All @@ -168,9 +179,13 @@ describe('<Select />', () => {
</Select>,
);
const button = getByRole('button');
button.focus();
act(() => {
button.focus();
});

button.blur();
act(() => {
button.blur();
});

expect(handleBlur.callCount).to.equal(1);
expect(handleBlur.firstCall.returnValue).to.equal('blur-testing');
Expand Down Expand Up @@ -245,7 +260,9 @@ describe('<Select />', () => {
</Select>,
);
fireEvent.mouseDown(getByRole('button'));
getAllByRole('option')[1].click();
act(() => {
getAllByRole('option')[1].click();
});

expect(onChangeHandler.calledOnce).to.equal(true);
const selected = onChangeHandler.args[0][1];
Expand All @@ -264,7 +281,9 @@ describe('<Select />', () => {
);

fireEvent.mouseDown(getByRole('button'));
getAllByRole('option')[1].click();
act(() => {
getAllByRole('option')[1].click();
});

expect(eventLog).to.deep.equal(['CHANGE_EVENT', 'CLOSE_EVENT']);
});
Expand All @@ -279,7 +298,9 @@ describe('<Select />', () => {
</Select>,
);
fireEvent.mouseDown(getByRole('button'));
getAllByRole('option')[1].click();
act(() => {
getAllByRole('option')[1].click();
});

expect(onChangeHandler.callCount).to.equal(0);
});
Expand Down Expand Up @@ -562,7 +583,9 @@ describe('<Select />', () => {
{ baseElement: document.body },
);
const trigger = screen.getByRole('button');
trigger.focus();
act(() => {
trigger.focus();
});

fireEvent.keyDown(trigger, { key: 'ArrowDown' });
expect(screen.queryByRole('listbox')).to.equal(null);
Expand All @@ -573,16 +596,6 @@ describe('<Select />', () => {
});

describe('prop: MenuProps', () => {
let clock;

beforeEach(() => {
clock = useFakeTimers();
});

afterEach(() => {
clock.restore();
});

it('should apply additional props to the Menu component', () => {
const onEntered = spy();
const { getByRole } = render(
Expand Down Expand Up @@ -661,16 +674,6 @@ describe('<Select />', () => {
});

describe('prop: open (controlled)', () => {
let clock;

beforeEach(() => {
clock = useFakeTimers();
});

afterEach(() => {
clock.restore();
});

it('should not focus on close controlled select', () => {
function ControlledWrapper() {
const [open, setOpen] = React.useState(false);
Expand Down
13 changes: 7 additions & 6 deletions packages/material-ui/src/SpeedDial/SpeedDial.test.js
Expand Up @@ -26,9 +26,8 @@ describe('<SpeedDial />', () => {
clock.restore();
});

// StrictModeViolation: not using act(), prefer test/utils/createClientRender
const mount = createMount({ strict: false });
const render = createClientRender({ strict: false });
const mount = createMount();
const render = createClientRender();

const icon = <Icon>font_icon</Icon>;
const FakeAction = () => <div />;
Expand Down Expand Up @@ -203,8 +202,8 @@ describe('<SpeedDial />', () => {
</SpeedDial>,
);
const fab = getByRole('button');
fab.focus();
act(() => {
fab.focus();
clock.tick();
});
expect(handleOpen.callCount).to.equal(1);
Expand All @@ -226,8 +225,8 @@ describe('<SpeedDial />', () => {
const fab = getByRole('button');
const actions = getAllByRole('menuitem');

fab.focus();
act(() => {
fab.focus();
clock.runAll();
});

Expand Down Expand Up @@ -301,7 +300,9 @@ describe('<SpeedDial />', () => {
))}
</SpeedDial>,
);
fabButton.focus();
act(() => {
fabButton.focus();
});
};

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/material-ui/test/integration/Select.test.js
Expand Up @@ -130,8 +130,6 @@ describe('<Select> integration', () => {
<MenuItem value={10}>Ten</MenuItem>
</Select>
</FormControl>,
// StrictModeViolation: Requires fake timers + act
{ strict: false },
Comment on lines -133 to -134
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to have been fixed somewhere in between now and #21817

);

act(() => {
Expand Down