|
| 1 | +import { writeFileSync } from 'fs'; |
| 2 | + |
| 3 | +import { transform } from './transform'; |
| 4 | + |
| 5 | +// Mock out FS to avoid writing to disk |
| 6 | +// We aren't processing the result anyway, so no need for specifying the response |
| 7 | +jest.mock('fs'); |
| 8 | + |
| 9 | +describe('babel-plugin-strip-runtime with vanillaCss', () => { |
| 10 | + describe('with the classic runtime', () => { |
| 11 | + const runtime = 'classic'; |
| 12 | + |
| 13 | + const styles = `{ |
| 14 | + danger: { |
| 15 | + color: 'red', |
| 16 | + backgroundColor: 'red' |
| 17 | + }, |
| 18 | + success: { |
| 19 | + color: 'green', |
| 20 | + backgroundColor: 'green' |
| 21 | + } |
| 22 | + }`; |
| 23 | + |
| 24 | + it('should transform vanillaCss to ax', () => { |
| 25 | + const actual = transform( |
| 26 | + ` |
| 27 | + import { cssMap, vanillaCss } from '@compiled/react'; |
| 28 | +
|
| 29 | + const someStyles = cssMap(${styles}); |
| 30 | +
|
| 31 | + function someFunctionCall(_obj) {} |
| 32 | +
|
| 33 | + export const bap = someFunctionCall({ |
| 34 | + // node DOM constructor |
| 35 | + toDOM(node) { |
| 36 | + const { localId, state } = node.attrs; |
| 37 | + // injectCompiledCss should be added right before \`attrs\` at build time. |
| 38 | + const attrs = { |
| 39 | + 'data-task-local-id': localId || 'local-task', |
| 40 | + 'data-task-state': state || 'TODO', |
| 41 | + // vanillaCss function will hint Babel to inject styles on run time, and extract styles on build time |
| 42 | + class: vanillaCss([someStyles.base, state === "DONE" && someStyles.done]), |
| 43 | + }; |
| 44 | + // construct a div node |
| 45 | + return ['div', attrs, 0]; |
| 46 | + }, |
| 47 | + }); |
| 48 | + `, |
| 49 | + { |
| 50 | + run: 'both', |
| 51 | + runtime, |
| 52 | + extractStylesToDirectory: { source: 'src/', dest: 'dist/' }, |
| 53 | + } |
| 54 | + ); |
| 55 | + |
| 56 | + expect(actual).toMatchInlineSnapshot(` |
| 57 | + "/* app.tsx generated by @compiled/babel-plugin v0.0.0 */ |
| 58 | + import './app.compiled.css'; |
| 59 | + import * as React from 'react'; |
| 60 | + import { ax, ix } from '@compiled/react/runtime'; |
| 61 | + const someStyles = { |
| 62 | + danger: '_syaz5scu _bfhk5scu', |
| 63 | + success: '_syazbf54 _bfhkbf54', |
| 64 | + }; |
| 65 | + function someFunctionCall(_obj) {} |
| 66 | + export const bap = someFunctionCall({ |
| 67 | + // node DOM constructor |
| 68 | + toDOM(node) { |
| 69 | + const { localId, state } = node.attrs; |
| 70 | + // injectCompiledCss should be added right before \`attrs\` at build time. |
| 71 | + const attrs = { |
| 72 | + 'data-task-local-id': localId || 'local-task', |
| 73 | + 'data-task-state': state || 'TODO', |
| 74 | + // vanillaCss function will hint Babel to inject styles on run time, and extract styles on build time |
| 75 | + class: ax([someStyles.base, state === 'DONE' && someStyles.done]), |
| 76 | + }; |
| 77 | + // construct a div node |
| 78 | + return ['div', attrs, 0]; |
| 79 | + }, |
| 80 | + }); |
| 81 | + " |
| 82 | + `); |
| 83 | + |
| 84 | + expect(writeFileSync).toBeCalledWith( |
| 85 | + expect.stringContaining('app.compiled.css'), |
| 86 | + '._bfhk5scu{background-color:red}\n' + |
| 87 | + '._bfhkbf54{background-color:green}\n' + |
| 88 | + '._syaz5scu{color:red}\n' + |
| 89 | + '._syazbf54{color:green}' |
| 90 | + ); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments