|
| 1 | +import { keyframes, styled } from '@compiled/react'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | + |
| 4 | +import defaultFadeOut, { fadeOut as shadowedFadeOut, namedFadeOut } from '../__fixtures__'; |
| 5 | + |
| 6 | +const getOpacity = (str: string | number) => str; |
| 7 | + |
| 8 | +const getKeyframe = (name: string) => { |
| 9 | + const styles = Array.from( |
| 10 | + document.body.querySelectorAll('style'), |
| 11 | + (style) => style.innerHTML |
| 12 | + ).join('\n'); |
| 13 | + |
| 14 | + return styles.substring(styles.indexOf(`@keyframes ${name}`)); |
| 15 | +}; |
| 16 | + |
| 17 | +describe('keyframes', () => { |
| 18 | + describe('referenced through a css prop', () => { |
| 19 | + describe('render an animation', () => { |
| 20 | + it('given an object call expression argument', () => { |
| 21 | + const fadeOut = keyframes({ |
| 22 | + from: { |
| 23 | + opacity: 1, |
| 24 | + }, |
| 25 | + to: { |
| 26 | + opacity: 0, |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + const { getByText } = render(<div css={{ animationName: fadeOut }}>hello world</div>); |
| 31 | + |
| 32 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1m8j3od'); |
| 33 | + expect(getKeyframe('k1m8j3od')).toMatchInlineSnapshot( |
| 34 | + `"@keyframes k1m8j3od{0%{opacity:1}to{opacity:0}}"` |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it('given a template literal call expression argument', () => { |
| 39 | + const fadeOut = keyframes(`from { opacity: 1; } to { opacity: 0; }`); |
| 40 | + const { getByText } = render(<div css={{ animationName: fadeOut }}>hello world</div>); |
| 41 | + |
| 42 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'klmf72q'); |
| 43 | + expect(getKeyframe('klmf72q')).toMatchInlineSnapshot( |
| 44 | + `"@keyframes klmf72q{0%{opacity:1}to{opacity:0}}"` |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it('given a string call expression argument', () => { |
| 49 | + const fadeOut = keyframes('from { opacity: 1; } to { opacity: 0; }'); |
| 50 | + const { getByText } = render(<div css={{ animationName: fadeOut }}>hello world</div>); |
| 51 | + |
| 52 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1b0zjii'); |
| 53 | + expect(getKeyframe('k1b0zjii')).toMatchInlineSnapshot( |
| 54 | + `"@keyframes k1b0zjii{0%{opacity:1}to{opacity:0}}"` |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('given a tagged template expression', () => { |
| 59 | + const fadeOut = keyframes` |
| 60 | + from { opacity: 1; } |
| 61 | + to { opacity: 0; } |
| 62 | + `; |
| 63 | + |
| 64 | + const { getByText } = render(<div css={{ animationName: fadeOut }}>hello world</div>); |
| 65 | + |
| 66 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1vk0ha6'); |
| 67 | + expect(getKeyframe('k1vk0ha6')).toMatchInlineSnapshot( |
| 68 | + `"@keyframes k1vk0ha6{0%{opacity:1}to{opacity:0}}"` |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('defined in a default import', () => { |
| 73 | + const { getByText } = render( |
| 74 | + <div css={{ animationName: defaultFadeOut }}>hello world</div> |
| 75 | + ); |
| 76 | + |
| 77 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1m8j3od'); |
| 78 | + expect(getKeyframe('k1m8j3od')).toMatchInlineSnapshot( |
| 79 | + `"@keyframes k1m8j3od{0%{opacity:1}to{opacity:0}}"` |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it('defined in an imported named import', () => { |
| 84 | + const { getByText } = render(<div css={{ animationName: namedFadeOut }}>hello world</div>); |
| 85 | + |
| 86 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1m8j3od'); |
| 87 | + expect(getKeyframe('k1m8j3od')).toMatchInlineSnapshot( |
| 88 | + `"@keyframes k1m8j3od{0%{opacity:1}to{opacity:0}}"` |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('defined in a local named import', () => { |
| 93 | + const { getByText } = render( |
| 94 | + <div css={{ animationName: shadowedFadeOut }}>hello world</div> |
| 95 | + ); |
| 96 | + |
| 97 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1m8j3od'); |
| 98 | + expect(getKeyframe('k1m8j3od')).toMatchInlineSnapshot( |
| 99 | + `"@keyframes k1m8j3od{0%{opacity:1}to{opacity:0}}"` |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('containing a call expression', () => { |
| 104 | + const from = 1; |
| 105 | + const to = 0; |
| 106 | + |
| 107 | + const fadeOut = keyframes({ |
| 108 | + from: { |
| 109 | + opacity: getOpacity(from), |
| 110 | + }, |
| 111 | + to: { |
| 112 | + opacity: getOpacity(to), |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + const { getByText } = render(<div css={{ animationName: fadeOut }}>hello world</div>); |
| 117 | + |
| 118 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'kbrsk95'); |
| 119 | + expect(getKeyframe('kbrsk95')).toMatchInlineSnapshot( |
| 120 | + `"@keyframes kbrsk95{0%{opacity:1}to{opacity:0}}"` |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it('containing an identifier referencing a constant numeric literal', () => { |
| 125 | + const fromOpacity = 1; |
| 126 | + const toOpacity = 0; |
| 127 | + |
| 128 | + const fadeOut = keyframes({ |
| 129 | + from: { |
| 130 | + opacity: fromOpacity, |
| 131 | + }, |
| 132 | + to: { |
| 133 | + opacity: toOpacity, |
| 134 | + }, |
| 135 | + }); |
| 136 | + |
| 137 | + const { getByText } = render(<div css={{ animationName: fadeOut }}>hello world</div>); |
| 138 | + |
| 139 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'korwhog'); |
| 140 | + expect(getKeyframe('korwhog')).toMatchInlineSnapshot( |
| 141 | + `"@keyframes korwhog{0%{opacity:1}to{opacity:0}}"` |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + it('containing an identifier referencing a call expression', () => { |
| 146 | + const fromOpacity = getOpacity(1); |
| 147 | + const toOpacity = getOpacity(0); |
| 148 | + |
| 149 | + const fadeOut = keyframes({ |
| 150 | + from: { |
| 151 | + opacity: fromOpacity, |
| 152 | + }, |
| 153 | + to: { |
| 154 | + opacity: toOpacity, |
| 155 | + }, |
| 156 | + }); |
| 157 | + |
| 158 | + const { getByText } = render(<div css={{ animationName: fadeOut }}>hello world</div>); |
| 159 | + |
| 160 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'korwhog'); |
| 161 | + expect(getKeyframe('korwhog')).toMatchInlineSnapshot( |
| 162 | + `"@keyframes korwhog{0%{opacity:1}to{opacity:0}}"` |
| 163 | + ); |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + describe('referenced through a styled component', () => { |
| 169 | + describe('render an animation', () => { |
| 170 | + it('given an object call expression argument', () => { |
| 171 | + const fadeOut = keyframes({ |
| 172 | + from: { |
| 173 | + opacity: 1, |
| 174 | + }, |
| 175 | + to: { |
| 176 | + opacity: 0, |
| 177 | + }, |
| 178 | + }); |
| 179 | + |
| 180 | + const Component = styled.div({ animationName: fadeOut }); |
| 181 | + |
| 182 | + const { getByText } = render(<Component>hello world</Component>); |
| 183 | + |
| 184 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1m8j3od'); |
| 185 | + expect(getKeyframe('k1m8j3od')).toMatchInlineSnapshot( |
| 186 | + `"@keyframes k1m8j3od{0%{opacity:1}to{opacity:0}}"` |
| 187 | + ); |
| 188 | + }); |
| 189 | + |
| 190 | + it('given a template literal call expression argument', () => { |
| 191 | + const fadeOut = keyframes(`from { opacity: 1; } to { opacity: 0; }`); |
| 192 | + const Component = styled.div({ animationName: fadeOut }); |
| 193 | + const { getByText } = render(<Component>hello world</Component>); |
| 194 | + |
| 195 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'klmf72q'); |
| 196 | + expect(getKeyframe('klmf72q')).toMatchInlineSnapshot( |
| 197 | + `"@keyframes klmf72q{0%{opacity:1}to{opacity:0}}"` |
| 198 | + ); |
| 199 | + }); |
| 200 | + |
| 201 | + it('given a string call expression argument', () => { |
| 202 | + const fadeOut = keyframes('from { opacity: 1; } to { opacity: 0; }'); |
| 203 | + const Component = styled.div({ animationName: fadeOut }); |
| 204 | + const { getByText } = render(<Component>hello world</Component>); |
| 205 | + |
| 206 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1b0zjii'); |
| 207 | + expect(getKeyframe('k1b0zjii')).toMatchInlineSnapshot( |
| 208 | + `"@keyframes k1b0zjii{0%{opacity:1}to{opacity:0}}"` |
| 209 | + ); |
| 210 | + }); |
| 211 | + |
| 212 | + it('given a tagged template expression', () => { |
| 213 | + const fadeOut = keyframes` |
| 214 | + from { opacity: 1; } |
| 215 | + to { opacity: 0; } |
| 216 | + `; |
| 217 | + |
| 218 | + const Component = styled.div({ animationName: fadeOut }); |
| 219 | + const { getByText } = render(<Component>hello world</Component>); |
| 220 | + |
| 221 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1vk0ha6'); |
| 222 | + expect(getKeyframe('k1vk0ha6')).toMatchInlineSnapshot( |
| 223 | + `"@keyframes k1vk0ha6{0%{opacity:1}to{opacity:0}}"` |
| 224 | + ); |
| 225 | + }); |
| 226 | + |
| 227 | + it('defined in a default export', () => { |
| 228 | + const Component = styled.div({ animationName: defaultFadeOut }); |
| 229 | + const { getByText } = render(<Component>hello world</Component>); |
| 230 | + |
| 231 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1m8j3od'); |
| 232 | + expect(getKeyframe('k1m8j3od')).toMatchInlineSnapshot( |
| 233 | + `"@keyframes k1m8j3od{0%{opacity:1}to{opacity:0}}"` |
| 234 | + ); |
| 235 | + }); |
| 236 | + |
| 237 | + it('defined in an imported named import', () => { |
| 238 | + const Component = styled.div({ animationName: namedFadeOut }); |
| 239 | + const { getByText } = render(<Component>hello world</Component>); |
| 240 | + |
| 241 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1m8j3od'); |
| 242 | + expect(getKeyframe('k1m8j3od')).toMatchInlineSnapshot( |
| 243 | + `"@keyframes k1m8j3od{0%{opacity:1}to{opacity:0}}"` |
| 244 | + ); |
| 245 | + }); |
| 246 | + |
| 247 | + it('defined in a local named import', () => { |
| 248 | + const Component = styled.div({ animationName: shadowedFadeOut }); |
| 249 | + const { getByText } = render(<Component>hello world</Component>); |
| 250 | + |
| 251 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'k1m8j3od'); |
| 252 | + expect(getKeyframe('k1m8j3od')).toMatchInlineSnapshot( |
| 253 | + `"@keyframes k1m8j3od{0%{opacity:1}to{opacity:0}}"` |
| 254 | + ); |
| 255 | + }); |
| 256 | + |
| 257 | + it('containing a call expression', () => { |
| 258 | + const from = 1; |
| 259 | + const to = 0; |
| 260 | + |
| 261 | + const fadeOut = keyframes({ |
| 262 | + from: { |
| 263 | + opacity: getOpacity(from), |
| 264 | + }, |
| 265 | + to: { |
| 266 | + opacity: getOpacity(to), |
| 267 | + }, |
| 268 | + }); |
| 269 | + |
| 270 | + const Component = styled.div({ animationName: fadeOut }); |
| 271 | + const { getByText } = render(<Component>hello world</Component>); |
| 272 | + |
| 273 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'kbrsk95'); |
| 274 | + expect(getKeyframe('kbrsk95')).toMatchInlineSnapshot( |
| 275 | + `"@keyframes kbrsk95{0%{opacity:1}to{opacity:0}}"` |
| 276 | + ); |
| 277 | + }); |
| 278 | + |
| 279 | + it('containing an identifier referencing a constant numeric literal', () => { |
| 280 | + const fromOpacity = 1; |
| 281 | + const toOpacity = 0; |
| 282 | + |
| 283 | + const fadeOut = keyframes({ |
| 284 | + from: { |
| 285 | + opacity: fromOpacity, |
| 286 | + }, |
| 287 | + to: { |
| 288 | + opacity: toOpacity, |
| 289 | + }, |
| 290 | + }); |
| 291 | + |
| 292 | + const Component = styled.div({ animationName: fadeOut }); |
| 293 | + const { getByText } = render(<Component>hello world</Component>); |
| 294 | + |
| 295 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'korwhog'); |
| 296 | + expect(getKeyframe('korwhog')).toMatchInlineSnapshot( |
| 297 | + `"@keyframes korwhog{0%{opacity:1}to{opacity:0}}"` |
| 298 | + ); |
| 299 | + }); |
| 300 | + |
| 301 | + it('containing an identifier referencing a call expression', () => { |
| 302 | + const fromOpacity = getOpacity(1); |
| 303 | + const toOpacity = getOpacity(0); |
| 304 | + |
| 305 | + const fadeOut = keyframes({ |
| 306 | + from: { |
| 307 | + opacity: fromOpacity, |
| 308 | + }, |
| 309 | + to: { |
| 310 | + opacity: toOpacity, |
| 311 | + }, |
| 312 | + }); |
| 313 | + |
| 314 | + const Component = styled.div({ animationName: fadeOut }); |
| 315 | + const { getByText } = render(<Component>hello world</Component>); |
| 316 | + |
| 317 | + expect(getByText('hello world')).toHaveCompiledCss('animation-name', 'korwhog'); |
| 318 | + expect(getKeyframe('korwhog')).toMatchInlineSnapshot( |
| 319 | + `"@keyframes korwhog{0%{opacity:1}to{opacity:0}}"` |
| 320 | + ); |
| 321 | + }); |
| 322 | + }); |
| 323 | + }); |
| 324 | +}); |
0 commit comments