-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[StepButton] test using testing-library
- Loading branch information
Showing
1 changed file
with
82 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,117 @@ | ||
import 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 describeConformance from '../test-utils/describeConformance'; | ||
import { createClientRender } from 'test/utils/createClientRender'; | ||
import StepButton from './StepButton'; | ||
import StepLabel from '../StepLabel'; | ||
import ButtonBase from '../ButtonBase'; | ||
import { fireEvent } from '@testing-library/dom'; | ||
|
||
describe('<StepButton />', () => { | ||
let classes; | ||
let shallow; | ||
let mount; | ||
const defaultProps = { orientation: 'horizontal' }; | ||
const render = createClientRender(); | ||
|
||
before(() => { | ||
classes = getClasses(<StepButton />); | ||
shallow = createShallow({ dive: true }); | ||
mount = createMount({ strict: true }); | ||
}); | ||
|
||
after(() => { | ||
mount.cleanUp(); | ||
}); | ||
describe('internals', () => { | ||
let mount; | ||
|
||
describeConformance(<StepButton {...defaultProps} />, () => ({ | ||
classes, | ||
inheritComponent: ButtonBase, | ||
mount, | ||
refInstanceof: window.HTMLButtonElement, | ||
skip: ['componentProp'], | ||
})); | ||
|
||
it('passes active, completed, disabled to StepLabel', () => { | ||
const wrapper = shallow( | ||
<StepButton active completed disabled {...defaultProps}> | ||
Step One | ||
</StepButton>, | ||
); | ||
const stepLabel = wrapper.find(StepLabel); | ||
assert.strictEqual(stepLabel.props().active, true); | ||
assert.strictEqual(stepLabel.props().completed, true); | ||
assert.strictEqual(stepLabel.props().disabled, true); | ||
assert.strictEqual(stepLabel.props().children, 'Step One'); | ||
}); | ||
before(() => { | ||
mount = createMount({ strict: true }); | ||
}); | ||
|
||
it('should pass props to a provided StepLabel', () => { | ||
const wrapper = shallow( | ||
<StepButton active completed disabled label="Step One" {...defaultProps}> | ||
<StepLabel>Step One</StepLabel> | ||
</StepButton>, | ||
); | ||
const stepLabel = wrapper.find(StepLabel); | ||
assert.strictEqual(stepLabel.props().active, true); | ||
assert.strictEqual(stepLabel.props().completed, true); | ||
assert.strictEqual(stepLabel.props().disabled, true); | ||
}); | ||
after(() => { | ||
mount.cleanUp(); | ||
}); | ||
|
||
it('should pass disabled prop to the ButtonBase', () => { | ||
const wrapper = shallow( | ||
<StepButton disabled {...defaultProps}> | ||
Step One | ||
</StepButton>, | ||
); | ||
const stepLabel = wrapper.find(ButtonBase); | ||
assert.strictEqual(stepLabel.props().disabled, true); | ||
}); | ||
describeConformance(<StepButton />, () => ({ | ||
classes, | ||
inheritComponent: ButtonBase, | ||
mount, | ||
refInstanceof: window.HTMLButtonElement, | ||
skip: ['componentProp'], | ||
})); | ||
|
||
describe('event handlers', () => { | ||
describe('handleMouseEnter/Leave', () => { | ||
const handleMouseEnter = spy(); | ||
const handleMouseLeave = spy(); | ||
it('passes active, completed, disabled to StepLabel', () => { | ||
const wrapper = mount( | ||
<StepButton active completed disabled> | ||
Step One | ||
</StepButton>, | ||
); | ||
|
||
const stepLabel = wrapper.find(StepLabel); | ||
expect(stepLabel.props()).to.have.property('active', true); | ||
expect(stepLabel.props()).to.have.property('completed', true); | ||
expect(stepLabel.props()).to.have.property('disabled', true); | ||
expect(stepLabel.props()).to.have.property('children', 'Step One'); | ||
}); | ||
|
||
it('should pass props to a provided StepLabel', () => { | ||
const wrapper = mount( | ||
<StepButton active completed disabled label="Step One"> | ||
<StepLabel>Step One</StepLabel> | ||
</StepButton>, | ||
); | ||
|
||
it('should fire event callbacks', () => { | ||
const wrapper = shallow( | ||
<StepButton | ||
onMouseEnter={handleMouseEnter} | ||
onMouseLeave={handleMouseLeave} | ||
{...defaultProps} | ||
> | ||
Step One | ||
</StepButton>, | ||
); | ||
wrapper.simulate('mouseEnter'); | ||
assert.strictEqual(handleMouseEnter.callCount, 1); | ||
wrapper.simulate('mouseLeave'); | ||
assert.strictEqual(handleMouseEnter.callCount, 1); | ||
assert.strictEqual(handleMouseLeave.callCount, 1); | ||
}); | ||
const stepLabel = wrapper.find(StepLabel); | ||
expect(stepLabel.props()).to.have.property('active', true); | ||
expect(stepLabel.props()).to.have.property('completed', true); | ||
expect(stepLabel.props()).to.have.property('disabled', true); | ||
expect(stepLabel.props()).to.have.property('children', 'Step One'); | ||
}); | ||
}); | ||
|
||
it('should disable the button', () => { | ||
const { getByRole } = render(<StepButton disabled>Step One</StepButton>); | ||
|
||
it('should bubble callbacks used internally', () => { | ||
expect(getByRole('button')).to.have.property('disabled', true); | ||
}); | ||
|
||
describe('event handlers', () => { | ||
it('should forward mouseenter, mouseleave and touchstart', () => { | ||
const handleMouseEnter = spy(); | ||
const handleMouseLeave = spy(); | ||
const handleTouchStart = spy(); | ||
const wrapper = shallow( | ||
const { getByRole } = render( | ||
<StepButton | ||
onMouseEnter={handleMouseEnter} | ||
onMouseLeave={handleMouseLeave} | ||
onTouchStart={handleTouchStart} | ||
{...defaultProps} | ||
> | ||
Step One | ||
</StepButton>, | ||
); | ||
wrapper.simulate('mouseEnter'); | ||
assert.strictEqual(handleMouseEnter.callCount, 1); | ||
wrapper.simulate('mouseLeave'); | ||
assert.strictEqual(handleMouseEnter.callCount, 1); | ||
assert.strictEqual(handleMouseLeave.callCount, 1); | ||
wrapper.simulate('touchStart'); | ||
assert.strictEqual(handleMouseEnter.callCount, 1); | ||
assert.strictEqual(handleMouseLeave.callCount, 1); | ||
assert.strictEqual(handleTouchStart.callCount, 1); | ||
wrapper.simulate('mouseEnter'); | ||
wrapper.simulate('touchStart'); | ||
assert.strictEqual(handleMouseEnter.callCount, 2); | ||
assert.strictEqual(handleMouseLeave.callCount, 1); | ||
assert.strictEqual(handleTouchStart.callCount, 2); | ||
const button = getByRole('button'); | ||
|
||
fireEvent.mouseOver(button); | ||
|
||
expect(handleMouseEnter).to.have.property('callCount', 1); | ||
expect(handleMouseLeave).to.have.property('callCount', 0); | ||
expect(handleTouchStart).to.have.property('callCount', 0); | ||
|
||
fireEvent.mouseOut(button); | ||
|
||
expect(handleMouseEnter).to.have.property('callCount', 1); | ||
expect(handleMouseLeave).to.have.property('callCount', 1); | ||
expect(handleTouchStart).to.have.property('callCount', 0); | ||
|
||
// fake touch | ||
fireEvent.touchStart(button, { touches: [{}] }); | ||
|
||
expect(handleMouseEnter).to.have.property('callCount', 1); | ||
expect(handleMouseLeave).to.have.property('callCount', 1); | ||
expect(handleTouchStart).to.have.property('callCount', 1); | ||
|
||
fireEvent.mouseOver(button); | ||
fireEvent.touchStart(button, { touches: [{}] }); | ||
|
||
expect(handleMouseEnter).to.have.property('callCount', 2); | ||
expect(handleMouseLeave).to.have.property('callCount', 1); | ||
expect(handleTouchStart).to.have.property('callCount', 2); | ||
}); | ||
}); | ||
}); |