@@ -2,53 +2,51 @@ import React from 'react';
2
2
import { mount } from 'enzyme' ;
3
3
import { expect } from 'chai' ;
4
4
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
+ }
6
45
7
46
/* eslint-disable no-undef */
8
47
describe ( 'Snackbar Render tests' , ( ) => {
9
48
let wrappedComponent ;
10
49
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
-
52
50
beforeEach ( ( ) => {
53
51
wrappedComponent = mount ( < SnackDisplay /> ) ;
54
52
} ) ;
@@ -80,4 +78,31 @@ describe('Snackbar Render tests', () => {
80
78
wrappedComponent . find ( 'button' ) . simulate ( 'click' ) ;
81
79
expect ( simulatedValue ( ) ) . equals ( expectedValueAfterDisplay ) ;
82
80
} ) ;
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
+ } ) ;
83
108
} ) ;
0 commit comments