|
| 1 | +import { transform } from '../../test-utils'; |
| 2 | + |
| 3 | +describe('xcss prop transformation', () => { |
| 4 | + it('should transform static inline object', () => { |
| 5 | + const result = transform(` |
| 6 | + <Component xcss={{ color: 'red' }} /> |
| 7 | + `); |
| 8 | + |
| 9 | + expect(result).toMatchInlineSnapshot(` |
| 10 | + "import * as React from "react"; |
| 11 | + import { ax, ix, CC, CS } from "@compiled/react/runtime"; |
| 12 | + const _ = "._syaz5scu{color:red}"; |
| 13 | + <CC> |
| 14 | + <CS>{[_]}</CS> |
| 15 | + {<Component xcss={"_syaz5scu"} />} |
| 16 | + </CC>; |
| 17 | + " |
| 18 | + `); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should throw when not static', () => { |
| 22 | + expect(() => { |
| 23 | + transform( |
| 24 | + ` |
| 25 | + import { bar } from './foo'; |
| 26 | +
|
| 27 | + <Component xcss={{ color: bar }} /> |
| 28 | + `, |
| 29 | + { highlightCode: false } |
| 30 | + ); |
| 31 | + }).toThrowErrorMatchingInlineSnapshot(` |
| 32 | + "unknown file: Object given to the xcss prop must be static (4:23). |
| 33 | + 2 | import { bar } from './foo'; |
| 34 | + 3 | |
| 35 | + > 4 | <Component xcss={{ color: bar }} /> |
| 36 | + | ^^^^^^^^^^^^^^ |
| 37 | + 5 | " |
| 38 | + `); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should transform named xcss prop usage', () => { |
| 42 | + const result = transform(` |
| 43 | + <Component innerXcss={{ color: 'red' }} /> |
| 44 | + `); |
| 45 | + |
| 46 | + expect(result).toMatchInlineSnapshot(` |
| 47 | + "import * as React from "react"; |
| 48 | + import { ax, ix, CC, CS } from "@compiled/react/runtime"; |
| 49 | + const _ = "._syaz5scu{color:red}"; |
| 50 | + <CC> |
| 51 | + <CS>{[_]}</CS> |
| 52 | + {<Component innerXcss={"_syaz5scu"} />} |
| 53 | + </CC>; |
| 54 | + " |
| 55 | + `); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should work with css map', () => { |
| 59 | + const result = transform(` |
| 60 | + import { cssMap } from '@compiled/react'; |
| 61 | +
|
| 62 | + const styles = cssMap({ |
| 63 | + primary: { color: 'red' }, |
| 64 | + }); |
| 65 | +
|
| 66 | + <Component xcss={styles.primary} /> |
| 67 | + `); |
| 68 | + |
| 69 | + expect(result).toMatchInlineSnapshot(` |
| 70 | + "import * as React from "react"; |
| 71 | + import { ax, ix, CC, CS } from "@compiled/react/runtime"; |
| 72 | + const _ = "._syaz5scu{color:red}"; |
| 73 | + const styles = { |
| 74 | + primary: "_syaz5scu", |
| 75 | + }; |
| 76 | + <CC> |
| 77 | + <CS>{[_]}</CS> |
| 78 | + {<Component xcss={styles.primary} />} |
| 79 | + </CC>; |
| 80 | + " |
| 81 | + `); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should allow ternaries', () => { |
| 85 | + const result = transform(` |
| 86 | + import { cssMap } from '@compiled/react'; |
| 87 | +
|
| 88 | + const styles = cssMap({ |
| 89 | + primary: { color: 'red' }, |
| 90 | + secondary: { color: 'blue' } |
| 91 | + }); |
| 92 | +
|
| 93 | + <Component xcss={isPrimary ? styles.primary : styles.secondary} /> |
| 94 | + `); |
| 95 | + |
| 96 | + expect(result).toMatchInlineSnapshot(` |
| 97 | + "import * as React from "react"; |
| 98 | + import { ax, ix, CC, CS } from "@compiled/react/runtime"; |
| 99 | + const _2 = "._syaz13q2{color:blue}"; |
| 100 | + const _ = "._syaz5scu{color:red}"; |
| 101 | + const styles = { |
| 102 | + primary: "_syaz5scu", |
| 103 | + secondary: "_syaz13q2", |
| 104 | + }; |
| 105 | + <CC> |
| 106 | + <CS>{[_, _2]}</CS> |
| 107 | + {<Component xcss={isPrimary ? styles.primary : styles.secondary} />} |
| 108 | + </CC>; |
| 109 | + " |
| 110 | + `); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should allow concatenating styles', () => { |
| 114 | + const result = transform(` |
| 115 | + import { cssMap, j } from '@compiled/react'; |
| 116 | +
|
| 117 | + const styles = cssMap({ |
| 118 | + primary: { color: 'red' }, |
| 119 | + secondary: { color: 'blue' } |
| 120 | + }); |
| 121 | +
|
| 122 | + <Component xcss={j(isPrimary && styles.primary, !isPrimary && styles.secondary)} /> |
| 123 | + `); |
| 124 | + |
| 125 | + expect(result).toMatchInlineSnapshot(` |
| 126 | + "import * as React from "react"; |
| 127 | + import { ax, ix, CC, CS } from "@compiled/react/runtime"; |
| 128 | + import { j } from "@compiled/react"; |
| 129 | + const _2 = "._syaz13q2{color:blue}"; |
| 130 | + const _ = "._syaz5scu{color:red}"; |
| 131 | + const styles = { |
| 132 | + primary: "_syaz5scu", |
| 133 | + secondary: "_syaz13q2", |
| 134 | + }; |
| 135 | + <CC> |
| 136 | + <CS>{[_, _2]}</CS> |
| 137 | + { |
| 138 | + <Component |
| 139 | + xcss={j(isPrimary && styles.primary, !isPrimary && styles.secondary)} |
| 140 | + /> |
| 141 | + } |
| 142 | + </CC>; |
| 143 | + " |
| 144 | + `); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should ignore xcss prop when compiled must be in scope', () => { |
| 148 | + const result = transform( |
| 149 | + ` |
| 150 | + <Component xcss={{ color: 'red' }} /> |
| 151 | + `, |
| 152 | + { requireCompiledInScopeForXCSSProp: true } |
| 153 | + ); |
| 154 | + |
| 155 | + expect(result).toMatchInlineSnapshot(` |
| 156 | + "<Component |
| 157 | + xcss={{ |
| 158 | + color: "red", |
| 159 | + }} |
| 160 | + />; |
| 161 | + " |
| 162 | + `); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should transform xcss prop when compiled is in scope', () => { |
| 166 | + const result = transform( |
| 167 | + ` |
| 168 | + import { cssMap } from '@compiled/react'; |
| 169 | +
|
| 170 | + const styles = cssMap({ |
| 171 | + primary: { color: 'red' }, |
| 172 | + }); |
| 173 | +
|
| 174 | + <Component xcss={styles.primary} /> |
| 175 | + `, |
| 176 | + { requireCompiledInScopeForXCSSProp: true } |
| 177 | + ); |
| 178 | + |
| 179 | + expect(result).toMatchInlineSnapshot(` |
| 180 | + "import * as React from "react"; |
| 181 | + import { ax, ix, CC, CS } from "@compiled/react/runtime"; |
| 182 | + const _ = "._syaz5scu{color:red}"; |
| 183 | + const styles = { |
| 184 | + primary: "_syaz5scu", |
| 185 | + }; |
| 186 | + <CC> |
| 187 | + <CS>{[_]}</CS> |
| 188 | + {<Component xcss={styles.primary} />} |
| 189 | + </CC>; |
| 190 | + " |
| 191 | + `); |
| 192 | + }); |
| 193 | +}); |
0 commit comments