-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
381 lines (358 loc) · 10.3 KB
/
index.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import {
theme,
cond,
breakpoint,
palette,
size,
propIf,
propIfElse,
getErrorMessage,
} from '../src/index'
describe('utils', () => {
const _props = {
theme: {
color: {
gray: {
light: '#aaa',
},
},
palette: {
primary: '#4682b4',
primaryLight: '#5699c5',
},
breakpoints: {
mobile: {
medium: 10,
small: 5,
},
desktop: {
medium: 100,
small: 50,
},
},
sizes: {
container: '1200px',
block: '120px',
},
},
truthy: true,
falsy: false,
maybeTruthy: '1',
maybeFalsy: '',
}
describe('theme()', () => {
it('with string path', () => {
const result = theme('color')(_props)
expect(result).toBe(_props.theme.color)
})
it('with array path', () => {
const result = theme(['color'])(_props)
expect(result).toBe(_props.theme.color)
})
it('with deep array path', () => {
const result = theme(['color', 'gray', 'light'])(_props)
expect(result).toBe(_props.theme.color.gray.light)
})
it('non exists key by array path', () => {
const path = ['color', 'gray', 'NOTFOUND']
try {
theme(path)(_props)
}
catch (error) {
expect(error.message).toBe(getErrorMessage(['theme', ...path]))
}
})
})
describe('cond()', () => {
it('truthy with string path', () => {
const result = cond('truthy', 'color')(_props)
expect(result).toBe(_props.theme.color)
})
it('falsy with string path', () => {
const result = cond('falsy', 'color')(_props)
expect(result).toBe(false)
})
it('truthy with array path', () => {
const result = cond('truthy', ['color'])(_props)
expect(result).toBe(_props.theme.color)
})
it('falsy with array path', () => {
const result = cond('falsy', ['color'])(_props)
expect(result).toBe(false)
})
it('truthy with deep array path', () => {
const result = cond('truthy', ['color', 'gray', 'light'])(_props)
expect(result).toBe(_props.theme.color.gray.light)
})
it('falsy with deep array path', () => {
const result = cond('falsy', ['color', 'gray', 'light'])(_props)
expect(result).toBe(false)
})
it('on non exists path string', () => {
const name = 'NOTFOUND'
try {
cond('truthy', name)(_props)
}
catch (error) {
expect(error.message).toBe(getErrorMessage(['theme', name]))
}
})
it('on non exists path deep array', () => {
const path = ['color', 'NOTFOUND', 'light']
try {
cond('truthy', path)(_props)
}
catch (error) {
expect(error.message).toBe(getErrorMessage(['theme', ...path]))
}
})
})
describe('breakpoint()', () => {
it('exists with default size', () => {
const result = breakpoint('mobile')(_props)
expect(result).toBe(_props.theme.breakpoints.mobile.medium)
})
it('exists with custom size', () => {
const result = breakpoint('desktop', 'small')(_props)
expect(result).toBe(_props.theme.breakpoints.desktop.small)
})
it('non exists with default', () => {
const name = 'NOTFOUND'
const size = 'medium'
try {
breakpoint(name, size)(_props)
}
catch (error) {
expect(error.message).toBe(getErrorMessage(['theme', 'breakpoints', name, size]))
}
})
it('non exists with custom', () => {
const name = 'NOTFOUND'
const size = 'ERROR'
try {
breakpoint('NOTFOUND', 'ERROR')(_props)
}
catch (error) {
expect(error.message).toBe(getErrorMessage(['theme', 'breakpoints', name, size]))
}
})
it('exists with custom non exists size', () => {
const name = 'desktop'
const size = 'NOTFOUNDSIZE'
try {
breakpoint('desktop', 'NOTFOUNDSIZE')(_props)
}
catch (error) {
expect(error.message).toBe(getErrorMessage(['theme', 'breakpoints', name, size]))
}
})
})
describe('palette()', () => {
it('with default shade', () => {
const result = palette('primary')(_props)
expect(result).toBe(_props.theme.palette.primary)
})
it('with custom shade', () => {
const result = palette('primary', 'Light')(_props)
expect(result).toBe(_props.theme.palette.primaryLight)
})
it('non exists palette without shade', () => {
const name = 'NOTFOUND'
try {
palette(name)(_props)
}
catch (error) {
expect(error.message).toBe(getErrorMessage(['theme', 'palette', name]))
}
})
it('non exists palette with custom shade', () => {
const name = 'NOTFOUND'
const shade = 'ASD'
try {
palette(name, shade)(_props)
}
catch (error) {
expect(error.message).toBe(getErrorMessage(['theme', 'palette', `${name}${shade}`]))
}
})
it('exists palette with non exists shade', () => {
const name = 'primary'
const shade = 'NOTFOUND'
try {
palette(name, shade)(_props)
}
catch (error) {
expect(error.message).toBe(getErrorMessage(['theme', 'palette', `${name}${shade}`]))
}
})
})
describe('size()', () => {
it('resolve size', () => {
const result = size('container')(_props)
expect(result).toBe(_props.theme.sizes.container)
})
it('another size', () => {
const result = size('block')(_props)
expect(result).toBe(_props.theme.sizes.block)
})
it('non exists size', () => {
const name = 'NOTFOUND'
try {
size(name)(_props)
}
catch (error) {
expect(error.message).toBe(getErrorMessage(['theme', 'sizes', name]))
}
})
})
describe('propIf()', () => {
it('condition string with branch string', () => {
expect(
propIf('truthy', 'Result')(_props),
).toBe('Result')
expect(
propIf('falsy', 'Result')(_props),
).toBe(undefined)
})
it('condition string with branch function', () => {
const fn = props => props.theme.palette
expect(
propIf('truthy', fn)(_props),
).toBe(_props.theme.palette)
expect(
propIf('falsy', fn)(_props),
).toBe(undefined)
})
it('condition func with branch string', () => {
expect(
propIf(props => props.truthy, 'Result')(_props),
).toBe('Result')
expect(
propIf(props => props.falsy, 'Result')(_props),
).toBe(undefined)
})
it('condition func with branch func', () => {
const fn = props => props.theme.palette
expect(
propIf(props => props.truthy, fn)(_props),
).toBe(fn(_props))
expect(
propIf(props => props.falsy, fn)(_props),
).toBe(undefined)
})
it('condition string non exists field', () => {
expect(
propIf('NOTFOUND', 'Result')(_props),
).toBe(undefined)
})
it('condition func returns falsy', () => {
expect(
propIf(() => false, 'Result')(_props),
).toBe(undefined)
expect(
propIf(() => null, 'Result')(_props),
).toBe(undefined)
expect(
propIf(() => undefined, 'Result')(_props),
).toBe(undefined)
expect(
propIf(() => '', 'Result')(_props),
).toBe(undefined)
})
it('condition is boolean', () => {
expect(
propIf(true, 'Result')(_props),
).toBe('Result')
expect(
propIf(true, () => 'Result')(_props),
).toBe('Result')
expect(
propIf(false, 'Result')(_props),
).toBe(undefined)
})
})
describe('propIfElse()', () => {
it('condition string with branch string', () => {
expect(
propIfElse('truthy', 'Success', 'Fail')(_props),
).toBe('Success')
expect(
propIfElse('falsy', 'Result', 'Fail')(_props),
).toBe('Fail')
})
it('condition string with branches function', () => {
const fn1 = props => props.theme.palette
const fn2 = props => props.theme.sizes
expect(
propIfElse('truthy', fn1, fn2)(_props),
).toBe(_props.theme.palette)
expect(
propIfElse('falsy', fn1, fn2)(_props),
).toBe(_props.theme.sizes)
})
it('condition func with branches string', () => {
expect(
propIfElse(props => props.truthy, 'Success', 'Fail')(_props),
).toBe('Success')
expect(
propIfElse(props => props.falsy, 'Success', 'Fail')(_props),
).toBe('Fail')
})
it('condition func with branch func', () => {
const fn1 = props => props.theme.palette
const fn2 = props => props.theme.sizes
expect(
propIfElse(props => props.truthy, fn1, fn2)(_props),
).toBe(fn1(_props))
expect(
propIfElse(props => props.falsy, fn1, fn2)(_props),
).toBe(fn2(_props))
})
it('condition string with branch if string branch else func', () => {
const fn1 = props => props.theme.palette
expect(
propIfElse('truthy', 'Success', fn1)(_props),
).toBe('Success')
expect(
propIfElse('falsy', 'Success', fn1)(_props),
).toBe(fn1(_props))
})
it('condition string with branch if func branch else string', () => {
const fn1 = props => props.theme.palette
expect(
propIfElse('truthy', fn1, 'Fail')(_props),
).toBe(fn1(_props))
expect(
propIfElse('falsy', fn1, 'Fail')(_props),
).toBe('Fail')
})
it('condition func with branch if func branch else string', () => {
const fn1 = props => props.theme.palette
expect(
propIfElse(props => props.truthy, fn1, 'Fail')(_props),
).toBe(fn1(_props))
expect(
propIfElse(props => props.falsy, fn1, 'Fail')(_props),
).toBe('Fail')
})
it('condition is boolean', () => {
expect(
propIfElse(true, 'Result', 'Falsy')(_props),
).toBe('Result')
expect(
propIfElse(true, () => 'Result', () => 'Falsy')(_props),
).toBe('Result')
expect(
propIfElse(false, 'Result', 'Falsy')(_props),
).toBe('Falsy')
})
it('condition without falsy branch // need it?', () => {
expect(
propIfElse(() => true, 'Result')(_props),
).toBe('Result')
expect(
propIfElse(() => false, 'Result')(_props),
).toBe(undefined)
})
})
})