import type { TransformOptions } from '../test-utils';
import { transform as transformCode } from '../test-utils';
describe('babel plugin', () => {
const transform = (code: string, opts: TransformOptions = {}) =>
transformCode(code, { comments: true, ...opts });
it('should not change code where there is no compiled components', () => {
const actual = transform(`const one = 1;`);
expect(actual.trim()).toEqual('const one = 1;');
});
it('should not comment file if no transformation occurred', () => {
const actual = transform(`
import { ClassNames } from '@compiled/react/runtime';
`);
expect(actual).toMatchInlineSnapshot(`
"import { ClassNames } from "@compiled/react/runtime";
"
`);
});
it('should generate fallback file comment when filename is not defined', () => {
const actual = transform(`
import { styled } from '@compiled/react';
const MyDiv = styled.div\`
font-size: 12px;
\`;
`);
expect(actual).toInclude('File generated by @compiled/babel-plugin v0.0.0');
});
it('should generate fallback file comment when filename is defined', () => {
const code = `
import { styled } from '@compiled/react';
const MyDiv = styled.div\`
font-size: 12px;
\`;
`;
const actual = transform(code, { filename: 'test.tsx' });
expect(actual).toInclude('test.tsx generated by @compiled/babel-plugin v0.0.0');
});
it('should transform basic styled component', () => {
const actual = transform(`
import { styled } from '@compiled/react';
const MyDiv = styled.div\`
font-size: 12px;
\`;
`);
expect(actual).toMatchInlineSnapshot(`
"/* File generated by @compiled/babel-plugin v0.0.0 */
import { forwardRef } from "react";
import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
const _ = "._1wyb1fwx{font-size:12px}";
const MyDiv = forwardRef(
({ as: C = "div", style: __cmpls, ...__cmplp }, __cmplr) => {
if (__cmplp.innerRef) {
throw new Error("Please use 'ref' instead of 'innerRef'.");
}
return (