|
| 1 | +import type { TransformOptions } from '../../test-utils'; |
| 2 | +import { transform as transformCode } from '../../test-utils'; |
| 3 | +import { ErrorMessages } from '../../utils/css-map'; |
| 4 | + |
| 5 | +// Add an example element so we can check the raw CSS styles |
| 6 | +const EXAMPLE_USAGE = 'const Element = (variant) => <div css={styles[variant]} />;'; |
| 7 | + |
| 8 | +describe('css map advanced functionality (at rules, selectors object)', () => { |
| 9 | + const transform = (code: string, opts: TransformOptions = {}) => |
| 10 | + transformCode(code, { pretty: false, ...opts }); |
| 11 | + |
| 12 | + it('should parse a mix of at rules and the selectors object', () => { |
| 13 | + const actual = transform(` |
| 14 | + import { cssMap } from '@compiled/react'; |
| 15 | +
|
| 16 | + const styles = cssMap({ |
| 17 | + success: { |
| 18 | + color: '#0b0', |
| 19 | + '&:hover': { |
| 20 | + color: '#060', |
| 21 | + }, |
| 22 | + '@media': { |
| 23 | + 'screen and (min-width: 500px)': { |
| 24 | + fontSize: '10vw', |
| 25 | + }, |
| 26 | + }, |
| 27 | + selectors: { |
| 28 | + span: { |
| 29 | + color: 'lightgreen', |
| 30 | + '&:hover': { |
| 31 | + color: '#090', |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + danger: { |
| 37 | + color: 'red', |
| 38 | + '&:hover': { |
| 39 | + color: 'darkred', |
| 40 | + }, |
| 41 | + '@media': { |
| 42 | + 'screen and (min-width: 500px)': { |
| 43 | + fontSize: '20vw', |
| 44 | + }, |
| 45 | + }, |
| 46 | + selectors: { |
| 47 | + span: { |
| 48 | + color: 'orange', |
| 49 | + '&:hover': { |
| 50 | + color: 'pink', |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }); |
| 56 | +
|
| 57 | + ${EXAMPLE_USAGE} |
| 58 | + `); |
| 59 | + |
| 60 | + expect(actual).toIncludeMultiple([ |
| 61 | + // Styles from success variant |
| 62 | + '._syazjafr{color:#0b0}', |
| 63 | + '._30l3aebp:hover{color:#060}', |
| 64 | + '@media screen and (min-width:500px){._1takoyl8{font-size:10vw}}', |
| 65 | + '._1tjq1v9d span{color:lightgreen}', |
| 66 | + '._yzbcy77s span:hover{color:#090}', |
| 67 | + |
| 68 | + // Styles from danger variant |
| 69 | + '._syaz5scu{color:red}', |
| 70 | + '._30l3qaj3:hover{color:darkred}', |
| 71 | + '@media screen and (min-width:500px){._1taki9ra{font-size:20vw}}', |
| 72 | + '._1tjqruxl span{color:orange}', |
| 73 | + '._yzbc32ev span:hover{color:pink}', |
| 74 | + |
| 75 | + 'const styles={success:"_syazjafr _30l3aebp _1takoyl8 _1tjq1v9d _yzbcy77s",danger:"_syaz5scu _30l3qaj3 _1taki9ra _1tjqruxl _yzbc32ev"}', |
| 76 | + ]); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should parse selectors object', () => { |
| 80 | + const actual = transform(` |
| 81 | + import { cssMap } from '@compiled/react'; |
| 82 | +
|
| 83 | + const styles = cssMap({ |
| 84 | + success: { |
| 85 | + color: '#0b0', |
| 86 | + '&:hover': { |
| 87 | + color: '#060', |
| 88 | + }, |
| 89 | + }, |
| 90 | + danger: { |
| 91 | + color: 'red', |
| 92 | + selectors: { |
| 93 | + '&:first-of-type': { |
| 94 | + color: 'lightgreen', |
| 95 | + '&:hover': { |
| 96 | + color: '#090', |
| 97 | + }, |
| 98 | + }, |
| 99 | + // Hover on child element |
| 100 | + '& :hover': { |
| 101 | + color: 'orange', |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }); |
| 106 | +
|
| 107 | + ${EXAMPLE_USAGE} |
| 108 | + `); |
| 109 | + |
| 110 | + expect(actual).toIncludeMultiple([ |
| 111 | + // Styles from success variant |
| 112 | + '._syazjafr{color:#0b0}', |
| 113 | + '._30l3aebp:hover{color:#060}', |
| 114 | + |
| 115 | + // Styles from danger variant |
| 116 | + '._syaz5scu{color:red}', |
| 117 | + '._pnmb1v9d:first-of-type{color:lightgreen}', |
| 118 | + '._p685y77s:first-of-type:hover{color:#090}', |
| 119 | + '._838lruxl :hover{color:orange}', |
| 120 | + |
| 121 | + 'const styles={success:"_syazjafr _30l3aebp",danger:"_syaz5scu _pnmb1v9d _p685y77s _838lruxl"}', |
| 122 | + ]); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should error if duplicate selectors passed (inside selectors object and outside)', () => { |
| 126 | + expect(() => { |
| 127 | + transform(` |
| 128 | + import { cssMap } from '@compiled/react'; |
| 129 | +
|
| 130 | + const styles = cssMap({ |
| 131 | + success: { |
| 132 | + color: '#0b0', |
| 133 | + '&:hover': { |
| 134 | + color: '#060', |
| 135 | + }, |
| 136 | + selectors: { |
| 137 | + '&:hover': { |
| 138 | + color: '#ff0', |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }); |
| 143 | + `); |
| 144 | + }).toThrow(ErrorMessages.DUPLICATE_SELECTOR); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should error if duplicate selectors passed using different formats (mixing an identifier and a string literal)', () => { |
| 148 | + expect(() => { |
| 149 | + transform(` |
| 150 | + import { cssMap } from '@compiled/react'; |
| 151 | +
|
| 152 | + const styles = cssMap({ |
| 153 | + success: { |
| 154 | + color: '#0b0', |
| 155 | + // This wouldn't pass the type-checking anyway |
| 156 | + div: { |
| 157 | + color: '#060', |
| 158 | + }, |
| 159 | + selectors: { |
| 160 | + 'div': { |
| 161 | + color: '#ff0', |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }); |
| 166 | + `); |
| 167 | + }).toThrow(ErrorMessages.DUPLICATE_SELECTOR); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should error if selector targeting current element is passed without ampersand at front', () => { |
| 171 | + // :hover (by itself) is identical to &:hover, believe it or not! |
| 172 | + // This is due to the parent-orphaned-pseudos plugin in @compiled/css. |
| 173 | + expect(() => { |
| 174 | + transform(` |
| 175 | + import { cssMap } from '@compiled/react'; |
| 176 | +
|
| 177 | + const styles = cssMap({ |
| 178 | + success: { |
| 179 | + color: '#0b0', |
| 180 | + selectors: { |
| 181 | + ':hover': { |
| 182 | + color: 'aquamarine', |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }); |
| 187 | + `); |
| 188 | + }).toThrow(ErrorMessages.USE_SELECTORS_WITH_AMPERSAND); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should error if duplicate selectors passed using both the forms `&:hover` and `:hover`', () => { |
| 192 | + expect(() => { |
| 193 | + transform(` |
| 194 | + import { cssMap } from '@compiled/react'; |
| 195 | +
|
| 196 | + const styles = cssMap({ |
| 197 | + success: { |
| 198 | + color: '#0b0', |
| 199 | + '&:hover': { |
| 200 | + color: 'cyan', |
| 201 | + }, |
| 202 | + selectors: { |
| 203 | + ':hover': { |
| 204 | + color: 'aquamarine', |
| 205 | + }, |
| 206 | + }, |
| 207 | + }, |
| 208 | + }); |
| 209 | + `); |
| 210 | + }).toThrow(ErrorMessages.USE_SELECTORS_WITH_AMPERSAND); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should not error if selector has same name as property', () => { |
| 214 | + const actual = transform(` |
| 215 | + import { cssMap } from '@compiled/react'; |
| 216 | +
|
| 217 | + const styles = cssMap({ |
| 218 | + success: { |
| 219 | + color: '#0b0', |
| 220 | + // All bets are off when we do not know what constitutes |
| 221 | + // a valid selector, so we give up in the selectors key |
| 222 | + selectors: { |
| 223 | + color: { |
| 224 | + color: 'pink', |
| 225 | + }, |
| 226 | + fontSize: { |
| 227 | + background: 'blue', |
| 228 | + }, |
| 229 | + }, |
| 230 | + fontSize: '50px', |
| 231 | + }, |
| 232 | + }); |
| 233 | +
|
| 234 | + ${EXAMPLE_USAGE} |
| 235 | + `); |
| 236 | + |
| 237 | + expect(actual).toIncludeMultiple([ |
| 238 | + '._syazjafr{color:#0b0}', |
| 239 | + '._14jq32ev color{color:pink}', |
| 240 | + '._1wsc13q2 fontSize{background-color:blue}', |
| 241 | + '._1wyb12am{font-size:50px}', |
| 242 | + |
| 243 | + 'const styles={success:"_syazjafr _1wyb12am _14jq32ev _1wsc13q2"}', |
| 244 | + ]); |
| 245 | + }); |
| 246 | + |
| 247 | + it('should parse an at rule (@media)', () => { |
| 248 | + const permutations: string[] = [`screen`, `'screen'`]; |
| 249 | + |
| 250 | + for (const secondHalf of permutations) { |
| 251 | + const actual = transform(` |
| 252 | + import { cssMap } from '@compiled/react'; |
| 253 | +
|
| 254 | + const styles = cssMap({ |
| 255 | + success: { |
| 256 | + color: 'red', |
| 257 | + '@media': { |
| 258 | + 'screen and (min-width: 500px)': { |
| 259 | + color: 'blue', |
| 260 | + }, |
| 261 | + ${secondHalf}: { |
| 262 | + color: 'pink', |
| 263 | + }, |
| 264 | + }, |
| 265 | + }, |
| 266 | + }); |
| 267 | +
|
| 268 | + ${EXAMPLE_USAGE} |
| 269 | + `); |
| 270 | + |
| 271 | + expect(actual).toIncludeMultiple([ |
| 272 | + '._syaz5scu{color:red}', |
| 273 | + '@media screen and (min-width:500px){._1qhm13q2{color:blue}}', |
| 274 | + '@media screen{._434732ev{color:pink}}', |
| 275 | + |
| 276 | + 'const styles={success:"_syaz5scu _1qhm13q2 _434732ev"}', |
| 277 | + ]); |
| 278 | + } |
| 279 | + }); |
| 280 | + |
| 281 | + it('should error if more than one selectors key passed', () => { |
| 282 | + expect(() => { |
| 283 | + transform(` |
| 284 | + import { cssMap } from '@compiled/react'; |
| 285 | +
|
| 286 | + const styles = cssMap({ |
| 287 | + success: { |
| 288 | + color: 'red', |
| 289 | + selectors: { |
| 290 | + '&:hover': { |
| 291 | + color: '#ff0', |
| 292 | + }, |
| 293 | + }, |
| 294 | + selectors: { |
| 295 | + '&:active': { |
| 296 | + color: '#0ff', |
| 297 | + }, |
| 298 | + }, |
| 299 | + }, |
| 300 | + }); |
| 301 | + `); |
| 302 | + }).toThrow(ErrorMessages.DUPLICATE_SELECTORS_BLOCK); |
| 303 | + }); |
| 304 | + |
| 305 | + it('should error if value of selectors key is not an object', () => { |
| 306 | + expect(() => { |
| 307 | + transform(` |
| 308 | + import { cssMap } from '@compiled/react'; |
| 309 | +
|
| 310 | + const styles = cssMap({ |
| 311 | + success: { |
| 312 | + color: 'red', |
| 313 | + selectors: 'blue', |
| 314 | + }, |
| 315 | + }); |
| 316 | + `); |
| 317 | + }).toThrow(ErrorMessages.SELECTORS_BLOCK_VALUE_TYPE); |
| 318 | + }); |
| 319 | +}); |
0 commit comments