@@ -4,68 +4,26 @@ import TransitionablePortal from 'src/addons/TransitionablePortal/Transitionable
4
4
import * as common from 'test/specs/commonTests'
5
5
import { domEvent , sandbox , assertWithTimeout } from 'test/utils'
6
6
7
- // ----------------------------------------
8
- // Wrapper
9
- // ----------------------------------------
10
- let wrapper
11
-
12
- // we need to unmount the modal after every test to remove it from the document
13
- // wrap the render methods to update a global wrapper that is unmounted after each test
14
- const wrapperMount = ( ...args ) => ( wrapper = mount ( ...args ) )
15
- const wrapperShallow = ( ...args ) => ( wrapper = shallow ( ...args ) )
16
-
17
7
const quickTransition = { duration : 0 }
18
8
const requiredProps = {
19
- children : < div /> ,
9
+ children : < div id = 'children' /> ,
20
10
}
21
11
22
12
describe ( 'TransitionablePortal' , ( ) => {
23
- beforeEach ( ( ) => {
24
- wrapper = undefined
25
- document . body . innerHTML = ''
26
- } )
27
-
28
- afterEach ( ( ) => {
29
- if ( wrapper && wrapper . unmount ) wrapper . unmount ( )
30
- } )
31
-
32
13
common . isConformant ( TransitionablePortal , { requiredProps } )
33
14
34
15
describe ( 'children' , ( ) => {
35
- it ( 'renders a Portal' , ( ) => {
36
- wrapperShallow ( < TransitionablePortal { ...requiredProps } /> ) . should . have . descendants ( 'Portal' )
37
- } )
38
-
39
16
it ( 'renders a Transition' , ( ) => {
40
- wrapperShallow ( < TransitionablePortal { ...requiredProps } /> ) . should . have . descendants (
41
- 'Transition' ,
42
- )
43
- } )
44
- } )
45
-
46
- describe ( 'getDerivedStateFromProps' , ( ) => {
47
- it ( 'passes `open` prop to `portalOpen` when defined' , ( ) => {
48
- wrapperMount ( < TransitionablePortal { ...requiredProps } /> )
17
+ const wrapper = mount ( < TransitionablePortal { ...requiredProps } open /> )
49
18
50
- wrapper . setProps ( { open : true } )
51
- wrapper . should . have . state ( 'portalOpen' , true )
52
- wrapper . setProps ( { open : false } )
53
- wrapper . should . have . state ( 'portalOpen' , false )
54
- } )
55
-
56
- it ( 'does not pass `open` prop to `portalOpen` when not defined' , ( ) => {
57
- wrapperMount ( < TransitionablePortal { ...requiredProps } /> )
58
-
59
- wrapper . setProps ( { transition : { } } )
60
- wrapper . should . have . not . state ( 'portalOpen' )
19
+ wrapper . should . have . descendants ( '.transition' )
61
20
} )
62
21
} )
63
22
64
23
describe ( 'onClose' , ( ) => {
65
- it ( 'is called with (null, data) when Portal closes ' , ( done ) => {
24
+ it ( 'is called with (null, data) on a click outside ' , ( done ) => {
66
25
const onClose = sandbox . spy ( )
67
-
68
- wrapperMount (
26
+ const wrapper = mount (
69
27
< TransitionablePortal
70
28
{ ...requiredProps }
71
29
onClose = { onClose }
@@ -80,36 +38,33 @@ describe('TransitionablePortal', () => {
80
38
assertWithTimeout ( ( ) => {
81
39
onClose . should . have . been . calledOnce ( )
82
40
onClose . should . have . been . calledWithMatch ( null , { portalOpen : false } )
41
+
42
+ wrapper . unmount ( )
83
43
} , done )
84
44
} )
85
45
86
- it ( 'changes `portalOpen` to false' , ( ) => {
87
- wrapperMount (
88
- < TransitionablePortal
89
- { ...requiredProps }
90
- transition = { quickTransition }
91
- trigger = { < button /> }
92
- /> ,
93
- )
46
+ it ( 'hides contents on a click outside' , ( ) => {
47
+ const wrapper = mount ( < TransitionablePortal { ...requiredProps } trigger = { < button /> } /> )
94
48
95
49
wrapper . find ( 'button' ) . simulate ( 'click' )
96
- domEvent . click ( document . body )
50
+ wrapper . should . have . descendants ( '.in#children' )
97
51
98
- wrapper . should . have . state ( 'portalOpen' , false )
52
+ domEvent . click ( document . body )
53
+ wrapper . update ( )
54
+ wrapper . should . have . descendants ( '.out#children' )
99
55
} )
100
56
} )
101
57
102
58
describe ( 'onHide' , ( ) => {
103
59
it ( 'is called with (null, data) when exiting transition finished' , ( done ) => {
104
60
const onHide = sandbox . spy ( )
105
- const trigger = < button />
106
- wrapperMount (
61
+ const wrapper = mount (
107
62
< TransitionablePortal
108
63
{ ...requiredProps }
109
64
onHide = { onHide }
110
65
open
111
66
transition = { quickTransition }
112
- trigger = { trigger }
67
+ trigger = { < button /> }
113
68
/> ,
114
69
)
115
70
@@ -121,37 +76,58 @@ describe('TransitionablePortal', () => {
121
76
portalOpen : false ,
122
77
transitionVisible : false ,
123
78
} )
79
+
80
+ wrapper . unmount ( )
124
81
} , done )
125
82
} )
126
83
} )
127
84
128
85
describe ( 'onOpen' , ( ) => {
129
- it ( 'is called with (null, data) when Portal opens' , ( ) => {
86
+ it ( 'is called with (null, data) when opens' , ( ) => {
130
87
const onOpen = sandbox . spy ( )
88
+ const wrapper = mount (
89
+ < TransitionablePortal { ...requiredProps } onOpen = { onOpen } trigger = { < button /> } /> ,
90
+ )
131
91
132
- wrapperMount ( < TransitionablePortal { ...requiredProps } onOpen = { onOpen } trigger = { < button /> } /> )
133
92
wrapper . find ( 'button' ) . simulate ( 'click' )
134
-
135
93
onOpen . should . have . been . calledOnce ( )
136
94
onOpen . should . have . been . calledWithMatch ( null , { portalOpen : true } )
137
95
} )
138
96
139
- it ( 'changes `portalOpen` to true ' , ( ) => {
140
- wrapperMount ( < TransitionablePortal { ...requiredProps } trigger = { < button /> } /> )
97
+ it ( 'renders contents ' , ( ) => {
98
+ const wrapper = mount ( < TransitionablePortal { ...requiredProps } trigger = { < button /> } /> )
141
99
142
100
wrapper . find ( 'button' ) . simulate ( 'click' )
143
- wrapper . should . have . state ( 'portalOpen' , true )
101
+ wrapper . should . have . descendants ( '.in#children' )
144
102
} )
145
103
} )
146
104
147
105
describe ( 'open' , ( ) => {
148
- it ( 'does not block update of state on Portal close' , ( ) => {
149
- wrapperMount ( < TransitionablePortal { ...requiredProps } open /> )
150
- wrapper . should . have . state ( 'portalOpen' , true )
151
- wrapper . update ( )
106
+ it ( 'does not block update of state on a portal close' , ( ) => {
107
+ const wrapper = mount ( < TransitionablePortal { ...requiredProps } open /> )
108
+ wrapper . should . have . descendants ( '.in#children' )
152
109
153
110
domEvent . click ( document . body )
154
- wrapper . should . have . state ( 'portalOpen' , false )
111
+ wrapper . update ( )
112
+ wrapper . should . have . descendants ( '.out#children' )
113
+ } )
114
+
115
+ it ( 'passes `open` prop to Transition when defined' , ( ) => {
116
+ const wrapper = mount ( < TransitionablePortal { ...requiredProps } /> )
117
+
118
+ wrapper . setProps ( { open : true } )
119
+ wrapper . should . have . descendants ( '.in#children' )
120
+
121
+ wrapper . setProps ( { open : false } )
122
+ wrapper . should . have . descendants ( '.out#children' )
123
+ } )
124
+
125
+ it ( 'does not pass `open` prop to Transition when not defined' , ( ) => {
126
+ const wrapper = mount ( < TransitionablePortal { ...requiredProps } /> )
127
+ wrapper . should . have . not . descendants ( '#children' )
128
+
129
+ wrapper . setProps ( { transition : { } } )
130
+ wrapper . should . have . not . descendants ( '#children' )
155
131
} )
156
132
} )
157
133
} )
0 commit comments