-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLoadingSwitch.js
246 lines (237 loc) · 6.51 KB
/
LoadingSwitch.js
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
// @flow
/* eslint-disable react/no-multi-comp */
import type { ComponentType } from 'react'
import { forEach } from 'lodash'
import renderer from 'react-test-renderer'
import React, { Fragment } from 'react'
import LoadingSwitch from '../src/LoadingSwitch'
const renderError = ({ message }) => `Error: ${message}`
renderError.displayName = 'renderError'
const renderLoading = () => 'Loading'
renderLoading.displayName = 'renderLoading'
const children = fwdArgs => String(fwdArgs)
children.displayName = 'children'
const loading = true
const error = new Error('`error`: Test message')
const errorWhenMissing = () => new Error('`errorWhenMissing`: Test message')
const snapshotTest: ({|
description: string, component: ComponentType<{||}>,
|}) => void = ({
description, component,
}) => {
it(description, () => {
const element = React.createElement(component)
expect(renderer.create(element).toJSON()).toMatchSnapshot()
})
}
forEach([
{
description: 'renders `children(require)` when `require` is a truthy string',
component: () => (
<LoadingSwitch
errorWhenMissing={errorWhenMissing}
loading={loading}
renderError={renderError}
renderLoading={renderLoading}
require="whatever"
>
{children}
</LoadingSwitch>
),
},
{
description: 'renders `children(require)` when `require` is a plain object',
component: () => {
const require = { foo: 'bar' }
return (
<LoadingSwitch
errorWhenMissing={errorWhenMissing}
loading={loading}
renderError={renderError}
renderLoading={renderLoading}
require={require}
>
{({ foo }: typeof require) => foo}
</LoadingSwitch>
)
},
},
{
description: 'renders `renderLoading()` when `loading` is true and `require` is undefined',
component: () => (
<LoadingSwitch
errorWhenMissing={errorWhenMissing}
loading={loading}
renderError={renderError}
renderLoading={renderLoading}
require={undefined}
>
{children}
</LoadingSwitch>
),
},
{
description: 'renders `renderLoading()` when `loading` is true and `require` is a falsey string',
component: () => (
<LoadingSwitch
errorWhenMissing={errorWhenMissing}
loading={loading}
renderError={renderError}
renderLoading={renderLoading}
require=""
>
{children}
</LoadingSwitch>
),
},
{
description: 'renders `renderLoading()` when `loading` is true and `require` is false',
component: () => (
<LoadingSwitch
errorWhenMissing={errorWhenMissing}
loading={loading}
renderError={renderError}
renderLoading={renderLoading}
require={false}
>
{children}
</LoadingSwitch>
),
},
{
description: 'renders `renderLoading()` when `loading` is true and `require` is 0',
component: () => (
<LoadingSwitch
errorWhenMissing={errorWhenMissing}
loading={loading}
renderError={renderError}
renderLoading={renderLoading}
require={0}
>
{children}
</LoadingSwitch>
),
},
{
description: 'renders `renderError(error)` when `error` is provided',
component: () => (
<LoadingSwitch
errorWhenMissing={errorWhenMissing}
error={error}
loading={loading}
renderError={renderError}
renderLoading={renderLoading}
require={require}
>
{children}
</LoadingSwitch>
),
},
{
description: 'renders `renderError() with default `errorWhenMissing` when `loading` is false but `require` is still falsey',
component: () => (
<LoadingSwitch
errorWhenMissing={errorWhenMissing}
loading={false}
renderError={renderError}
renderLoading={renderLoading}
require={undefined}
>
{children}
</LoadingSwitch>
),
},
{
description: 'renders `renderError(errorWhenMissing())` when `loading` is false but `require` is still falsey and `errorWhenMissing` is provided',
component: () => (
<LoadingSwitch
errorWhenMissing={errorWhenMissing}
loading={false}
renderError={renderError}
renderLoading={renderLoading}
require={undefined}
>
{children}
</LoadingSwitch>
),
},
{
description: 'renders `renderError(error))` when both `error` and `errorWhenMissing` are provided',
component: () => (
<LoadingSwitch
error={error}
errorWhenMissing={errorWhenMissing}
loading={false}
renderError={renderError}
renderLoading={renderLoading}
require={undefined}
>
{children}
</LoadingSwitch>
),
},
{
description: 'can wrap with default props with different require type signatures',
component: () => {
const DefaultLoadingSwitch = props => (
<LoadingSwitch
renderLoading={() => 'Default renderLoading'}
{...Object.freeze(props)}
/>
)
const ls1Require = 'foo'
const ls2Require = { foo: 'bar' }
return (
<Fragment>
<DefaultLoadingSwitch
errorWhenMissing={errorWhenMissing}
loading={false}
renderError={renderError}
require={ls1Require}
>
{children}
</DefaultLoadingSwitch>
<DefaultLoadingSwitch
errorWhenMissing={errorWhenMissing}
loading={false}
renderError={renderError}
require={ls2Require}
>
{({ foo }: typeof ls2Require) => foo}
</DefaultLoadingSwitch>
</Fragment>
)
},
},
{
description: 'can wrap with default props and they get used',
component: () => {
const DefaultLoadingSwitch = props => (
<LoadingSwitch
renderLoading={() => 'Default renderLoading'}
{...Object.freeze(props)}
/>
)
return (
<Fragment>
<DefaultLoadingSwitch
errorWhenMissing={errorWhenMissing}
loading
renderError={renderError}
require={undefined}
>
{children}
</DefaultLoadingSwitch>
<DefaultLoadingSwitch
errorWhenMissing={errorWhenMissing}
loading
renderError={renderError}
require=""
>
{children}
</DefaultLoadingSwitch>
</Fragment>
)
},
},
], snapshotTest)