@@ -5,15 +5,16 @@ import {mount, shallow} from 'enzyme'
5
5
import toJson from 'enzyme-to-json'
6
6
import * as React from 'react'
7
7
import compose from 'recompose/compose'
8
+ import defaultProps from 'recompose/defaultProps'
8
9
import { withSpinner } from './index'
9
10
10
11
const Loading = ( ) => < span > Loading...</ span >
11
12
jest . useFakeTimers ( )
12
13
13
14
describe ( 'withSpinner' , ( ) => {
14
15
it ( 'should render spinner if loading is true' , ( ) => {
15
- const Component = compose (
16
- WrappedComponent => props => < WrappedComponent { ... props } data = { { loading : true } } /> ,
16
+ const Component = compose < any , { } > (
17
+ defaultProps ( { data : { loading : true } } ) ,
17
18
withSpinner ( { spinnerComponent : Loading } ) ,
18
19
) ( ( ) => < div > </ div > )
19
20
@@ -29,8 +30,8 @@ describe('withSpinner', () => {
29
30
} )
30
31
31
32
it ( 'should not render spinner if loading is false' , ( ) => {
32
- const Component = compose (
33
- WrappedComponent => props => < WrappedComponent { ... props } data = { { loading : false } } /> ,
33
+ const Component = compose < any , { } > (
34
+ defaultProps ( { data : { loading : false } } ) ,
34
35
withSpinner ( { spinnerComponent : Loading } ) ,
35
36
) ( ( { data} ) => < div > loading: { data . loading . toString ( ) } </ div > )
36
37
@@ -43,7 +44,7 @@ describe('withSpinner', () => {
43
44
it ( 'should render spinner in 100 ms and after render component' , ( ) => {
44
45
const DisplayComponent = ( { data} ) => < div > loading: { data . loading . toString ( ) } , item: { data . item . id } </ div >
45
46
46
- const Component = compose (
47
+ const Component = compose < any , { } > (
47
48
WrappedComponent => class extends React . Component < any , any > {
48
49
state = { loading : true , item : null }
49
50
@@ -61,19 +62,21 @@ describe('withSpinner', () => {
61
62
withSpinner ( { spinnerComponent : Loading } ) ,
62
63
) ( DisplayComponent )
63
64
64
- const wrapper = mount ( < Component /> )
65
+ let wrapper = mount ( < Component /> )
65
66
66
67
// Should not display a Spinner
67
68
expect ( wrapper . html ( ) ) . toBeNull ( )
68
69
69
70
// Run timer to 100 ms since withSpinner timeout defaults to 100 ms
70
71
jest . runTimersToTime ( 100 )
71
72
// ProgressBar should now be found
73
+ wrapper = wrapper . update ( )
72
74
expect ( wrapper . find ( Loading ) ) . toHaveLength ( 1 )
73
75
expect ( wrapper . find ( DisplayComponent ) ) . toHaveLength ( 0 )
74
76
75
77
// Run timer to 1000 ms for our own timeout
76
78
jest . runTimersToTime ( 1000 )
79
+ wrapper = wrapper . update ( )
77
80
// DisplayComponent should be found
78
81
expect ( wrapper . find ( DisplayComponent ) ) . toHaveLength ( 1 )
79
82
expect ( wrapper . find ( Loading ) ) . toHaveLength ( 0 )
@@ -85,22 +88,21 @@ describe('withSpinner', () => {
85
88
const Loading = ( ) => < span > Loading...</ span >
86
89
87
90
const Component = compose (
88
- WrappedComponent => props => < WrappedComponent { ... props } data = { { loading : true } } /> ,
91
+ defaultProps ( { data : { loading : true } } ) ,
89
92
withSpinner ( { spinnerComponent : Loading } ) ,
90
93
) ( null )
91
94
92
95
const wrapper = shallow ( < Component /> ) . first ( ) . shallow ( )
93
96
jest . runTimersToTime ( 100 )
94
- wrapper . update ( )
95
97
96
98
expect ( toJson ( wrapper ) ) . toMatchSnapshot ( )
97
99
} )
98
100
99
101
it ( 'should ignore errors if handleError is false' , ( ) => {
100
102
const DisplayComponent = ( { data} ) => < div > loading: { data . loading . toString ( ) } , item: { data . item . id } </ div >
101
103
102
- const Component = compose (
103
- WrappedComponent => props => < WrappedComponent { ... props } data = { { loading : false , error : true } } /> ,
104
+ const Component = compose < any , { } > (
105
+ defaultProps ( { data : { loading : false , error : true } } ) ,
104
106
withSpinner ( { spinnerComponent : Loading , handleError : false } ) ,
105
107
) ( DisplayComponent )
106
108
@@ -114,7 +116,7 @@ describe('withSpinner', () => {
114
116
const ErrorComponent = ( ) => < span > An unknown error occured...</ span >
115
117
116
118
const Component = compose (
117
- WrappedComponent => props => < WrappedComponent { ... props } data = { { loading : false , error : true } } /> ,
119
+ defaultProps ( { data : { loading : false , error : true } } ) ,
118
120
withSpinner ( { spinnerComponent : Loading , errorComponent : ErrorComponent } ) ,
119
121
) ( null )
120
122
@@ -128,8 +130,8 @@ describe('withSpinner', () => {
128
130
it ( 'should delay rendering of spinnerComponent the timeout that is passed' , ( ) => {
129
131
const DisplayComponent = ( { data} ) => < div > loading: { data . loading . toString ( ) } , item: { data . item . id } </ div >
130
132
131
- const Component = compose (
132
- WrappedComponent => props => < WrappedComponent { ... props } data = { { loading : true } } /> ,
133
+ const Component = compose < any , { } > (
134
+ defaultProps ( { data : { loading : true } } ) ,
133
135
withSpinner ( { spinnerComponent : Loading , timeout : 500 } ) ,
134
136
) ( DisplayComponent )
135
137
@@ -141,7 +143,7 @@ describe('withSpinner', () => {
141
143
142
144
// Run timer over our timeout
143
145
jest . runTimersToTime ( 600 )
144
- expect ( wrapper . first ( ) . shallow ( ) . node ) . toEqual ( Loading ( ) )
146
+ expect ( wrapper . first ( ) . shallow ( ) . getElement ( ) ) . toEqual ( Loading ( ) )
145
147
} )
146
148
147
149
it ( 'should not render errorComponent if skipErrors returns true' , ( ) => {
@@ -151,8 +153,8 @@ describe('withSpinner', () => {
151
153
Validation error occured on field: { error . validationError . field } with message: { error . validationError . message }
152
154
</ div >
153
155
154
- const Component = compose (
155
- WrappedComponent => props => < WrappedComponent { ... props } data = { { loading : false , error : validationError } } /> ,
156
+ const Component = compose < any , { } > (
157
+ defaultProps ( { data : { loading : false , error : validationError } } ) ,
156
158
withSpinner ( ( { spinnerComponent : Loading ,
157
159
errorComponent : ErrorComponent ,
158
160
skipErrors : data => data . error . validationError && data . error . validationError . field === 'password'
@@ -169,7 +171,7 @@ describe('withSpinner', () => {
169
171
it ( 'should support custom loading property' , ( ) => {
170
172
const DisplayComponent = ( { result} ) => < div > loading: { result . loading . toString ( ) } , item: { result . item . id } </ div >
171
173
172
- const Component = compose (
174
+ const Component = compose < any , { } > (
173
175
WrappedComponent => class extends React . Component < any , any > {
174
176
state = { loading : true , item : null }
175
177
@@ -187,19 +189,21 @@ describe('withSpinner', () => {
187
189
withSpinner ( { spinnerComponent : Loading , prop : 'result' } ) ,
188
190
) ( DisplayComponent )
189
191
190
- const wrapper = mount ( < Component /> )
192
+ let wrapper = mount ( < Component /> )
191
193
192
194
// Should not display a Spinner
193
195
expect ( wrapper . html ( ) ) . toBeNull ( )
194
196
195
197
// Run timer to 100 ms since withSpinner timeout defaults to 100 ms
196
198
jest . runTimersToTime ( 100 )
199
+ wrapper = wrapper . update ( )
197
200
// ProgressBar should now be found
198
201
expect ( wrapper . find ( Loading ) ) . toHaveLength ( 1 )
199
202
expect ( wrapper . find ( DisplayComponent ) ) . toHaveLength ( 0 )
200
203
201
204
// Run timer to 1000 ms for our own timeout
202
205
jest . runTimersToTime ( 1000 )
206
+ wrapper = wrapper . update ( )
203
207
// DisplayComponent should be found
204
208
expect ( wrapper . find ( DisplayComponent ) ) . toHaveLength ( 1 )
205
209
expect ( wrapper . find ( Loading ) ) . toHaveLength ( 0 )
@@ -208,7 +212,7 @@ describe('withSpinner', () => {
208
212
it ( 'should support custom nested loading property' , ( ) => {
209
213
const DisplayComponent = ( { result} ) => < div > loading: { result . nested . loading . toString ( ) } , item: { result . nested . item && result . nested . item . id } </ div >
210
214
211
- const Component = compose (
215
+ const Component = compose < any , { } > (
212
216
WrappedComponent => class extends React . Component < any , any > {
213
217
state = { loading : true , item : null }
214
218
@@ -228,19 +232,21 @@ describe('withSpinner', () => {
228
232
// withSpinner(),
229
233
) ( DisplayComponent )
230
234
231
- const wrapper = mount ( < Component /> )
235
+ let wrapper = mount ( < Component /> )
232
236
233
237
// Should not display a Spinner
234
238
expect ( wrapper . html ( ) ) . toBeNull ( )
235
239
236
240
// Run timer to 100 ms since withSpinner timeout defaults to 100 ms
237
241
jest . runTimersToTime ( 100 )
242
+ wrapper = wrapper . update ( )
238
243
// ProgressBar should now be found
239
244
expect ( wrapper . find ( Loading ) ) . toHaveLength ( 1 )
240
245
expect ( wrapper . find ( DisplayComponent ) ) . toHaveLength ( 0 )
241
246
242
247
// Run timer to 1000 ms for our own timeout
243
248
jest . runTimersToTime ( 1000 )
249
+ wrapper = wrapper . update ( )
244
250
// DisplayComponent should be found
245
251
expect ( wrapper . find ( DisplayComponent ) ) . toHaveLength ( 1 )
246
252
expect ( wrapper . find ( Loading ) ) . toHaveLength ( 0 )
@@ -251,7 +257,7 @@ describe('withSpinner', () => {
251
257
const EmptyComponent = ( ) => < span > No data...</ span >
252
258
253
259
const Component = compose (
254
- WrappedComponent => props => < WrappedComponent { ... props } data = { { loading : false , value : { } } } /> ,
260
+ defaultProps ( { data : { loading : false , value : { } } } ) ,
255
261
withSpinner ( { spinnerComponent : Loading , emptyComponent : EmptyComponent , prop : [ 'data' , 'value' ] } ) ,
256
262
) ( null )
257
263
0 commit comments