This repository has been archived by the owner on Aug 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
178 lines (156 loc) · 5.57 KB
/
test.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
import 'jest-styled-components'
import styled, { css as scCSS, isStyledComponent } from 'styled-components'
import { space, color } from 'styled-system'
import React from 'react'
import { create as render } from 'react-test-renderer'
import { isDOMComponent, isCompositeComponent } from 'react-dom/test-utils'
import system from './src'
describe('@rebass/components', () => {
test('returns a React component', () => {
const Box = system()
const box = render(<Box />).getInstance()
expect(isCompositeComponent(box)).toBe(true)
})
test('returns a styled-component', () => {
const Box = system()
expect(typeof Box).toBe('function')
expect(typeof Box.styledComponentId).toBe('string')
expect(isStyledComponent(Box)).toBe(true)
})
test('Adds defaultProps', () => {
const Box = system({
p: 2,
bg: 'tomato'
})
expect(Box.defaultProps.p).toBe(2)
expect(Box.defaultProps.bg).toBe('tomato')
})
test('adds propTypes', () => {
const Box = system({}, 'space', 'color')
expect(Box.propTypes).toEqual({
...space.propTypes,
...color.propTypes,
})
})
// deprecate
test.skip('adds styled-system functions based on default props', () => {
const Box = system({
p: 3,
bg: 'tomato'
})
const json = render(<Box />).toJSON()
expect(json).toHaveStyleRule('padding', '16px')
expect(json).toHaveStyleRule('background-color', 'tomato')
})
test('accepts system key arguments', () => {
const Box = system({}, 'space', 'color')
expect(typeof Box.propTypes.m).toBe('function')
expect(typeof Box.propTypes.color).toBe('function')
expect(typeof Box.propTypes.bg).toBe('function')
})
test('ignores nonexistant function keys', () => {
const Box = system({}, 'foo', 'bar')
expect(Box.propsTypes).toBe(undefined)
})
test('accepts custom function arguments', () => {
const big = props => props.big ? { padding: '64px' } : null
const Box = system({}, big)
const json = render(<Box big />).toJSON()
expect(json).toHaveStyleRule('padding', '64px')
})
test('removes styled-system props from underlying DOM element', () => {
const Box = system({
color: 'blue'
})
const json = render(<Box />).toJSON()
expect(json.props.color).toBe(undefined)
expect(typeof json.props.className).toBe('string')
})
test('removes blacklist props from underlying DOM element', () => {
const Box = system({
blacklist: ['customProp']
})
const json = render(<Box customProp='hi' />).toJSON()
expect(json.props.customProp).toBe(undefined)
})
test('accepts an `is` prop to change the underlying DOM element', () => {
const Box = system({})
const json = render(<Box is='h1' />).toJSON()
expect(json.type).toBe('h1')
})
test('accepts a style function argument', () => {
const Box = system({}, props => `color:${props.color};`)
const json = render(<Box color='magenta' />).toJSON()
expect(json).toHaveStyleRule('color', 'magenta')
})
test('accepts theme as a default prop', () => {
const theme = {
colors: {
blue: '#0af'
}
}
const Box = system({ color: 'blue', theme }, 'color')
const json = render(<Box />).toJSON()
expect(json).toHaveStyleRule('color', '#0af')
})
test('passes css string arguments', () => {
const Box = system({}, 'color:cyan;')
const json = render(<Box />).toJSON()
expect(json).toHaveStyleRule('color', 'cyan')
})
test('works with the styled-component `css` helper', () => {
const Box = system({}, scCSS`
color: ${props => props.color};
`)
const json = render(<Box color='yellow' />).toJSON()
expect(json).toHaveStyleRule('color', 'yellow')
})
test('defaultProps are passed to extended components', () => {
const Box = system({
p: 2,
bg: 'tomato'
}, 'space', 'color')
const ExtendedBox = system({ extend: Box })
const json = render(<ExtendedBox />).toJSON()
expect(json).toHaveStyleRule('background-color', 'tomato')
})
test('accepts a css prop for custom styling', () => {
const Box = system({})
const json = render(<Box css='color:tomato;' />).toJSON()
expect(json).toHaveStyleRule('color', 'tomato')
})
test('merges defaultProps from `extend` prop component', () => {
const Base = system({ p: 3 }, 'space')
const Ext = system({ extend: Base })
const json = render(<Ext />).toJSON()
expect(json).toHaveStyleRule('padding', '16px')
expect(Ext.defaultProps.p).toBe(3)
})
test('extends components with the `extend` prop and passes `is` prop to base component', () => {
const Base = system({ p: 3 }, 'space')
const Ext = system({ extend: Base }, 'color')
const base = render(<Base />).toJSON()
const json = render(<Ext is='footer' p={3} bg='tomato' />).toJSON()
expect(json.type).toBe('footer')
expect(json).toHaveStyleRule('background-color', 'tomato')
expect(json).toHaveStyleRule('padding', '16px')
})
test('extends a non-system component and does not accept an is prop', () => {
const Base = props => <div className='Base' {...props} />
const Ext = system({ extend: Base }, 'color')
const json = render(
<Ext is='footer' color='tomato' />
).toJSON()
expect(json.type).toBe('div')
expect(json).toHaveStyleRule('color', 'tomato')
})
test('passes innerRef to underlying element', () => {
const Base = system({})
let foo = 'hello'
const instance = render(
<Base innerRef={ref => foo = ref} />
).getInstance()
expect(isCompositeComponent(instance)).toBe(true)
expect(foo).not.toBe('hello')
})
})