Skip to content

Commit 6399dc5

Browse files
authored
add test for accidental replace in v10.8.1 (#1571)
* add test for accidental replace in v10.8.1 Issue: #1565 * revert unwanted format
1 parent 7596b8d commit 6399dc5

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

packages/react-jss/src/createUseStyles.test.js

+137
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/* eslint-disable react/prop-types */
22

33
import * as React from 'react'
4+
import TestRenderer from 'react-test-renderer'
45
import {renderToString} from 'react-dom/server'
56
import expect from 'expect.js'
67
import {stripIndent} from 'common-tags'
78
import createCommonBaseTests from '../test-utils/createCommonBaseTests'
9+
import {getCss, getStyle, removeWhitespace, resetSheets} from '../../../tests/utils'
810
import {createUseStyles, JssProvider, SheetsRegistry} from '.'
911

1012
const createStyledComponent = (styles, options) => {
@@ -41,4 +43,139 @@ describe('React-JSS: createUseStyles', () => {
4143
`)
4244
})
4345
})
46+
47+
describe('multiple components that share same hook', () => {
48+
const useStyles = createUseStyles({
49+
item: (props) => ({
50+
color: props.active ? 'red' : 'blue',
51+
'&:hover': {
52+
fontSize: 60
53+
}
54+
})
55+
})
56+
57+
function Item({active = false, onClick, children}) {
58+
const classes = useStyles({active})
59+
return (
60+
<button type="button" className={classes.item} onClick={onClick}>
61+
{children}
62+
</button>
63+
)
64+
}
65+
66+
function App() {
67+
const [activeKey, setActiveKey] = React.useState(1)
68+
return (
69+
<main>
70+
{[1, 2].map(key => (
71+
<Item key={key} active={key === activeKey} onClick={() => setActiveKey(key)}>
72+
{key}
73+
</Item>
74+
))}
75+
</main>
76+
)
77+
}
78+
79+
beforeEach(resetSheets())
80+
81+
let registry
82+
let root
83+
beforeEach(() => {
84+
registry = new SheetsRegistry()
85+
86+
TestRenderer.act(() => {
87+
root = TestRenderer.create(
88+
<JssProvider registry={registry} generateId={rule => `${rule.key}-id`}>
89+
<App />
90+
</JssProvider>
91+
)
92+
})
93+
})
94+
95+
describe('initial rendering', () => {
96+
it('should return correct registry.toString', () => {
97+
expect(registry.toString()).to.be(stripIndent`
98+
.item-id {}
99+
.item-d0-id {
100+
color: red;
101+
}
102+
.item-d0-id:hover {
103+
font-size: 60px;
104+
}
105+
.item-d1-id {
106+
color: blue;
107+
}
108+
.item-d1-id:hover {
109+
font-size: 60px;
110+
}
111+
`)
112+
})
113+
114+
it('should render correct CSS in DOM', () => {
115+
const style = getStyle()
116+
expect(getCss(style)).to.be(
117+
removeWhitespace(stripIndent`
118+
.item-id {}
119+
.item-d0-id {
120+
color: red;
121+
}
122+
.item-d0-id:hover {
123+
font-size: 60px;
124+
}
125+
.item-d1-id {
126+
color: blue;
127+
}
128+
.item-d1-id:hover {
129+
font-size: 60px;
130+
}
131+
`)
132+
)
133+
})
134+
})
135+
136+
describe('update via click', () => {
137+
beforeEach(() => {
138+
TestRenderer.act(() => {
139+
root.root.findAllByType('button')[1].props.onClick()
140+
})
141+
})
142+
143+
const eachRules = [
144+
'.item-id {}',
145+
stripIndent`
146+
.item-d0-id {
147+
color: blue;
148+
}
149+
`,
150+
stripIndent`
151+
.item-d0-id:hover {
152+
font-size: 60px;
153+
}
154+
`,
155+
stripIndent`
156+
.item-d1-id {
157+
color: red;
158+
}
159+
`,
160+
stripIndent`
161+
.item-d1-id:hover {
162+
font-size: 60px;
163+
}
164+
`
165+
]
166+
167+
describe('check each rules', () => {
168+
eachRules.forEach(rule => {
169+
it(`should contain ${rule} in registry.toString()`, () => {
170+
expect(registry.toString()).to.contain(rule)
171+
})
172+
173+
it(`should render ${rule} in DOM`, () => {
174+
const style = getStyle()
175+
expect(getCss(style)).to.contain(removeWhitespace(rule))
176+
})
177+
})
178+
})
179+
})
180+
})
44181
})

0 commit comments

Comments
 (0)