Skip to content

Commit a7fc795

Browse files
committed
applyStyles: Adds tests
1 parent a76a2bf commit a7fc795

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import applyStyles from './applyStyles'
2+
3+
describe('applyStyles', () => {
4+
describe('given arbitrary component props', () => {
5+
const props = {
6+
unknownProp: '20px',
7+
padding: null,
8+
margin: undefined,
9+
10+
gap: 10,
11+
templateCols: '250px',
12+
flexDirection: 'column',
13+
justifyContentMd: 'flex-start',
14+
placeLgDown: 'baseline',
15+
alignItemsSmOnly: 'center',
16+
}
17+
const css = applyStyles(props)
18+
19+
it('should ignore unknown props', () => {
20+
expect(css).not.toContain('uknownProp')
21+
})
22+
23+
it('should ignore known props with falsy values', () => {
24+
expect(css).not.toContain('padding')
25+
expect(css).not.toContain('margin')
26+
})
27+
28+
it('should produce CSS properties based on known props', () => {
29+
expect(css).toContain('flex-direction:column')
30+
})
31+
32+
it('should apply custom transformation function to known props', () => {
33+
expect(css).toContain('grid-gap:10px')
34+
expect(css).toContain('grid-template-columns:250px')
35+
})
36+
37+
it('should wrap responsive prop with "up" behavior in media query', () => {
38+
expect(css).toContain(
39+
'@media (min-width:768px) {justify-content:flex-start;}',
40+
)
41+
})
42+
43+
it('should wrap responsive prop with "down" behavior in media query', () => {
44+
expect(css).toContain('@media (max-width:1199px) {place-self:baseline;}')
45+
})
46+
47+
it('should wrap responsive prop with "only" behavior in media query', () => {
48+
expect(css).toContain(
49+
'@media (min-width:576px) and (max-width:767px) {align-items:center;}',
50+
)
51+
})
52+
})
53+
})

src/utils/styles/applyStyles/applyStyles.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const createStyleString = (
2828
: styleProps
2929
}
3030

31+
/**
32+
* Produces a CSS string based on the given component props.
33+
* Takes only known prop aliases, ignores all the other props.
34+
*/
3135
export default function applyStyles(pristineProps: Props): string {
3236
return (
3337
Object.keys(pristineProps)

0 commit comments

Comments
 (0)