Skip to content

Commit 83f5e19

Browse files
authored
[ExpansionPanelSummary] Test in StrictMode (#17873)
1 parent b14d3d0 commit 83f5e19

File tree

14 files changed

+137
-116
lines changed

14 files changed

+137
-116
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@babel/preset-env": "^7.6.0",
5252
"@babel/preset-react": "^7.0.0",
5353
"@babel/register": "^7.6.2",
54+
"@testing-library/dom": "^6.8.1",
5455
"@testing-library/react": "^9.2.0",
5556
"@types/chai": "^4.2.3",
5657
"@types/chai-dom": "^0.0.8",

packages/material-ui-lab/src/SpeedDialAction/SpeedDialAction.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('<SpeedDialAction />', () => {
1717
};
1818

1919
before(() => {
20-
// StrictModeViolation: unknown
20+
// StrictModeViolation: uses Tooltip
2121
mount = createMount({ strict: false });
2222
classes = getClasses(<SpeedDialAction {...defaultProps} />);
2323
});

packages/material-ui/src/Button/Button.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import ButtonBase from '../ButtonBase';
88

99
describe('<Button />', () => {
1010
let mount;
11-
// StrictModeViolation uses ButtonBase
12-
const render = createClientRender({ strict: false });
11+
const render = createClientRender({ strict: true });
1312
let classes;
1413

1514
before(() => {
16-
mount = createMount({ strict: false });
15+
mount = createMount({ strict: true });
1716
classes = getClasses(<Button>Hello World</Button>);
1817
});
1918

packages/material-ui/src/ExpansionPanelSummary/ExpansionPanelSummary.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const ExpansionPanelSummary = React.forwardRef(function ExpansionPanelSummary(pr
9393
onBlur(event);
9494
}
9595
};
96+
// TODO: Remove in v5 and use onClick in ExpansionPanel.js
9697
const handleChange = event => {
9798
if (onChange) {
9899
onChange(event);
Lines changed: 75 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,119 @@
11
import React from 'react';
2-
import { assert } from 'chai';
2+
import { expect } from 'chai';
33
import { spy } from 'sinon';
4-
import { createMount, findOutermostIntrinsic, getClasses } from '@material-ui/core/test-utils';
4+
import { createMount, getClasses } from '@material-ui/core/test-utils';
55
import describeConformance from '../test-utils/describeConformance';
6+
import { createClientRender, fireEvent } from 'test/utils/createClientRender';
67
import ExpansionPanelSummary from './ExpansionPanelSummary';
78
import ButtonBase from '../ButtonBase';
89

910
describe('<ExpansionPanelSummary />', () => {
1011
let mount;
1112
let classes;
12-
13-
function findExpandButton(wrapper) {
14-
return wrapper.find('[role="button"]:not([aria-hidden=true])');
15-
}
13+
const render = createClientRender({ strict: true });
1614

1715
before(() => {
18-
// StrictModeViolation: uses simulate
19-
mount = createMount({ strict: false });
16+
mount = createMount({ strict: true });
2017
classes = getClasses(<ExpansionPanelSummary />);
2118
});
2219

23-
after(() => {
24-
mount.cleanUp();
25-
});
26-
2720
describeConformance(<ExpansionPanelSummary />, () => ({
2821
classes,
2922
inheritComponent: ButtonBase,
3023
mount,
3124
refInstanceof: window.HTMLDivElement,
3225
skip: ['componentProp'],
26+
after: () => mount.cleanUp(),
3327
}));
3428

35-
it('should render with the content', () => {
36-
const wrapper = mount(<ExpansionPanelSummary>The Summary</ExpansionPanelSummary>);
37-
const itemsWrap = wrapper.find(`.${classes.content}`);
38-
assert.strictEqual(itemsWrap.text(), 'The Summary');
29+
it('renders the children inside the .content element', () => {
30+
const { container } = render(<ExpansionPanelSummary>The Summary</ExpansionPanelSummary>);
31+
32+
expect(container.querySelector(`.${classes.content}`)).to.have.text('The Summary');
3933
});
4034

4135
it('when disabled should have disabled class', () => {
42-
const wrapper = mount(<ExpansionPanelSummary disabled />);
43-
assert.strictEqual(findExpandButton(wrapper).hasClass(classes.disabled), true);
36+
const { getByRole } = render(<ExpansionPanelSummary disabled />);
37+
38+
expect(getByRole('button')).to.have.class(classes.disabled);
4439
});
4540

46-
it('when expanded should have expanded class', () => {
47-
const wrapper = mount(<ExpansionPanelSummary expanded />);
48-
assert.strictEqual(wrapper.find('[aria-expanded=true]').every(`.${classes.expanded}`), true);
41+
it('when expanded adds the expanded class to any button regardless of a11y', () => {
42+
const { getAllByRole } = render(<ExpansionPanelSummary expanded expandIcon="expand" />);
43+
44+
const buttons = getAllByRole('button', { hidden: true });
45+
expect(buttons).to.have.length(2);
46+
expect(buttons[0]).to.have.class(classes.expanded);
47+
expect(buttons[0]).to.have.attribute('aria-expanded', 'true');
48+
expect(buttons[0]).not.to.be.inaccessible;
49+
expect(buttons[1]).to.have.class(classes.expanded);
50+
expect(buttons[1]).to.be.inaccessible;
4951
});
5052

5153
it('should render with the expand icon and have the expandIcon class', () => {
52-
const wrapper = mount(<ExpansionPanelSummary expandIcon={<div>Icon</div>} />);
53-
const iconWrap = wrapper.find(`.${classes.expandIcon}`).first();
54-
assert.strictEqual(iconWrap.text(), 'Icon');
54+
const { getAllByRole } = render(<ExpansionPanelSummary expandIcon={<div>Icon</div>} />);
55+
56+
const expandButton = getAllByRole('button', { hidden: true })[1];
57+
expect(expandButton).to.have.class(classes.expandIcon);
58+
expect(expandButton).to.have.text('Icon');
59+
expect(expandButton).to.be.inaccessible;
5560
});
5661

57-
it('handleFocusVisible() should set focused state', () => {
58-
const wrapper = mount(<ExpansionPanelSummary />);
59-
wrapper
60-
.find(ButtonBase)
61-
.props()
62-
.onFocusVisible();
63-
wrapper.update();
64-
assert.strictEqual(findExpandButton(wrapper).hasClass(classes.focused), true);
62+
it('focusing adds the `focused` class if focused visible', () => {
63+
// TODO: Rename `focused` -> `focus-visible`
64+
// `focused` is a global state which is applied on focus
65+
// only here do we constrain it to focus-visible. THe name is also not consistent
66+
// with :focus
67+
const { getByRole } = render(<ExpansionPanelSummary />);
68+
fireEvent.mouseDown(document.body); // pointer device
69+
70+
fireEvent.keyDown(document.activeElement, { key: 'Tab' }); // not actually focusing (yet)
71+
getByRole('button').focus();
72+
73+
expect(getByRole('button')).to.be.focused;
74+
expect(getByRole('button')).to.have.class(classes.focused);
6575
});
6676

67-
it('handleBlur() should unset focused state', () => {
68-
const wrapper = mount(<ExpansionPanelSummary />);
69-
wrapper
70-
.find(ButtonBase)
71-
.props()
72-
.onFocusVisible();
73-
wrapper.update();
74-
wrapper
75-
.find(ButtonBase)
76-
.props()
77-
.onBlur();
78-
wrapper.update();
79-
assert.strictEqual(findExpandButton(wrapper).hasClass(classes.focused), false);
77+
it('blur should unset focused state', () => {
78+
const { getByRole } = render(<ExpansionPanelSummary />);
79+
fireEvent.mouseDown(document.body); // pointer device
80+
fireEvent.keyDown(document.activeElement, { key: 'Tab' }); // not actually focusing (yet)
81+
getByRole('button').focus();
82+
83+
getByRole('button').blur();
84+
85+
expect(getByRole('button')).not.to.be.focused;
86+
expect(getByRole('button')).not.to.have.class(classes.focused);
8087
});
8188

82-
describe('event callbacks', () => {
83-
it('should fire event callbacks', () => {
84-
const events = ['onClick', 'onFocusVisible', 'onBlur'];
85-
86-
const handlers = events.reduce((result, n) => {
87-
result[n] = spy();
88-
return result;
89-
}, {});
90-
91-
const wrapper = mount(<ExpansionPanelSummary {...handlers} />);
92-
93-
events.forEach(event => {
94-
wrapper
95-
.find(ButtonBase)
96-
.props()
97-
[event]({ persist: () => {} });
98-
assert.strictEqual(handlers[event].callCount, 1, `should have called the ${event} handler`);
99-
});
100-
});
89+
it('should fire onClick callbacks', () => {
90+
const handleClick = spy();
91+
const { getByRole } = render(<ExpansionPanelSummary onClick={handleClick} />);
92+
93+
getByRole('button').click();
94+
95+
expect(handleClick.callCount).to.equal(1);
10196
});
10297

103-
describe('prop: onChange', () => {
104-
it('fires onChange if the summary control is clicked', () => {
105-
const handleChange = spy();
106-
const wrapper = mount(<ExpansionPanelSummary expanded={false} onChange={handleChange} />);
98+
it('calls onChange when clicking', () => {
99+
const handleChange = spy();
100+
const { getByRole } = render(<ExpansionPanelSummary onChange={handleChange} />);
107101

108-
const control = findOutermostIntrinsic(wrapper);
109-
const eventMock = 'woofExpansionPanelSummary';
110-
control.simulate('click', { eventMock });
102+
getByRole('button').click();
111103

112-
assert.strictEqual(handleChange.callCount, 1);
113-
assert.strictEqual(handleChange.calledWithMatch({ eventMock }), true);
114-
});
104+
expect(handleChange.callCount).to.equal(1);
115105
});
116106

117-
describe('prop: click', () => {
118-
it('should trigger onClick', () => {
119-
const handleClick = spy();
120-
const wrapper = mount(<ExpansionPanelSummary onClick={handleClick} />);
121-
wrapper.simulate('click');
122-
assert.strictEqual(handleClick.callCount, 1);
123-
});
107+
it('calls onFocusVisible if focused visibly', () => {
108+
const handleFocusVisible = spy();
109+
const { getByRole } = render(<ExpansionPanelSummary onFocusVisible={handleFocusVisible} />);
110+
// simulate pointer device
111+
fireEvent.mouseDown(document.body);
112+
113+
// this doesn't actually apply focus like in the browser. we need to move focus manually
114+
fireEvent.keyDown(document.body, { key: 'Tab' });
115+
getByRole('button').focus();
116+
117+
expect(handleFocusVisible.callCount).to.equal(1);
124118
});
125119
});

packages/material-ui/src/RadioGroup/RadioGroup.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('<RadioGroup />', () => {
1313
let mount;
1414

1515
before(() => {
16-
// StrictModeViolation: uses #simulate
16+
// StrictModeViolation: test uses #simulate
1717
mount = createMount({ strict: false });
1818
});
1919

packages/material-ui/src/Select/Select.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('<Select />', () => {
1616

1717
before(() => {
1818
classes = getClasses(<Select />);
19-
// StrictModeViolation: test uses MenuItem
19+
// StrictModeViolation: uses Menu
2020
mount = createMount({ strict: false });
2121
});
2222

packages/material-ui/src/Tab/Tab.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('<Tab />', () => {
2020

2121
before(() => {
2222
shallow = createShallow({ dive: true });
23-
// StrictModeViolation: uses text()
23+
// StrictModeViolation: test uses text()
2424
mount = createMount({ strict: false });
2525
classes = getClasses(<Tab textColor="inherit" />);
2626
});

packages/material-ui/src/TablePagination/TablePagination.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('<TablePagination />', () => {
3232
classes = getClasses(
3333
<TablePagination count={1} onChangePage={() => {}} page={0} rowsPerPage={10} />,
3434
);
35-
// StrictModeViolation: uses #html()
35+
// StrictModeViolation: test uses #html()
3636
mount = createMount({ strict: false });
3737
});
3838

packages/material-ui/test/integration/Menu.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ describe('<Menu /> integration', () => {
7474
* @type {ReturnType<useFakeTimers>}
7575
*/
7676
let clock;
77+
// StrictModeViolation: uses Popover
7778
const render = createClientRender({ strict: false });
7879

7980
beforeEach(() => {

0 commit comments

Comments
 (0)