Repository navigation
Expand file tree
/
Copy pathindex.test.tsx
More file actions
270 lines (210 loc) · 9.24 KB
/
Copy pathindex.test.tsx
File metadata and controls
270 lines (210 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// jsdom, needs to be imported first
import './test-setup'
import {mount, shallow} from 'enzyme'
import toJson from 'enzyme-to-json'
import * as React from 'react'
import compose from 'recompose/compose'
import defaultProps from 'recompose/defaultProps'
import {withSpinner} from './index'
const Loading = () => <span>Loading...</span>
jest.useFakeTimers()
describe('withSpinner', () => {
it('should render spinner if loading is true', () => {
const Component = compose<any, {}>(
defaultProps({data: {loading: true}}),
withSpinner({spinnerComponent: Loading}),
)(() => <div></div>)
const wrapper = shallow(<Component />).first().shallow()
expect(wrapper.type()).toBeNull()
// Run timer so it passes withSpinner timeout
jest.runTimersToTime(100)
wrapper.update()
expect(toJson(wrapper)).toMatchSnapshot()
})
it('should not render spinner if loading is false', () => {
const Component = compose<any, {}>(
defaultProps({data: {loading: false}}),
withSpinner({spinnerComponent: Loading}),
)(({data}) => <div>loading: {data.loading.toString()}</div>)
const wrapper = shallow(<Component />).first().shallow().first().shallow()
wrapper.update()
expect(toJson(wrapper)).toMatchSnapshot()
})
it('should render spinner in 100 ms and after render component', () => {
const DisplayComponent = ({data}) => <div>loading: {data.loading.toString()}, item: {data.item.id}</div>
const Component = compose<any, {}>(
WrappedComponent => class extends React.Component<any, any> {
state = {loading: true, item: null}
componentDidMount() {
// Delay setting loading to false.
setTimeout(() => {
this.setState({loading: false, item: {id: 1}})
}, 1000)
}
render() {
return <WrappedComponent {...this.props} data={{loading: this.state.loading, item: this.state.item}} />
}
},
withSpinner({spinnerComponent: Loading}),
)(DisplayComponent)
let wrapper = mount(<Component />)
// Should not display a Spinner
expect(wrapper.html()).toBeNull()
// Run timer to 100 ms since withSpinner timeout defaults to 100 ms
jest.runTimersToTime(100)
// ProgressBar should now be found
wrapper = wrapper.update()
expect(wrapper.find(Loading)).toHaveLength(1)
expect(wrapper.find(DisplayComponent)).toHaveLength(0)
// Run timer to 1000 ms for our own timeout
jest.runTimersToTime(1000)
wrapper = wrapper.update()
// DisplayComponent should be found
expect(wrapper.find(DisplayComponent)).toHaveLength(1)
expect(wrapper.find(Loading)).toHaveLength(0)
expect(toJson(wrapper)).toMatchSnapshot()
})
it('should render passed spinnerComponent', () => {
const Loading = () => <span>Loading...</span>
const Component = compose(
defaultProps({data: {loading: true}}),
withSpinner({spinnerComponent: Loading}),
)(null)
const wrapper = shallow(<Component />).first().shallow()
jest.runTimersToTime(100)
expect(toJson(wrapper)).toMatchSnapshot()
})
it('should ignore errors if handleError is false', () => {
const DisplayComponent = ({data}) => <div>loading: {data.loading.toString()}, item: {data.item.id}</div>
const Component = compose<any, {}>(
defaultProps({data: {loading: false, error: true}}),
withSpinner({spinnerComponent: Loading, handleError: false}),
)(DisplayComponent)
const wrapper = shallow(<Component />).first().shallow()
jest.runTimersToTime(100)
expect(toJson(wrapper)).toMatchSnapshot()
})
it('should render passed errorComponent if error exists', () => {
const ErrorComponent = () => <span>An unknown error occured...</span>
const Component = compose(
defaultProps({data: {loading: false, error: true}}),
withSpinner({spinnerComponent: Loading, errorComponent: ErrorComponent}),
)(null)
const wrapper = shallow(<Component />).first().shallow()
jest.runTimersToTime(100)
expect(wrapper.find(ErrorComponent)).toHaveLength(1)
expect(toJson(wrapper)).toMatchSnapshot()
})
it('should delay rendering of spinnerComponent the timeout that is passed', () => {
const DisplayComponent = ({data}) => <div>loading: {data.loading.toString()}, item: {data.item.id}</div>
const Component = compose<any, {}>(
defaultProps({data: {loading: true}}),
withSpinner({spinnerComponent: Loading, timeout: 500}),
)(DisplayComponent)
const wrapper = shallow(<Component />).first().shallow()
// Run timer to a time before our timeout
jest.runTimersToTime(400)
expect(wrapper.type()).toBeNull()
// Run timer over our timeout
jest.runTimersToTime(600)
expect(wrapper.first().shallow().getElement()).toEqual(Loading())
})
it('should not render errorComponent if skipErrors returns true', () => {
const validationError = {validationError: {field: 'password', message: 'Wrong password'}}
const ErrorComponent = () => <span>An unknown error occured...</span>
const DisplayComponent = ({data: {error}}) => <div>
Validation error occured on field: {error.validationError.field} with message: {error.validationError.message}
</div>
const Component = compose<any, {}>(
defaultProps({data: {loading: false, error: validationError}}),
withSpinner(({spinnerComponent: Loading,
errorComponent: ErrorComponent,
skipErrors: data => data.error.validationError && data.error.validationError.field === 'password'
})),
)(DisplayComponent)
const wrapper = shallow(<Component />).first().shallow()
jest.runTimersToTime(100)
expect(wrapper.find(ErrorComponent)).toHaveLength(0)
expect(toJson(wrapper.first().shallow())).toMatchSnapshot()
})
it('should support custom loading property', () => {
const DisplayComponent = ({result}) => <div>loading: {result.loading.toString()}, item: {result.item.id}</div>
const Component = compose<any, {}>(
WrappedComponent => class extends React.Component<any, any> {
state = {loading: true, item: null}
componentDidMount() {
// Delay setting loading to false.
setTimeout(() => {
this.setState({loading: false, item: {id: 1}})
}, 1000)
}
render() {
return <WrappedComponent {...this.props} result={{loading: this.state.loading, item: this.state.item}} />
}
},
withSpinner({spinnerComponent: Loading, prop: 'result'}),
)(DisplayComponent)
let wrapper = mount(<Component />)
// Should not display a Spinner
expect(wrapper.html()).toBeNull()
// Run timer to 100 ms since withSpinner timeout defaults to 100 ms
jest.runTimersToTime(100)
wrapper = wrapper.update()
// ProgressBar should now be found
expect(wrapper.find(Loading)).toHaveLength(1)
expect(wrapper.find(DisplayComponent)).toHaveLength(0)
// Run timer to 1000 ms for our own timeout
jest.runTimersToTime(1000)
wrapper = wrapper.update()
// DisplayComponent should be found
expect(wrapper.find(DisplayComponent)).toHaveLength(1)
expect(wrapper.find(Loading)).toHaveLength(0)
})
it('should support custom nested loading property', () => {
const DisplayComponent = ({result}) => <div>loading: {result.nested.loading.toString()}, item: {result.nested.item && result.nested.item.id}</div>
const Component = compose<any, {}>(
WrappedComponent => class extends React.Component<any, any> {
state = {loading: true, item: null}
componentDidMount() {
// Delay setting loading to false.
setTimeout(() => {
this.setState({loading: false, item: {id: 1}})
}, 1000)
}
render() {
// return <WrappedComponent {...this.props} data={{loading: this.state.loading, item: this.state.item}} />
return <WrappedComponent {...this.props} result={{nested: {loading: this.state.loading, item: this.state.item}}} />
}
},
withSpinner({spinnerComponent: Loading, prop: ['result', 'nested']}),
// withSpinner(),
)(DisplayComponent)
let wrapper = mount(<Component />)
// Should not display a Spinner
expect(wrapper.html()).toBeNull()
// Run timer to 100 ms since withSpinner timeout defaults to 100 ms
jest.runTimersToTime(100)
wrapper = wrapper.update()
// ProgressBar should now be found
expect(wrapper.find(Loading)).toHaveLength(1)
expect(wrapper.find(DisplayComponent)).toHaveLength(0)
// Run timer to 1000 ms for our own timeout
jest.runTimersToTime(1000)
wrapper = wrapper.update()
// DisplayComponent should be found
expect(wrapper.find(DisplayComponent)).toHaveLength(1)
expect(wrapper.find(Loading)).toHaveLength(0)
expect(toJson(wrapper)).toMatchSnapshot()
})
it('should render a emptyComponent if data i empty', () => {
const EmptyComponent = () => <span>No data...</span>
const Component = compose(
defaultProps({data: {loading: false, value: {}}}),
withSpinner({spinnerComponent: Loading, emptyComponent: EmptyComponent, prop: ['data', 'value']}),
)(null)
const wrapper = shallow(<Component />).first().shallow()
jest.runTimersToTime(100)
expect(wrapper.find(EmptyComponent)).toHaveLength(1)
expect(toJson(wrapper)).toMatchSnapshot()
})
})