Skip to content

Commit bb9b0c7

Browse files
committed
Add test for updated nested rules
1 parent 915a4e3 commit bb9b0c7

1 file changed

Lines changed: 115 additions & 18 deletions

File tree

src/tests/functional.spec.jsx

Lines changed: 115 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,169 @@ import React from 'react'
33
import {stripIndent} from 'common-tags'
44
import {mount} from 'enzyme'
55

6-
import styled from '../'
7-
86
import CreateApp from './App'
97

8+
let Styled
9+
let styled
10+
11+
const mockNameGenerators = () => {
12+
let styledCounter = 0
13+
let jssCounter = 0
14+
15+
jest.mock('../utils/generateTagName')
16+
jest.mock('jss/lib/utils/generateClassName')
17+
18+
const generateTagName = require('../utils/generateTagName').default
19+
const generateClassName = require('jss/lib/utils/generateClassName').default
20+
21+
generateTagName.mockImplementation((tagName: string) => `${tagName}-${++styledCounter}`)
22+
generateClassName.mockImplementation(rule => `${rule.name}-${++jssCounter}`)
23+
}
24+
1025
describe('functional tests', () => {
26+
beforeEach(() => {
27+
mockNameGenerators()
28+
29+
Styled = require('../').Styled
30+
styled = Styled()
31+
})
32+
1133
it('should update props and unmount', () => {
1234
const App = CreateApp(styled)
1335
const wrapper = mount(<App />)
1436
const sheet = styled.mountSheet()
1537

1638
expect(sheet.toString()).toBe(stripIndent`
17-
.div-1-0-0 {
39+
.div-1-1 {
1840
margin: 50px;
1941
}
20-
.header-2-0-1 {
42+
.header-2-2 {
2143
padding: 10px;
2244
}
23-
.h1-5-0-2 {
45+
.h1-5-3 {
2446
color: red;
2547
}
26-
.section-3-0-3 {
48+
.section-3-4 {
2749
color: red;
2850
}
29-
.button-6-0-4 {
51+
.button-6-5 {
3052
margin: 0;
3153
}
32-
.button-7-0-5 {
54+
.button-7-6 {
3355
margin: 0;
3456
}
35-
.button-8-0-6 {
57+
.button-8-7 {
3658
margin: 10px;
3759
}
38-
.section-4-0-7 {
60+
.section-4-8 {
3961
color: yellow;
4062
}
4163
`)
4264

4365
wrapper.setProps({margin: 20})
4466

4567
expect(sheet.toString()).toBe(stripIndent`
46-
.div-1-0-0 {
68+
.div-1-1 {
4769
margin: 50px;
4870
}
49-
.header-2-0-1 {
71+
.header-2-2 {
5072
padding: 10px;
5173
}
52-
.h1-5-0-2 {
74+
.h1-5-3 {
5375
color: red;
5476
}
55-
.section-3-0-3 {
77+
.section-3-4 {
5678
color: red;
5779
}
58-
.button-6-0-4 {
80+
.button-6-5 {
5981
margin: 0;
6082
}
61-
.button-7-0-5 {
83+
.button-7-6 {
6284
margin: 0;
6385
}
64-
.button-8-0-6 {
86+
.button-8-7 {
6587
margin: 20px;
6688
}
67-
.section-4-0-7 {
89+
.section-4-8 {
6890
color: yellow;
6991
}
7092
`)
7193

7294
wrapper.unmount()
7395
})
96+
97+
it('should update nested props', () => {
98+
styled = Styled({
99+
button: {
100+
fontSize: 12,
101+
},
102+
})
103+
104+
const Div = styled('div')({
105+
padding: 15,
106+
'&:hover': {
107+
'& $button': {
108+
color: ({primary}) => (primary ? 'red' : 'green'),
109+
},
110+
},
111+
})
112+
113+
const Button = styled('button')({
114+
composes: '$button',
115+
})
116+
117+
const App = (props: {primary?: boolean}) => (
118+
<div>
119+
<Div primary={props.primary}>
120+
<Button>Button</Button>
121+
</Div>
122+
<Div primary={!props.primary}>
123+
<Button>Button</Button>
124+
</Div>
125+
</div>
126+
)
127+
128+
const wrapper = mount(<App />)
129+
const sheet = styled.mountSheet()
130+
131+
expect(sheet.toString()).toBe(stripIndent`
132+
.button-1 {
133+
font-size: 12px;
134+
}
135+
.div-1-2 {
136+
padding: 15px;
137+
}
138+
.div-1-2:hover .button-1 {
139+
color: green;
140+
}
141+
.div-3-5:hover .button-1 {
142+
color: green;
143+
}
144+
.div-4-9:hover .button-1 {
145+
color: red;
146+
}
147+
`)
148+
149+
wrapper.setProps({primary: true})
150+
151+
expect(sheet.toString()).toBe(stripIndent`
152+
.button-1 {
153+
font-size: 12px;
154+
}
155+
.div-1-2 {
156+
padding: 15px;
157+
}
158+
.div-1-2:hover .button-1 {
159+
color: red;
160+
}
161+
.div-3-5:hover .button-1 {
162+
color: red;
163+
}
164+
.div-4-9:hover .button-1 {
165+
color: green;
166+
}
167+
`)
168+
169+
wrapper.unmount()
170+
})
74171
})

0 commit comments

Comments
 (0)