import { transform } from '../../test-utils';
describe('xcss prop transformation', () => {
it('should transform static inline object', () => {
const result = transform(`
`);
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
const _ = "._syaz5scu{color:red}";
{[_]}
{}
;
"
`);
});
it('should throw when not static', () => {
expect(() => {
transform(
`
import { bar } from './foo';
`,
{ highlightCode: false }
);
}).toThrowErrorMatchingInlineSnapshot(`
"unknown file: Object given to the xcss prop must be static (4:23).
2 | import { bar } from './foo';
3 |
> 4 |
| ^^^^^^^^^^^^^^
5 | "
`);
});
it('should transform named xcss prop usage', () => {
const result = transform(`
`);
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
const _ = "._syaz5scu{color:red}";
{[_]}
{}
;
"
`);
});
it('should work with css map', () => {
const result = transform(`
import { cssMap } from '@compiled/react';
const styles = cssMap({
primary: { color: 'red' },
});
`);
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
const _ = "._syaz5scu{color:red}";
const styles = {
primary: "_syaz5scu",
};
{[_]}
{}
;
"
`);
});
it('should allow ternaries', () => {
const result = transform(`
import { cssMap } from '@compiled/react';
const styles = cssMap({
primary: { color: 'red' },
secondary: { color: 'blue' }
});
`);
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
const _2 = "._syaz13q2{color:blue}";
const _ = "._syaz5scu{color:red}";
const styles = {
primary: "_syaz5scu",
secondary: "_syaz13q2",
};
{[_, _2]}
{}
;
"
`);
});
it('should allow concatenating styles', () => {
const result = transform(`
import { cssMap, j } from '@compiled/react';
const styles = cssMap({
primary: { color: 'red' },
secondary: { color: 'blue' }
});
`);
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
import { j } from "@compiled/react";
const _2 = "._syaz13q2{color:blue}";
const _ = "._syaz5scu{color:red}";
const styles = {
primary: "_syaz5scu",
secondary: "_syaz13q2",
};
{[_, _2]}
{
}
;
"
`);
});
it('should transform xcss prop when compiled is in scope', () => {
const result = transform(
`
import { cssMap } from '@compiled/react';
const styles = cssMap({
primary: { color: 'red' },
});
`
);
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
const _ = "._syaz5scu{color:red}";
const styles = {
primary: "_syaz5scu",
};
{[_]}
{}
;
"
`);
});
it('should not blow up transforming an empty xcss object', () => {
const result = transform(
`
`
);
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
{[]}
{}
;
"
`);
});
it('should ignore primitive components using runtime xcss prop', () => {
const result = transform(
`
import { Box, xcss } from '@atlaskit/primitives';
`
);
expect(result).toMatchInlineSnapshot(`
"import { Box, xcss } from "@atlaskit/primitives";
;
"
`);
});
it('should only add styles to xcss call sites that use them', () => {
const result = transform(
`
import { cssMap } from '@compiled/react';
import Button from '@atlaskit/button';
const stylesOne = cssMap({ text: { color: 'red' } })
const stylesTwo = cssMap({ text: { color: 'blue' } })
export function Mixed() {
return (
<>
>
);
}
`
);
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
import Button from "@atlaskit/button";
const _2 = "._syaz13q2{color:blue}";
const _ = "._syaz5scu{color:red}";
const stylesOne = {
text: "_syaz5scu",
};
const stylesTwo = {
text: "_syaz13q2",
};
export function Mixed() {
return (
<>
{[_]}
{}
{[_2]}
{}
>
);
}
"
`);
});
it('should ignore primitive components mixed with compiled components', () => {
const result = transform(
`
import { Box, xcss } from '@atlaskit/primitives';
import Button from '@atlaskit/button';
import { cssMap } from '@compiled/react';
const styles = cssMap({ text: { color: 'red' } })
export function Mixed() {
return (
<>
>
);
}
`
);
// Here, xcss runs at runtime
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
import { Box, xcss } from "@atlaskit/primitives";
import Button from "@atlaskit/button";
const _ = "._syaz5scu{color:red}";
const styles = {
text: "_syaz5scu",
};
export function Mixed() {
return (
<>
{[_]}
{}
>
);
}
"
`);
});
it('should not transform xcss if processXcss = false', () => {
const result = transform(
`
`,
{ processXcss: false }
);
expect(result).toMatchInlineSnapshot(`
";
"
`);
});
});
// Note: we don't support explicitly *importing* Compiled
// in the same file as Emotion - this is something we lint against
// through the jsx-pragma rule.
//
// We only choose to worry about cases where we don't have
// two different CSS-in-JS libraries being explicitly imported,
// i.e. xcss prop, where @compiled/react isn't imported but
// @compiled/babel-plugin will still process the xcss usages.
describe('xcss prop interacting with other libraries', () => {
it('should skip importing Compiled runtime when no direct Compiled usage was found', () => {
const result = transform(
`
/** @jsx jsx */
import { css, jsx } from '@emotion/react';
import { Box, xcss } from '@atlaskit/primitives';
import Button from '@atlaskit/button';
export function Mixed() {
return (
<>
>
);
}
`
);
expect(result).toMatchInlineSnapshot(`
"import { css, jsx } from "@emotion/react";
import { Box, xcss } from "@atlaskit/primitives";
import Button from "@atlaskit/button";
export function Mixed() {
return (
<>
>
);
}
"
`);
});
it('should import Compiled runtime when inline object is used in xcss', () => {
const result = transform(
`
/** @jsx jsx */
import { css, jsx } from '@emotion/react';
import { Box } from '@atlaskit/primitives';
export function Mixed() {
return (
<>
>
);
}
`
);
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
import { css, jsx } from "@emotion/react";
import { Box } from "@atlaskit/primitives";
const _ = "._syaz5scu{color:red}";
export function Mixed() {
return (
<>
{[_]}
{}
>
);
}
"
`);
});
it("xcss prop shouldn't affect styled prop from styled-components", () => {
const result = transform(
`
import { cssMap } from '@compiled/react';
import styled from 'styled-components';
import Button from '@atlaskit/button';
const stylesOne = cssMap({ text: { color: 'red' } });
const stylesTwo = cssMap({ text: { color: 'blue' } });
const Component = styled.div\`
color: green;
\`;
export function Mixed() {
return (
<>
hello world
>
);
}
`
);
expect(result).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
import styled from "styled-components";
import Button from "@atlaskit/button";
const _3 = "._syaz13q2{color:blue}";
const _2 = "._syaz5scu{color:red}";
const _ = "._syaz32ev{color:pink}";
const stylesOne = {
text: "_syaz5scu",
};
const stylesTwo = {
text: "_syaz13q2",
};
const Component = styled.div\`
color: green;
\`;
export function Mixed() {
return (
<>
{[_]}
{}
{[_2]}
{}
{[_3]}
{}
hello world
>
);
}
"
`);
});
});