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] Convert Modal tests to testing-library #26912

Merged
merged 2 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
33 changes: 32 additions & 1 deletion packages/material-ui/src/Backdrop/Backdrop.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { expect } from 'chai';
import { createMount, createClientRender, describeConformanceV5 } from 'test/utils';
import { spy, useFakeTimers } from 'sinon';
import { createMount, createClientRender, describeConformanceV5, act } from 'test/utils';
import Backdrop, { backdropClasses as classes } from '@material-ui/core/Backdrop';
import Fade from '@material-ui/core/Fade';

Expand Down Expand Up @@ -32,4 +33,34 @@ describe('<Backdrop />', () => {
);
expect(container.querySelector('h1')).to.have.text('Hello World');
});

describe('prop: transitionDuration', () => {
/**
* @type {ReturnType<typeof useFakeTimers>}
*/
let clock;
beforeEach(() => {
clock = useFakeTimers();
});
afterEach(() => {
clock.restore();
});

it('delays appearance of its children', () => {
const handleEntered = spy();
render(
<Backdrop open transitionDuration={1954} onEntered={handleEntered}>
<div />
</Backdrop>,
);

expect(handleEntered.callCount).to.equal(0);

act(() => {
clock.tick(1954);
});

expect(handleEntered.callCount).to.equal(1);
});
});
});
38 changes: 25 additions & 13 deletions packages/material-ui/src/Modal/Modal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
within,
createMount,
describeConformanceV5,
screen,
} from 'test/utils';
import { createTheme, ThemeProvider } from '@material-ui/core/styles';
import Fade from '@material-ui/core/Fade';
import Backdrop from '@material-ui/core/Backdrop';
import Modal, { modalClasses as classes } from '@material-ui/core/Modal';

describe('<Modal />', () => {
Expand Down Expand Up @@ -111,18 +111,23 @@ describe('<Modal />', () => {
});

describe('backdrop', () => {
it('should render a backdrop with a fade transition', () => {
const wrapper = mount(
<Modal open BackdropComponent={Backdrop}>
it('can render a custom backdrop component', () => {
function TestBackdrop(props) {
const { open } = props;
if (!open) {
return null;
}

return <div data-testid="backdrop" />;
}

render(
<Modal open BackdropComponent={TestBackdrop}>
<div />
</Modal>,
);

const backdrop = wrapper.find(Backdrop);
expect(backdrop.exists()).to.equal(true);

const transition = backdrop.find(Fade);
Copy link
Member Author

Choose a reason for hiding this comment

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

Already tested in Backdrop.test

expect(transition.props()).to.have.property('in', true);
expect(screen.getByTestId('backdrop')).not.to.equal(null);
});

it('should render a backdrop component into the portal before the modal content', () => {
Expand All @@ -141,14 +146,21 @@ describe('<Modal />', () => {
});

it('should pass prop to the transition component', () => {
const wrapper = mount(
<Modal open BackdropComponent={Backdrop} BackdropProps={{ transitionDuration: 200 }}>
function TestBackdrop(props) {
const { open, transitionDuration } = props;
if (!open) {
return null;
}

return <div data-testid="backdrop" data-timeout={transitionDuration} />;
}
render(
<Modal open BackdropComponent={TestBackdrop} BackdropProps={{ transitionDuration: 200 }}>
<div />
</Modal>,
);

const transition = wrapper.find(Fade);
expect(transition.props()).to.have.property('timeout', 200);
expect(screen.getByTestId('backdrop')).to.have.attribute('data-timeout', '200');
});

it('should attach a handler to the backdrop that fires onClose', () => {
Expand Down