Skip to content

Commit c441b51

Browse files
authored
Add tests (#22)
1 parent 735b45b commit c441b51

File tree

1 file changed

+136
-5
lines changed

1 file changed

+136
-5
lines changed

test/__tests__/notifications.js

Lines changed: 136 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,155 @@
11
import React from 'react';
2-
import { shallow } from 'enzyme';
2+
import { mount } from 'enzyme';
33
import { expect } from 'chai';
4+
import { jsdom } from 'jsdom';
45
import sinon from 'sinon';
56
import Component from '../../src/notifications';
67
import NotifySystem from 'react-notification-system';
78

9+
const createDOM = () => jsdom('<!doctype html><html><body><div></div></body></html>');
10+
811
describe('NotificationsComponent', () => {
12+
let DOM;
13+
14+
const notification = {
15+
title: 'Hey, it\'s good to see you!',
16+
message: 'Now you can see how easy it is to use notifications in React!',
17+
dismissible: false,
18+
level: 'info',
19+
uid: 'demo-uid',
20+
autoDismiss: 5,
21+
};
22+
23+
const mountComponent = props => mount(
24+
<Component
25+
notifications={[]}
26+
{...props}
27+
/>, {
28+
attachTo: DOM.body.firstChild,
29+
context: {
30+
store: {
31+
dispatch: () => {}
32+
}
33+
}
34+
}
35+
);
36+
37+
beforeEach(() => {
38+
DOM = createDOM();
39+
});
40+
941
it('should render one <NotifySystem /> component', () => {
10-
const wrapper = shallow(<Component />);
11-
expect(wrapper.children()).to.exist;
42+
const wrapper = mountComponent();
43+
expect(wrapper.find(NotifySystem).length).to.equal(1);
1244
});
1345

1446
it('should warn if prop:notifications is not array', () => {
1547
const c = sinon.stub(console, 'error');
1648

17-
const wrapper = shallow(<Component notifications={1} />);
49+
const wrapper = mountComponent({ notifications: 1 });
1850
const warning = c.args[0][0];
1951

52+
c.restore();
53+
2054
expect(warning).to.match(/Invalid prop `notifications` of type `number` supplied to `Notifications`, expected `array`./);
55+
});
2156

22-
c.restore();
57+
it('should render a single notification', () => {
58+
const wrapper = mountComponent();
59+
60+
wrapper.setProps({
61+
notifications: [notification]
62+
});
63+
64+
expect(wrapper.html()).to.have.string(notification.title);
65+
expect(wrapper.html()).to.have.string(notification.message);
66+
});
67+
68+
it('calls onRemove once the notification is auto dismissed', (done) => {
69+
const wrapper = mountComponent();
70+
const onRemove = sinon.spy();
71+
72+
wrapper.setProps({
73+
notifications: [{
74+
...notification,
75+
autoDismiss: 1,
76+
onRemove
77+
}]
78+
});
79+
80+
setTimeout(() => {
81+
expect(onRemove.called).to.be.true;
82+
done();
83+
}, 1100);
84+
});
85+
86+
it('calls onRemove once the notification is manually dismissed', (done) => {
87+
const wrapper = mountComponent();
88+
const onRemove = sinon.spy();
89+
const onCallback = sinon.spy();
90+
91+
wrapper.setProps({
92+
notifications: [{
93+
...notification,
94+
autoDismiss: 0,
95+
action: {
96+
label: 'Dismiss',
97+
callback: onCallback
98+
},
99+
onRemove
100+
}]
101+
});
102+
103+
wrapper.find('button').simulate('click');
104+
105+
setTimeout(() => {
106+
expect(onCallback.called).to.be.true;
107+
expect(onRemove.called).to.be.true;
108+
done();
109+
}, 50);
110+
});
111+
112+
it('calls onRemove once the notification is auto dismissed while style is false', (done) => {
113+
const wrapper = mountComponent({ style: false });
114+
const onRemove = sinon.spy();
115+
116+
wrapper.setProps({
117+
notifications: [{
118+
...notification,
119+
autoDismiss: 1,
120+
onRemove
121+
}]
122+
});
123+
124+
setTimeout(() => {
125+
expect(onRemove.called).to.be.true;
126+
done();
127+
}, 1100);
128+
});
129+
130+
it('calls onRemove once the notification is manually dismissed while style is false', (done) => {
131+
const wrapper = mountComponent({ style: false });
132+
const onRemove = sinon.spy();
133+
const onCallback = sinon.spy();
134+
135+
wrapper.setProps({
136+
notifications: [{
137+
...notification,
138+
autoDismiss: 0,
139+
action: {
140+
label: 'Dismiss',
141+
callback: onCallback
142+
},
143+
onRemove
144+
}]
145+
});
146+
147+
wrapper.find('button').simulate('click');
148+
149+
setTimeout(() => {
150+
expect(onCallback.called).to.be.true;
151+
expect(onRemove.called).to.be.true;
152+
done();
153+
}, 50);
23154
});
24155
});

0 commit comments

Comments
 (0)