Skip to content

Commit 440f889

Browse files
committed
✅ Add more tests for snackbar
improves coverage
1 parent b5cc90a commit 440f889

File tree

1 file changed

+67
-42
lines changed

1 file changed

+67
-42
lines changed

lib/snackbar/tests/render.test.js

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,51 @@ import React from 'react';
22
import { mount } from 'enzyme';
33
import { expect } from 'chai';
44
import Snackbar from '..';
5-
import Button from '../../button';
5+
6+
// eslint-disable-next-line import/prefer-default-export
7+
export class SnackDisplay extends React.Component {
8+
constructor(props) {
9+
super(props);
10+
this.state = {
11+
showSnackbar: false,
12+
};
13+
this.openBottomSnackbar = this.openBottomSnackbar.bind(this);
14+
this.handleSnackClose = this.handleSnackClose.bind(this);
15+
}
16+
17+
openBottomSnackbar() {
18+
this.setState({
19+
showSnackbar: !this.state.showSnackbar,
20+
});
21+
}
22+
23+
handleSnackClose() {
24+
this.setState({
25+
showSnackbar: false,
26+
});
27+
}
28+
29+
render() {
30+
return (
31+
<div>
32+
<Snackbar
33+
active={this.state.showSnackbar}
34+
onClose={this.handleSnackClose}
35+
>
36+
<span>This is a bottom snackbar.</span>
37+
</Snackbar>
38+
<button onClick={this.openBottomSnackbar}>
39+
Open Bottom Snackbar
40+
</button>
41+
</div>
42+
);
43+
}
44+
}
645

746
/* eslint-disable no-undef */
847
describe('Snackbar Render tests', () => {
948
let wrappedComponent;
1049

11-
class SnackDisplay extends React.Component {
12-
constructor(props) {
13-
super(props);
14-
this.state = {
15-
showSnackbar: false,
16-
};
17-
this.openBottomSnackbar = this.openBottomSnackbar.bind(this);
18-
this.handleSnackClose = this.handleSnackClose.bind(this);
19-
}
20-
21-
openBottomSnackbar() {
22-
this.setState({
23-
showSnackbar: !this.state.showSnackbar,
24-
});
25-
}
26-
27-
handleSnackClose() {
28-
this.setState({
29-
showSnackbar: false,
30-
});
31-
}
32-
33-
render() {
34-
return (
35-
<div>
36-
<Snackbar
37-
active={this.state.showSnackbar}
38-
onClose={this.handleSnackClose}
39-
>
40-
<span>This is a bottom snackbar.</span>
41-
</Snackbar>
42-
<div style={{ margin: '10px' }}>
43-
<Button type="primary" onClick={this.openBottomSnackbar}>
44-
Open Bottom Snackbar
45-
</Button>
46-
</div>
47-
</div>
48-
);
49-
}
50-
}
51-
5250
beforeEach(() => {
5351
wrappedComponent = mount(<SnackDisplay />);
5452
});
@@ -80,4 +78,31 @@ describe('Snackbar Render tests', () => {
8078
wrappedComponent.find('button').simulate('click');
8179
expect(simulatedValue()).equals(expectedValueAfterDisplay);
8280
});
81+
82+
it('Successfully renders snackbar.', () => {
83+
const simulatedValue = () => wrappedComponent.find('div.active').length;
84+
85+
wrappedComponent.setState({ showSnackbar: false });
86+
wrappedComponent.setState({ showSnackbar: true });
87+
expect(simulatedValue()).equals(1);
88+
setTimeout(() => {
89+
expect(simulatedValue()).equals(0);
90+
done();
91+
}, 2000);
92+
});
93+
94+
it('Should render snackbar by default when active prop is set to true by default', () => {
95+
const expectedValueBefore = true;
96+
const expectedValueAfter = false;
97+
const simulatedValue = () => wrappedComponent
98+
.find(Snackbar)
99+
.childAt(0)
100+
.prop('active');
101+
102+
wrappedComponent = mount(<Snackbar active />);
103+
104+
expect(simulatedValue()).to.equal(expectedValueBefore);
105+
wrappedComponent.setProps({ active: false });
106+
expect(simulatedValue()).equals(expectedValueAfter);
107+
});
83108
});

0 commit comments

Comments
 (0)