Skip to content

Commit a84df68

Browse files
committed
feat(Fade): enable fading components
1 parent 5dadc6f commit a84df68

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

lib/Fade.jsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React, { PropTypes } from 'react';
2+
import classNames from 'classnames';
3+
4+
const propTypes = {
5+
className: PropTypes.string
6+
};
7+
8+
const defaultProps = {};
9+
10+
class Fade extends React.Component {
11+
constructor(props) {
12+
super(props);
13+
14+
this.state = {
15+
fadeIn: false
16+
};
17+
18+
this.fade = this.fade.bind(this);
19+
this.fadeIn = this.fadeIn.bind(this);
20+
this.fadeOut = this.fadeOut.bind(this);
21+
}
22+
23+
fade(fade, cb, delay) {
24+
this.setState({
25+
fadeIn: fade
26+
});
27+
28+
if (cb) {
29+
setTimeout(cb, delay);
30+
}
31+
}
32+
33+
fadeIn(cb, delay) {
34+
this.fade(true, cb, delay);
35+
}
36+
37+
fadeOut(cb, delay) {
38+
this.fade(false, cb, delay);
39+
}
40+
41+
render() {
42+
const classes = classNames(
43+
this.props.className,
44+
'fade',
45+
{
46+
in: this.state.fadeIn
47+
}
48+
);
49+
50+
return (
51+
<div {...this.props} className={classes} />
52+
);
53+
}
54+
}
55+
56+
Fade.propTypes = propTypes;
57+
Fade.defaultProps = defaultProps;
58+
59+
export default Fade;

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Dropdown from './Dropdown';
66
import DropdownItem from './DropdownItem';
77
import DropdownMenu from './DropdownMenu';
88
import DropdownToggle from './DropdownToggle';
9+
import Fade from './Fade';
910
import Label from './Label';
1011
import Popover from './Popover';
1112
import PopoverTitle from './PopoverTitle';
@@ -22,6 +23,7 @@ export {
2223
DropdownItem,
2324
DropdownMenu,
2425
DropdownToggle,
26+
Fade,
2527
Label,
2628
Popover,
2729
PopoverContent,

test/Fade.spec.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
2+
import React from 'react';
3+
import ReactDOM from 'react-dom';
4+
import { shallow, mount } from 'enzyme';
5+
import { Fade } from '../lib';
6+
7+
describe('Fade', () => {
8+
it('should render with "fade" class', () => {
9+
const wrapper = shallow(<Fade>Yo!</Fade>);
10+
11+
expect(wrapper.hasClass('fade')).toBe(true);
12+
expect(wrapper.hasClass('in')).toBe(false);
13+
expect(wrapper.text()).toBe('Yo!');
14+
});
15+
16+
it('should render additional classes', () => {
17+
const wrapper = shallow(<Fade className="other">Yo!</Fade>);
18+
19+
expect(wrapper.hasClass('other')).toBe(true);
20+
expect(wrapper.hasClass('fade')).toBe(true);
21+
expect(wrapper.hasClass('in')).toBe(false);
22+
});
23+
24+
it('should toggle "in" class with fadeIn & fadeOut', () => {
25+
const wrapper = mount(<Fade>Yo!</Fade>);
26+
const instance = wrapper.instance();
27+
28+
expect(wrapper.hasClass('in')).toBe(false);
29+
30+
instance.fadeIn();
31+
// hasClass does not pick up updates
32+
// maybe related https://github.com/airbnb/enzyme/issues/134
33+
expect(ReactDOM.findDOMNode(instance).className).toBe('fade in');
34+
35+
instance.fadeOut();
36+
37+
expect(ReactDOM.findDOMNode(instance).className).toBe('fade');
38+
});
39+
40+
it('should call fadeIn & fadeOut with delayed callbacks', () => {
41+
jasmine.clock().install();
42+
43+
const fadeInCallback = jasmine.createSpy('spy');
44+
const fadeOutCallback = jasmine.createSpy('spy');
45+
const wrapper = mount(<Fade>Yo!</Fade>);
46+
const instance = wrapper.instance();
47+
48+
expect(wrapper.hasClass('in')).toBe(false);
49+
50+
instance.fadeIn(fadeInCallback, 250);
51+
expect(fadeInCallback).not.toHaveBeenCalled();
52+
53+
jasmine.clock().tick(250);
54+
expect(fadeInCallback).toHaveBeenCalled();
55+
56+
instance.fadeOut(fadeOutCallback, 250);
57+
expect(fadeOutCallback).not.toHaveBeenCalled();
58+
59+
jasmine.clock().tick(250);
60+
expect(fadeOutCallback).toHaveBeenCalled();
61+
62+
jasmine.clock().uninstall();
63+
});
64+
});

0 commit comments

Comments
 (0)