-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-helpers.js
41 lines (36 loc) · 1.15 KB
/
test-helpers.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
/* eslint-env jest */
import { toArr, flatten, deepMerge } from '@exah/utils'
import renderer from 'react-test-renderer'
const THEME = {
media: {
D: '(min-width: 601px)',
M: '(max-width: 600px)'
}
}
const toJSON = (element) => renderer.create(element).toJSON()
const toStyles = (styles) => deepMerge(...flatten(toArr(styles)))
const wrapInMedia = (style) => ({ [`@media ${THEME.media.M}`]: style })
const testValue = ({
fn,
prop,
cssProp,
theme = THEME,
values = [],
autoValue
}) => () => {
expect(toStyles(fn({ theme, [prop]: true }))).toEqual(autoValue != null ? { [cssProp]: autoValue } : {})
expect(toStyles(fn({ theme, [prop]: 'auto' }))).toEqual(autoValue != null ? { [cssProp]: autoValue } : { [cssProp]: 'auto' })
expect(toStyles(fn({ theme, [prop]: null }))).toEqual({})
values.forEach((val) => {
expect(toStyles(fn({ theme, [prop]: val }))).toEqual({ [cssProp]: val })
expect(toStyles(fn({ theme, [prop]: val }))).toEqual({ [cssProp]: val })
expect(toStyles(fn({ theme, [prop]: { M: val } }))).toEqual(wrapInMedia({ [cssProp]: val }))
})
}
export {
toJSON,
THEME as theme,
toStyles,
wrapInMedia,
testValue
}