-
Notifications
You must be signed in to change notification settings - Fork 392
test(Label): Refactor Label tests to use shallow rendering #295
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
Merged
jeff-phillips-18
merged 1 commit into
patternfly:master
from
dmiller9911:Tests-DisposableLabel
Apr 9, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,32 +1,19 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import Label from './Label'; | ||
| import RemoveButton from './RemoveButton'; | ||
| import { noop } from '../../common/helpers'; | ||
|
|
||
| /** | ||
| * Country page Component | ||
| */ | ||
| const DisposableLabel = ({ children, type, onRemoveClick, ...rest }) => ( | ||
| <Label bsStyle={type} {...rest}> | ||
| {children} | ||
| <RemoveButton onRemoveClick={onRemoveClick} title="Remove" /> | ||
| </Label> | ||
| ); | ||
| const DisposableLabel = props => <Label {...props} />; | ||
|
|
||
| DisposableLabel.propTypes = { | ||
| /** Children nodes */ | ||
| children: PropTypes.node, | ||
| /** Label type */ | ||
| type: PropTypes.string, | ||
| /** callback when Label is removed */ | ||
| onRemoveClick: PropTypes.func | ||
| ...Label.propTypes | ||
| }; | ||
|
|
||
| DisposableLabel.defaultProps = { | ||
| children: null, | ||
| type: 'default', | ||
| ...Label.defaultProps, | ||
| onRemoveClick: noop | ||
| }; | ||
| /** sdd */ | ||
|
|
||
| export default DisposableLabel; |
This file contains hidden or 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,18 +1,17 @@ | ||
| import React from 'react'; | ||
| import { mount } from 'enzyme'; | ||
| import { DisposableLabel } from './index'; | ||
| import { MockLabelRemove } from './__mocks__/mockLabelExamples'; | ||
| import { shallow } from 'enzyme'; | ||
| import DisposableLabel from './DisposableLabel'; | ||
| import Label from './Label'; | ||
| import { noop } from '../../common/helpers'; | ||
|
|
||
| test('Label renders properly', () => { | ||
| const component = mount( | ||
| <DisposableLabel type="default">Some text</DisposableLabel> | ||
| ); | ||
| expect(component.render()).toMatchSnapshot(); | ||
| test('defaults props', () => { | ||
| const view = shallow(<DisposableLabel />); | ||
| expect(view).toMatchSnapshot(); | ||
| expect(view.find(Label).props().onRemoveClick).toBe(noop); | ||
| }); | ||
|
|
||
| test('Label is removed via function', () => { | ||
| const component = mount(<MockLabelRemove />); | ||
| expect(component.instance().state.types).toHaveLength(5); | ||
| component.instance().removeMe(1); | ||
| expect(component.instance().state.types).toHaveLength(4); | ||
| test('onRemoveClick is passed to the Label', () => { | ||
| const onRemoveClick = jest.fn(); | ||
| const component = shallow(<DisposableLabel onRemoveClick={onRemoveClick} />); | ||
| expect(component.find(Label).props().onRemoveClick).toBe(onRemoveClick); | ||
| }); | ||
This file contains hidden or 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,17 +1,31 @@ | ||
| import React from 'react'; | ||
| import { Label as BsLabel } from 'react-bootstrap'; | ||
| import DisposableLabel from './DisposableLabel'; | ||
| import PropTypes from 'prop-types'; | ||
| import RemoveButton from './RemoveButton'; | ||
|
|
||
| const Label = props => { | ||
| if (props.onRemoveClick) { | ||
| return <DisposableLabel {...props} />; | ||
| } | ||
| return <BsLabel {...props} />; | ||
| }; | ||
| const Label = ({ children, onRemoveClick, type, ...rest }) => ( | ||
| <BsLabel bsStyle={type} {...rest}> | ||
| {children} | ||
| {!!onRemoveClick && ( | ||
| <RemoveButton onRemoveClick={onRemoveClick} title="Remove" /> | ||
| )} | ||
| </BsLabel> | ||
| ); | ||
|
|
||
| Label.propTypes = { | ||
| ...BsLabel.propTypes, | ||
| ...DisposableLabel.propTypes | ||
| /** Children nodes */ | ||
| children: PropTypes.node, | ||
| /** Label type */ | ||
| type: PropTypes.string, | ||
| /** callback when Label is removed */ | ||
| onRemoveClick: PropTypes.func | ||
| }; | ||
|
|
||
| Label.defaultProps = { | ||
| children: null, | ||
| type: 'default', | ||
| onRemoveClick: undefined | ||
| }; | ||
|
|
||
| export default Label; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import React from 'react'; | ||
| import { shallow } from 'enzyme'; | ||
| import { Label as BsLabel } from 'react-bootstrap'; | ||
| import Label from './Label'; | ||
| import RemoveButton from './RemoveButton'; | ||
|
|
||
| it('sets bsStyle to type', () => { | ||
| const type = 'primary'; | ||
| const view = shallow(<Label type={type} />); | ||
| expect(view.find(BsLabel).props().bsStyle).toBe(type); | ||
| }); | ||
|
|
||
| it('spreads additional props to Bootstrap Label', () => { | ||
| const view = shallow(<Label id="id" another="another" />); | ||
| expect(view.find(BsLabel).props()).toMatchSnapshot( | ||
| 'Bootstrap label has correct props.' | ||
| ); | ||
| }); | ||
|
|
||
| it('does not render RemoveButton if onRemoveClick is not present', () => { | ||
| const view = shallow(<Label />); | ||
| expect(view.find(RemoveButton).exists()).toBe(false); | ||
| }); | ||
|
|
||
| it('renders RemoveButton if onRemoveClick is present', () => { | ||
| const view = shallow(<Label onRemoveClick={jest.fn()} />); | ||
| expect(view.find(RemoveButton).exists()).toBe(true); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import React from 'react'; | ||
| import { shallow } from 'enzyme'; | ||
| import RemoveButton from './RemoveButton'; | ||
|
|
||
| it('calls preventDefault on click', () => { | ||
| const preventDefault = jest.fn(); | ||
| const view = shallow(<RemoveButton />); | ||
| view.find('a').simulate('click', { preventDefault }); | ||
| expect(preventDefault).toBeCalled(); | ||
| }); | ||
|
|
||
| it('handles onRemoveClick having a falsy value', () => { | ||
| const preventDefault = jest.fn(); | ||
| const view = shallow(<RemoveButton onRemoveClick={null} />); | ||
| expect(() => { | ||
| view.find('a').simulate('click', { preventDefault }); | ||
| expect(preventDefault).toBeCalled(); | ||
| }).not.toThrow(); | ||
| }); | ||
|
|
||
| it('spreads additional props and className to icon', () => { | ||
| const view = shallow( | ||
| <RemoveButton id="id" another="another" className="additional classes" /> | ||
| ); | ||
| expect(view).toMatchSnapshot('Icon has correct props.'); | ||
| }); |
25 changes: 5 additions & 20 deletions
25
src/components/Label/__snapshots__/DisposableLabel.test.js.snap
This file contains hidden or 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,23 +1,8 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`Label renders properly 1`] = ` | ||
| <span | ||
| class="label label-default" | ||
| > | ||
| Some text | ||
| <a | ||
| class="pf-remove-button" | ||
| href="#" | ||
| > | ||
| <span | ||
| aria-hidden="true" | ||
| class="pficon pficon-close" | ||
| /> | ||
| <span | ||
| class="sr-only" | ||
| > | ||
| Remove | ||
| </span> | ||
| </a> | ||
| </span> | ||
| exports[`defaults props 1`] = ` | ||
| <Label | ||
| onRemoveClick={[Function]} | ||
| type="default" | ||
| /> | ||
| `; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`spreads additional props to Bootstrap Label: Bootstrap label has correct props. 1`] = ` | ||
| Object { | ||
| "another": "another", | ||
| "bsClass": "label", | ||
| "bsStyle": "default", | ||
| "children": Array [ | ||
| null, | ||
| false, | ||
| ], | ||
| "id": "id", | ||
| } | ||
| `; |
21 changes: 21 additions & 0 deletions
21
src/components/Label/__snapshots__/RemoveButton.test.js.snap
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`spreads additional props and className to icon: Icon has correct props. 1`] = ` | ||
| <a | ||
| className="pf-remove-button" | ||
| href="#" | ||
| onClick={[Function]} | ||
| > | ||
| <span | ||
| another="another" | ||
| aria-hidden="true" | ||
| className="pficon pficon-close additional classes" | ||
| id="id" | ||
| /> | ||
| <span | ||
| className="sr-only" | ||
| > | ||
| Remove | ||
| </span> | ||
| </a> | ||
| `; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you want to start using names?
i.e.
toMatchSnapshot('default props snapshot'));? or fine w/ leaving those out? sorry was confused and started using them ;)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It definitely does not hurt. The real benefit comes from when there are multiple snapshots in 1 test. By default, they will get the test's name. If there are multiple you just get a 1 2 3 etc. I think it would be a good idea though if we are going to request them for multiple to do them for single snapshots as well. That should cut down on the question of "when should I name the snapshot". Since it will be always it is easier to remember. Will update these.