1
1
/* eslint-env jest */
2
2
3
3
import React , { Component } from 'react'
4
- import Adapter from 'enzyme-adapter-react-16'
5
- import { mount , configure } from 'enzyme'
4
+ import { mount } from 'enzyme'
6
5
import '../src/patch.dev'
7
6
import AppContainer from '../src/AppContainer.dev'
8
7
import { didUpdate } from '../src/updateCounter'
9
8
import getReactStack from '../src/internal/getReactStack'
10
9
import { areComponentsEqual } from '../src/utils.dev'
11
10
import RHL from '../src/reactHotLoader'
12
11
13
- configure ( { adapter : new Adapter ( ) } )
14
-
15
- const createTestDouble = ( render , name , key ) => {
12
+ const spyComponent = ( render , displayName , key ) => {
16
13
const mounted = jest . fn ( )
17
14
const unmounted = jest . fn ( )
15
+ const willUpdate = jest . fn ( )
18
16
19
17
class TestingComponent extends Component {
20
18
componentWillMount ( ) {
21
19
mounted ( )
22
20
}
23
21
22
+ componentWillUpdate ( nextProps , nextState ) {
23
+ willUpdate ( nextProps , nextState , this . props , this . state )
24
+ }
25
+
24
26
componentWillUnmount ( ) {
25
27
unmounted ( )
26
28
}
@@ -36,11 +38,13 @@ const createTestDouble = (render, name, key) => {
36
38
}
37
39
}
38
40
39
- if ( name ) {
40
- TestingComponent . displayName = name
41
+ if ( displayName ) {
42
+ TestingComponent . displayName = displayName
41
43
}
44
+
42
45
return {
43
46
Component : TestingComponent ,
47
+ willUpdate,
44
48
mounted,
45
49
unmounted,
46
50
key,
@@ -50,38 +54,36 @@ const createTestDouble = (render, name, key) => {
50
54
describe ( 'reconciler' , ( ) => {
51
55
describe ( 'Application' , ( ) => {
52
56
it ( 'should regenerate internal component' , ( ) => {
53
- const ComponentRoot = createTestDouble (
57
+ const root = spyComponent (
54
58
( { children } ) => < div > { children } </ div > ,
55
59
'root' ,
56
60
'root' ,
57
61
)
58
62
59
- const Component1 = createTestDouble (
63
+ const first = spyComponent (
60
64
( { children } ) => < b > { children } </ b > ,
61
65
'test' ,
62
66
'1' ,
63
67
)
64
- const Component2 = createTestDouble ( ( ) => < u > REPLACED</ u > , 'test' , '2' )
65
- const Component3 = createTestDouble (
66
- ( ) => < u > NEW ONE</ u > ,
67
- 'somethingElse' ,
68
- '3' ,
69
- )
68
+ const second = spyComponent ( ( ) => < u > REPLACED</ u > , 'test' , '2' )
69
+ const third = spyComponent ( ( ) => < u > NEW ONE</ u > , 'somethingElse' , '3' )
70
+
71
+ let currentComponent = first
72
+ const currentProps = { }
70
73
71
- let currentComponent = Component1
72
74
const ComponentSwap = props => {
73
75
const { Component } = currentComponent
74
76
return (
75
77
< blockquote >
76
- < Component { ...props } />
78
+ < Component { ...props } { ... currentProps } />
77
79
</ blockquote >
78
80
)
79
81
}
80
82
81
83
const App = ( ) => (
82
- < ComponentRoot . Component >
84
+ < root . Component >
83
85
< ComponentSwap > 42</ ComponentSwap >
84
- </ ComponentRoot . Component >
86
+ </ root . Component >
85
87
)
86
88
87
89
const wrapper = mount (
@@ -91,56 +93,65 @@ describe('reconciler', () => {
91
93
)
92
94
93
95
// mount and perform first checks
94
- expect ( wrapper . find ( < Component1 . Component /> . type ) . length ) . toBe ( 1 )
95
- expect ( ComponentRoot . mounted ) . toHaveBeenCalledTimes ( 1 )
96
- expect ( Component1 . mounted ) . toHaveBeenCalledTimes ( 1 )
96
+ expect ( wrapper . find ( < first . Component /> . type ) . length ) . toBe ( 1 )
97
+ expect ( root . mounted ) . toHaveBeenCalledTimes ( 1 )
98
+ expect ( first . mounted ) . toHaveBeenCalledTimes ( 1 )
97
99
98
100
// replace with `the same` component
99
- currentComponent = Component2
101
+ currentComponent = second
100
102
// they are different
101
- expect (
102
- areComponentsEqual ( Component1 . Component , Component2 . Component ) ,
103
- ) . toBe ( false )
103
+ expect ( areComponentsEqual ( first . Component , second . Component ) ) . toBe ( false )
104
+
105
+ currentProps . newProp = true
104
106
didUpdate ( )
105
107
wrapper . setProps ( { update : 'now' } )
106
108
// not react-stand-in merge them together
107
- expect (
108
- areComponentsEqual ( Component1 . Component , Component2 . Component ) ,
109
- ) . toBe ( true )
110
- expect ( wrapper . find ( < Component1 . Component /> . type ) . length ) . toBe ( 1 )
111
- expect ( wrapper . find ( < Component2 . Component /> . type ) . length ) . toBe ( 1 )
112
-
113
- expect ( ComponentRoot . mounted ) . toHaveBeenCalledTimes ( 1 )
114
- expect ( Component1 . unmounted ) . toHaveBeenCalledTimes ( 0 )
115
- expect ( Component2 . mounted ) . toHaveBeenCalledTimes ( 0 )
109
+ expect ( areComponentsEqual ( first . Component , second . Component ) ) . toBe ( true )
110
+ expect ( wrapper . find ( < first . Component /> . type ) . length ) . toBe ( 1 )
111
+ expect ( wrapper . find ( < second . Component /> . type ) . length ) . toBe ( 1 )
112
+
113
+ expect ( root . mounted ) . toHaveBeenCalledTimes ( 1 )
114
+ expect ( first . unmounted ) . toHaveBeenCalledTimes ( 0 )
115
+ expect ( second . mounted ) . toHaveBeenCalledTimes ( 0 )
116
+ expect ( second . willUpdate ) . toHaveBeenCalledTimes ( 2 )
117
+ expect ( second . willUpdate . mock . calls [ 0 ] ) . toEqual ( [
118
+ { children : '42' } ,
119
+ null ,
120
+ { children : '42' } ,
121
+ null ,
122
+ ] )
123
+ expect ( second . willUpdate . mock . calls [ 1 ] ) . toEqual ( [
124
+ { children : '42' , newProp : true } ,
125
+ null ,
126
+ { children : '42' } ,
127
+ null ,
128
+ ] )
116
129
117
130
// replace with a different component
118
- currentComponent = Component3
131
+ currentComponent = third
119
132
didUpdate ( )
120
133
wrapper . setProps ( { update : 'now' } )
121
- expect ( wrapper . find ( < Component3 . Component /> . type ) . length ) . toBe ( 1 )
122
- // Component1 will never be unmounted
123
- expect ( Component1 . unmounted ) . toHaveBeenCalledTimes ( 0 )
124
- expect ( Component2 . unmounted ) . toHaveBeenCalledTimes ( 1 )
125
- expect ( Component3 . mounted ) . toHaveBeenCalledTimes ( 1 )
126
-
127
- expect (
128
- areComponentsEqual ( Component1 . Component , Component3 . Component ) ,
129
- ) . toBe ( false )
130
- expect (
131
- areComponentsEqual ( Component2 . Component , Component3 . Component ) ,
132
- ) . toBe ( false )
134
+ expect ( wrapper . find ( < third . Component /> . type ) . length ) . toBe ( 1 )
135
+ // first will never be unmounted
136
+ expect ( first . unmounted ) . toHaveBeenCalledTimes ( 0 )
137
+ expect ( second . unmounted ) . toHaveBeenCalledTimes ( 1 )
138
+ expect ( third . mounted ) . toHaveBeenCalledTimes ( 1 )
139
+
140
+ expect ( areComponentsEqual ( first . Component , third . Component ) ) . toBe ( false )
141
+ expect ( areComponentsEqual ( second . Component , third . Component ) ) . toBe ( false )
133
142
} )
134
143
135
144
it ( 'should regenerate internal component' , ( ) => {
136
- const Component1 = createTestDouble (
145
+ const first = spyComponent (
137
146
( { children } ) => < b > { children } </ b > ,
138
147
'test' ,
139
148
'1' ,
140
149
)
141
- const Component2 = createTestDouble ( ( ) => < u > REPLACED</ u > , 'test' , '2' )
142
150
143
- let currentComponent = Component1
151
+ const second = spyComponent ( ( ) => < u > REPLACED</ u > , 'test' , '2' )
152
+
153
+ let currentComponent = first
154
+
144
155
const ComponentSwap = props => {
145
156
const { Component } = currentComponent
146
157
return (
@@ -160,32 +171,32 @@ describe('reconciler', () => {
160
171
)
161
172
162
173
const wrapper = mount (
163
- < AppContainer reconciler >
174
+ < AppContainer >
164
175
< App />
165
176
</ AppContainer > ,
166
177
)
167
178
168
- currentComponent = Component2
179
+ currentComponent = second
169
180
didUpdate ( )
170
181
wrapper . setProps ( { update : 'now' } )
171
182
172
- expect ( Component1 . unmounted ) . toHaveBeenCalledTimes ( 0 )
173
- expect ( Component2 . mounted ) . toHaveBeenCalledTimes ( 0 )
183
+ expect ( first . unmounted ) . toHaveBeenCalledTimes ( 0 )
184
+ expect ( second . mounted ) . toHaveBeenCalledTimes ( 0 )
174
185
} )
175
186
176
187
it ( 'should handle function as a child' , ( ) => {
177
- const Component1 = createTestDouble (
188
+ const first = spyComponent (
178
189
( { children } ) => < b > { children ( 0 ) } </ b > ,
179
190
'test' ,
180
191
'1' ,
181
192
)
182
- const Component2 = createTestDouble (
193
+ const second = spyComponent (
183
194
( { children } ) => < u > { children ( 1 ) } </ u > ,
184
195
'test' ,
185
196
'2' ,
186
197
)
187
198
188
- let currentComponent = Component1
199
+ let currentComponent = first
189
200
const ComponentSwap = props => {
190
201
const { Component } = currentComponent
191
202
return (
@@ -203,19 +214,19 @@ describe('reconciler', () => {
203
214
)
204
215
205
216
const wrapper = mount (
206
- < AppContainer reconciler >
217
+ < AppContainer >
207
218
< App />
208
219
</ AppContainer > ,
209
220
)
210
221
211
222
expect ( wrapper . text ( ) ) . toContain ( 42 )
212
223
213
- currentComponent = Component2
224
+ currentComponent = second
214
225
didUpdate ( )
215
226
wrapper . setProps ( { update : 'now' } )
216
227
217
- expect ( Component1 . unmounted ) . toHaveBeenCalledTimes ( 0 )
218
- expect ( Component2 . mounted ) . toHaveBeenCalledTimes ( 0 )
228
+ expect ( first . unmounted ) . toHaveBeenCalledTimes ( 0 )
229
+ expect ( second . mounted ) . toHaveBeenCalledTimes ( 0 )
219
230
expect ( wrapper . text ( ) ) . toContain ( 43 )
220
231
} )
221
232
} )
@@ -227,7 +238,7 @@ describe('reconciler', () => {
227
238
228
239
RHL . disableComponentProxy = true
229
240
const wrapper = mount (
230
- < AppContainer reconciler >
241
+ < AppContainer >
231
242
< div >
232
243
< div >
233
244
< Transform >
0 commit comments