-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
styled.test.tsx
157 lines (141 loc) Β· 3.91 KB
/
styled.test.tsx
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
import * as React from 'react'
import '@testing-library/jest-dom/extend-expect'
import { render, cleanup } from '@testing-library/react'
import { ThemeProvider } from '@emotion/react'
import styled, { css, keyframes } from '.'
afterEach(cleanup)
describe('#styled', () => {
it('transforms rules', () => {
const Dummy = styled.div`
margin: 2;
padding: 1;
margin-top: 2px;
`
const { container } = render(<Dummy />)
expect(container.firstChild).toHaveStyle(`
margin: 2px 8px 8px 8px;
padding: 4px;
`)
})
it('works with conditional css', () => {
interface DummyProps {
margin: number
}
const Dummy = styled.div<DummyProps>`
color: red;
${(p) => css`
margin: ${p.margin};
`}
`
const { container } = render(<Dummy margin={2} />)
expect(container.firstChild).toHaveStyle(`
color: red;
margin: 8px;
`)
})
it('works with keyframes', () => {
const animation = keyframes`
from {
transform: translateX(0%);
}
to {
transform: translateX(100%);
}
`
const Dummy = styled.div`
${css`
animation: ${animation};
`}
`
const { container } = render(<Dummy />)
expect(container.firstChild).toHaveStyle(`animation: ${animation.name};`)
})
it('reads value from the theme', () => {
const theme = {
colors: {
primary: 'pink',
},
}
const Dummy = styled.div`
color: primary;
`
const { container } = render(
<ThemeProvider theme={theme}>
<Dummy />
</ThemeProvider>,
)
expect(container.firstChild).toHaveStyle('color: pink;')
})
it('handles negative values', () => {
const theme = {
space: {
md: 10,
},
}
const Dummy = styled.div`
margin: -md;
`
const { container } = render(
<ThemeProvider theme={theme}>
<Dummy />
</ThemeProvider>,
)
expect(container.firstChild).toHaveStyle('margin: -10px;')
})
it('works with css as object', () => {
const Dummy = styled.div({
margin: '2',
})
const { container } = render(<Dummy />)
expect(container.firstChild).toHaveStyle('margin: 8px;')
})
it('transforms first class interpolations', () => {
const Dummy = styled.div`
${() => [
'color: red;',
css`
margin: 1;
`,
]}
`
const { container } = render(<Dummy />)
expect(container.firstChild).toHaveStyle('color: red; margin: 4px;')
})
})
describe('#styled.xxx', () => {
it('supports basic tags', () => {
const Dummy = styled.div``
const { container } = render(<Dummy />)
expect(container.firstChild!.nodeName).toBe('DIV')
})
})
describe('#styled.xxxBox', () => {
it('supports box tags', () => {
const Dummy = styled.box``
const { container } = render(<Dummy m={1} />)
expect(container.firstChild!.nodeName).toBe('DIV')
expect(container.firstChild).toHaveStyle('margin: 4px;')
})
it('supports xxxBox tags', () => {
const Dummy = styled.headerBox``
const { container } = render(<Dummy m={1} />)
expect(container.firstChild!.nodeName).toBe('HEADER')
expect(container.firstChild).toHaveStyle('margin: 4px;')
})
it("doesn't forward attributes", () => {
const Dummy = styled.box``
const { container } = render(<Dummy margin={1} />)
expect(container.firstChild!.nodeName).toBe('DIV')
expect(container.firstChild).toHaveStyle('margin: 4px;')
expect(container.firstChild).not.toHaveAttribute('margin')
})
it('supports as prop', () => {
const Dummy = styled.divBox``
// This is not supported by emotion
// @ts-expect-error
const { container } = render(<Dummy as="a" margin={1} href="ok" />)
expect(container.firstChild!.nodeName).toBe('A')
expect(container.firstChild).toHaveStyle('margin: 4px;')
expect(container.firstChild).not.toHaveAttribute('margin')
})
})