Skip to content
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
21 changes: 4 additions & 17 deletions src/components/Label/DisposableLabel.js
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;
25 changes: 12 additions & 13 deletions src/components/Label/DisposableLabel.test.js
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();

@priley86 priley86 Apr 2, 2018 •

Copy link
Copy Markdown
Member

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 ;)

Copy link
Copy Markdown
Contributor Author

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.

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);
});
30 changes: 22 additions & 8 deletions src/components/Label/Label.js
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;
28 changes: 28 additions & 0 deletions src/components/Label/Label.test.js
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);
});
26 changes: 26 additions & 0 deletions src/components/Label/RemoveButton.test.js
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 src/components/Label/__snapshots__/DisposableLabel.test.js.snap
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"
/>
`;
14 changes: 14 additions & 0 deletions src/components/Label/__snapshots__/Label.test.js.snap
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 src/components/Label/__snapshots__/RemoveButton.test.js.snap
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>
`;