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] Remove data-mui-test from tests #23498

Merged
merged 1 commit into from
Nov 13, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ describe('<DateRangePicker />', () => {
open
renderInput={defaultRangeRenderInput}
onChange={() => {}}
renderDay={(day) => <div key={String(day)} data-mui-test="renderDayCalled" />}
renderDay={(day) => <div key={String(day)} data-testid="renderDayCalled" />}
Copy link
Member Author

Choose a reason for hiding this comment

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

Only change touching pickers since I didn't need to alter pickers implementation

value={[null, null]}
/>,
);

expect(getAllByMuiTest('renderDayCalled')).not.to.have.length(0);
expect(screen.getAllByTestId('renderDayCalled')).not.to.have.length(0);
});

it('prop – `calendars` renders provided amount of calendars', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/material-ui/src/Backdrop/Backdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const Backdrop = React.forwardRef(function Backdrop(props, ref) {
return (
<TransitionComponent in={open} timeout={transitionDuration} {...other}>
<div
data-mui-test="Backdrop"
className={clsx(
classes.root,
{
Expand Down
2 changes: 1 addition & 1 deletion packages/material-ui/src/Backdrop/Backdrop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('<Backdrop />', () => {

it('should render a backdrop div with content of nested children', () => {
const { container } = render(
<Backdrop open className="woofBackdrop">
Copy link
Member Author

Choose a reason for hiding this comment

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

Drive-by removal of unused props

<Backdrop open>
<h1>Hello World</h1>
</Backdrop>,
);
Expand Down
1 change: 0 additions & 1 deletion packages/material-ui/src/Dialog/Dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ const Dialog = React.forwardRef(function Dialog(props, ref) {
<div
className={clsx(classes.container, classes[`scroll${capitalize(scroll)}`])}
onMouseDown={handleMouseDown}
data-mui-test="FakeBackdrop"
>
<PaperComponent
elevation={24}
Expand Down
39 changes: 17 additions & 22 deletions packages/material-ui/src/Dialog/Dialog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
act,
createClientRender,
fireEvent,
screen,
} from 'test/utils';
import Modal from '../Modal';
import Dialog from './Dialog';
Expand All @@ -25,17 +26,17 @@ function userClick(element) {
}

/**
* @param {HTMLElement} container
* @param {typeof import('test/utils').screen} view
*/
function findBackdrop(container) {
return container.querySelector('[data-mui-test="FakeBackdrop"]');
function findBackdrop(view) {
return view.getByRole('dialog').parentElement;
}

/**
* @param {HTMLElement} container
* @param {typeof import('test/utils').screen} view
*/
function clickBackdrop(container) {
userClick(findBackdrop(container));
function clickBackdrop(view) {
userClick(findBackdrop(view));
}

describe('<Dialog />', () => {
Expand Down Expand Up @@ -108,7 +109,9 @@ describe('<Dialog />', () => {
dialog.click();
});

fireEvent.keyDown(document.querySelector('[data-mui-test="FakeBackdrop"]'), { key: 'Esc' });
// keyDown not targetted at anything specific
// eslint-disable-next-line material-ui/disallow-active-element-as-key-event-target
fireEvent.keyDown(document.activeElement, { key: 'Esc' });
expect(onEscapeKeyDown.calledOnce).to.equal(true);
expect(onClose.calledOnce).to.equal(true);

Expand Down Expand Up @@ -136,30 +139,22 @@ describe('<Dialog />', () => {

act(() => {
dialog.click();
fireEvent.keyDown(document.querySelector('[data-mui-test="FakeBackdrop"]'), { key: 'Esc' });
// keyDown is not targetted at anything specific.
// eslint-disable-next-line material-ui/disallow-active-element-as-key-event-target
fireEvent.keyDown(document.activeElement, { key: 'Esc' });
});

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

clickBackdrop(document.body);
clickBackdrop(screen);
expect(onClose.callCount).to.equal(0);
});

it('should spread custom props on the modal root node', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

drive-by removal of a duplicate test (describeConformance covers this case).

render(
<Dialog data-my-prop="woofDialog" open>
foo
</Dialog>,
);
const modal = document.querySelector('[data-mui-test="Modal"]');
expect(modal).to.have.attribute('data-my-prop', 'woofDialog');
});

describe('backdrop', () => {
it('does have `role` `none presentation`', () => {
render(<Dialog open>foo</Dialog>);

expect(findBackdrop(document.body)).to.have.attribute('role', 'none presentation');
expect(findBackdrop(screen)).to.have.attribute('role', 'none presentation');
});

it('calls onBackdropClick and onClose when clicked', () => {
Expand All @@ -171,7 +166,7 @@ describe('<Dialog />', () => {
</Dialog>,
);

clickBackdrop(document);
clickBackdrop(screen);
expect(onBackdropClick.callCount).to.equal(1);
expect(onClose.callCount).to.equal(1);
});
Expand All @@ -198,7 +193,7 @@ describe('<Dialog />', () => {
);

fireEvent.mouseDown(getByRole('heading'));
findBackdrop(document.body).click();
findBackdrop(screen).click();
expect(getByRole('dialog')).not.to.equal(null);
});
});
Expand Down
1 change: 0 additions & 1 deletion packages/material-ui/src/Menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ const Menu = React.forwardRef(function Menu(props, ref) {
{...other}
>
<MenuList
data-mui-test="Menu"
onKeyDown={handleListKeyDown}
actions={menuListActionsRef}
autoFocus={autoFocus && (activeItemIndex === -1 || disableAutoFocusItem)}
Expand Down
2 changes: 1 addition & 1 deletion packages/material-ui/src/Menu/Menu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('<Menu />', () => {
);
const popover = wrapper.find(Popover);
expect(popover.props().open).to.equal(true);
const menuEl = document.querySelector('[data-mui-test="Menu"]');
const menuEl = document.querySelector('[role="menu"]');
expect(document.activeElement).to.not.equal(menuEl);
expect(false).to.equal(menuEl.contains(document.activeElement));
});
Expand Down
1 change: 0 additions & 1 deletion packages/material-ui/src/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ const Modal = React.forwardRef(function Modal(inProps, ref) {
* https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-static-element-interactions.md
*/}
<div
data-mui-test="Modal"
ref={handleRef}
onKeyDown={handleKeyDown}
role="presentation"
Expand Down
1 change: 0 additions & 1 deletion packages/material-ui/src/Modal/SimpleBackdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const SimpleBackdrop = React.forwardRef(function SimpleBackdrop(props, ref) {

return open ? (
<div
data-mui-test="Backdrop"
aria-hidden
ref={ref}
{...other}
Expand Down
1 change: 0 additions & 1 deletion packages/material-ui/src/Popover/Popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ const Popover = React.forwardRef(function Popover(props, ref) {
{...TransitionProps}
>
<Paper
data-mui-test="Popover"
elevation={elevation}
ref={paperRef}
{...PaperProps}
Expand Down
28 changes: 17 additions & 11 deletions packages/material-ui/src/Popover/Popover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ describe('<Popover />', () => {

it('uses Grow as the Transition of the modal', () => {
const wrapper = mount(
<Popover {...defaultProps} open>
<Popover {...defaultProps} open data-testid="Modal">
<div />
</Popover>,
);
const modal = wrapper.find('[data-mui-test="Modal"]');
const modal = wrapper.find('[data-testid="Modal"]');
const transition = modal.find(Grow);

expect(transition.exists()).to.equal(true);
Expand Down Expand Up @@ -332,9 +332,10 @@ describe('<Popover />', () => {
anchorEl={anchorEl}
anchorOrigin={anchorOrigin}
transitionDuration={0}
PaperProps={{ 'data-testid': 'Popover' }}
TransitionProps={{
onEntered: () => {
popoverEl = document.querySelector('[data-mui-test="Popover"]');
popoverEl = document.querySelector('[data-testid="Popover"]');
resolve();
},
}}
Expand All @@ -348,9 +349,8 @@ describe('<Popover />', () => {
};

expectPopover = (top, left) => {
expect(popoverEl.style.top).to.equal(`${top}px`);
expect(popoverEl).toHaveInlineStyle({ top: `${top}px`, left: `${left}px` });

expect(popoverEl.style.left).to.equal(`${left}px`);
wrapper.unmount();
};
});
Expand Down Expand Up @@ -473,9 +473,10 @@ describe('<Popover />', () => {
anchorPosition={anchorPosition}
anchorOrigin={anchorOrigin}
transitionDuration={0}
PaperProps={{ 'data-testid': 'Popover' }}
TransitionProps={{
onEntered: () => {
popoverEl = document.querySelector('[data-mui-test="Popover"]');
popoverEl = document.querySelector('[data-testid="Popover"]');
resolve();
},
}}
Expand All @@ -487,9 +488,11 @@ describe('<Popover />', () => {
});

expectPopover = (top, left) => {
expect(popoverEl.style.top).to.equal(`${top}px`);
expect(popoverEl).toHaveInlineStyle({
Copy link
Member Author

Choose a reason for hiding this comment

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

unrelated refactor while I was working with the test.

top: `${top}px`,
left: `${left}px`,
});

expect(popoverEl.style.left).to.equal(`${left}px`);
wrapper.unmount();
};
});
Expand Down Expand Up @@ -521,11 +524,12 @@ describe('<Popover />', () => {
transitionDuration={0}
TransitionProps={{
onEntered: () => {
popoverEl = document.querySelector('[data-mui-test="Popover"]');
popoverEl = document.querySelector('[data-testid="Popover"]');
resolve();
},
}}
PaperProps={{
'data-testid': 'Popover',
style: {
top: 11,
left: 12,
Expand All @@ -539,9 +543,11 @@ describe('<Popover />', () => {
});

expectPopover = (top, left) => {
expect(popoverEl.style.top).to.equal(`${top}px`);
expect(popoverEl).toHaveInlineStyle({
top: `${top}px`,
left: `${left}px`,
});

expect(popoverEl.style.left).to.equal(`${left}px`);
wrapper.unmount();
};
});
Expand Down
5 changes: 3 additions & 2 deletions packages/material-ui/test/integration/Menu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function ButtonMenu(props) {
open={open}
onClose={handleClose}
transitionDuration={0}
BackdropProps={{ 'data-testid': 'Backdrop' }}
{...other}
>
{options.map((option, index) => (
Expand Down Expand Up @@ -339,15 +340,15 @@ describe('<Menu /> integration', () => {
});

it('closes the menu when the backdrop is clicked', () => {
const { getByRole } = render(<ButtonMenu />);
const { getByRole, getByTestId } = render(<ButtonMenu />);
const button = getByRole('button');
act(() => {
button.focus();
button.click();
});

act(() => {
document.querySelector('[data-mui-test="Backdrop"]').click();
getByTestId('Backdrop').click();
clock.tick(0);
});

Expand Down
10 changes: 9 additions & 1 deletion test/utils/initMatchers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chai from 'chai';
import chai, { AssertionError } from 'chai';
import chaiDom from 'chai-dom';
import _ from 'lodash';
import { isInaccessible } from '@testing-library/dom';
Expand Down Expand Up @@ -367,6 +367,10 @@ chai.use((chaiAPI, utils) => {
expectedStyleUnnormalized: Record<string, string>,
) {
const element = utils.flag(this, 'object') as HTMLElement;
if (element?.nodeType !== 1) {
// Same pre-condition for negated and unnegated assertion
throw new AssertionError(`Expected an Element but got ${String(element)}`);
}

assertMatchingStyles.call(this, element.style, expectedStyleUnnormalized, {
styleTypeHint: 'inline',
Expand All @@ -377,6 +381,10 @@ chai.use((chaiAPI, utils) => {
expectedStyleUnnormalized: Record<string, string>,
) {
const element = utils.flag(this, 'object') as HTMLElement;
if (element?.nodeType !== 1) {
// Same pre-condition for negated and unnegated assertion
throw new AssertionError(`Expected an Element but got ${String(element)}`);
}
const computedStyle = element.ownerDocument.defaultView!.getComputedStyle(element);

assertMatchingStyles.call(this, computedStyle, expectedStyleUnnormalized, {
Expand Down