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

[BottomNavigation] Migrate to testing library #20728

Merged
Merged
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
67 changes: 33 additions & 34 deletions packages/material-ui/src/BottomNavigation/BottomNavigation.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import * as React from 'react';
import { assert } from 'chai';
import { expect } from 'chai';
import { spy } from 'sinon';
import { createShallow, createMount, getClasses } from '@material-ui/core/test-utils';
import { createMount, getClasses } from '@material-ui/core/test-utils';
import { createClientRender, fireEvent } from 'test/utils/createClientRender';
import describeConformance from '../test-utils/describeConformance';
import BottomNavigationAction from '../BottomNavigationAction';
import Icon from '../Icon';
import BottomNavigation from './BottomNavigation';

describe('<BottomNavigation />', () => {
let shallow;
let mount;
let classes;
let actionClasses;
const render = createClientRender();
const icon = <Icon>restore</Icon>;
const getBottomNavigation = (container) => container.firstChild;

before(() => {
shallow = createShallow({ dive: true });
classes = getClasses(
<BottomNavigation showLabels value={0}>
<BottomNavigationAction icon={icon} />
</BottomNavigation>,
);
actionClasses = getClasses(<BottomNavigationAction icon={icon} />);
mount = createMount({ strict: true });
});

Expand All @@ -41,81 +44,77 @@ describe('<BottomNavigation />', () => {
);

it('renders with a null child', () => {
const wrapper = shallow(
const { container } = render(
<BottomNavigation showLabels value={0}>
<BottomNavigationAction label="One" />
{null}
<BottomNavigationAction label="Three" />
</BottomNavigation>,
);
assert.strictEqual(wrapper.find(BottomNavigationAction).length, 2);
expect(getBottomNavigation(container).childNodes.length).to.equal(2);
});

it('should pass selected prop to children', () => {
const wrapper = shallow(
const { container } = render(
<BottomNavigation showLabels value={1}>
<BottomNavigationAction icon={icon} />
<BottomNavigationAction icon={icon} />
</BottomNavigation>,
);
assert.strictEqual(wrapper.childAt(0).props().selected, false);
assert.strictEqual(wrapper.childAt(1).props().selected, true);
expect(getBottomNavigation(container).childNodes[0]).to.not.have.class(actionClasses.selected);
expect(getBottomNavigation(container).childNodes[1]).to.have.class(actionClasses.selected);
});

it('should overwrite parent showLabel prop', () => {
const wrapper = shallow(
<BottomNavigation showLabels value={1}>
<BottomNavigationAction icon={icon} />
<BottomNavigationAction icon={icon} showLabel={false} />
it('should overwrite parent showLabel prop adding class iconOnly', () => {
const { getByTestId } = render(
<BottomNavigation showLabels>
<BottomNavigationAction icon={icon} data-testid="withLabel" />
<BottomNavigationAction icon={icon} showLabel={false} data-testid="withoutLabel" />
</BottomNavigation>,
);
assert.strictEqual(wrapper.childAt(0).props().showLabel, true);
assert.strictEqual(wrapper.childAt(1).props().showLabel, false);
expect(getByTestId('withLabel')).to.not.have.class(actionClasses.iconOnly);
expect(getByTestId('withoutLabel')).to.have.class(actionClasses.iconOnly);
});

it('should forward the click', () => {
const handleChange = spy();
const wrapper = mount(
const { container } = render(
<BottomNavigation showLabels value={0} onChange={handleChange}>
<BottomNavigationAction icon={icon} />
<BottomNavigationAction icon={icon} />
</BottomNavigation>,
);
wrapper.find(BottomNavigationAction).at(1).simulate('click');
assert.strictEqual(handleChange.callCount, 1);
assert.strictEqual(handleChange.args[0][1], 1);
fireEvent.click(getBottomNavigation(container).childNodes[1]);
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.equal(1);
});

it('should use custom action values', () => {
const handleChange = spy();
const wrapper = mount(
const { container } = render(
<BottomNavigation showLabels value={'first'} onChange={handleChange}>
<BottomNavigationAction value="first" icon={icon} />
<BottomNavigationAction value="second" icon={icon} />
</BottomNavigation>,
);
wrapper.find(BottomNavigationAction).at(1).simulate('click');
assert.strictEqual(
handleChange.args[0][1],
'second',
'should have been called with value second',
);
fireEvent.click(getBottomNavigation(container).childNodes[1]);
expect(handleChange.args[0][1]).to.equal('second', 'should have been called with value second');
});

it('should handle also empty action value', () => {
const handleChange = spy();
const wrapper = mount(
const { container } = render(
<BottomNavigation showLabels value="val" onChange={handleChange}>
<BottomNavigationAction value="" icon={icon} />
<BottomNavigationAction icon={icon} />
<BottomNavigationAction value={null} icon={icon} />
</BottomNavigation>,
);
wrapper.find(BottomNavigationAction).at(0).simulate('click');
assert.strictEqual(handleChange.args[0][1], '');
wrapper.find(BottomNavigationAction).at(1).simulate('click');
assert.strictEqual(handleChange.args[1][1], 1);
wrapper.find(BottomNavigationAction).at(2).simulate('click');
assert.strictEqual(handleChange.args[2][1], null);
fireEvent.click(getBottomNavigation(container).childNodes[0]);
expect(handleChange.args[0][1], '');
fireEvent.click(getBottomNavigation(container).childNodes[1]);
expect(handleChange.args[1][1], 1);
fireEvent.click(getBottomNavigation(container).childNodes[2]);
expect(handleChange.args[2][1], '');
});
});