Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write a few unit-tests for babel plugin #1648

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions packages/babel-plugin/src/__tests__/css-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,110 @@ describe('css builder', () => {
"
`);
});

it('works with css map', () => {
const actual = transform(`
import { cssMap } from '@compiled/react';

const styles = cssMap({ red: { color: 'red' }, blue: { color: 'blue' } });

function Component({ color }) {
return <div css={styles[color]} />
}
`);

expect(actual).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 = {
red: "_syaz5scu",
blue: "_syaz13q2",
};
function Component({ color }) {
return (
<CC>
<CS>{[_, _2]}</CS>
{<div className={ax([styles[color]])} />}
</CC>
);
}
"
`);
});

it('works in spite of a style override', () => {
const actual = transform(`
import { css } from '@compiled/react';

const styles = css({ color: ({ color }) => color });

function Component({ color }) {
return <div style={{ background: 'red' }} css={styles} />
}
`);

expect(actual).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
const _ = "._syaz1cj8{color:var(--_xexnhp)}";
const styles = null;
function Component({ color }) {
return (
<CC>
<CS>{[_]}</CS>
{
<div
className={ax(["_syaz1cj8"])}
style={{
background: "red",
"--_xexnhp": ix((__cmplp) => __cmplp.color),
}}
/>
}
</CC>
);
}
"
`);
});

it('works when there is a clear member expression', () => {
const actual = transform(`
import { css } from '@compiled/react';

const styles = {
test: {
red: css({ color: 'red' }),
blue: css({ color: 'blue' }),
},
};

function Component({ color }) {
return <div css={styles.test.red} />
}
`);

expect(actual).toMatchInlineSnapshot(`
"import * as React from "react";
import { ax, ix, CC, CS } from "@compiled/react/runtime";
const _ = "._syaz5scu{color:red}";
const styles = {
test: {
red: null,
blue: null,
},
};
function Component({ color }) {
return (
<CC>
<CS>{[_]}</CS>
{<div className={ax(["_syaz5scu"])} />}
</CC>
);
}
"
`);
});
});
77 changes: 77 additions & 0 deletions packages/babel-plugin/src/utils/__tests__/css-builders.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import generate from '@babel/generator';

Check failure on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts

View workflow job for this annotation

GitHub Actions / build

'generate' is declared but its value is never read.

Check failure on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

'generate' is declared but its value is never read.

Check warning on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

'generate' is defined but never used

Check failure on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts

View workflow job for this annotation

GitHub Actions / build

'generate' is declared but its value is never read.

Check warning on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'generate' is defined but never used

Check failure on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'generate' is declared but its value is never read.

Check warning on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'generate' is defined but never used

Check failure on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'generate' is declared but its value is never read.
import { parse } from '@babel/parser';
import type { NodePath } from '@babel/traverse';
import traverse from '@babel/traverse';
import type { Identifier, MemberExpression } from '@babel/types';

import type { Metadata } from '../../types';
import { buildCss } from '../css-builders';

describe('buildCss', () => {
it('returns a css string and variables array for an identifier node', () => {
const file = parse(`
const color = { background: 'red' };
const styles = color;

run(styles);
`);

let path: NodePath<Identifier> | null = null;
traverse(file, {
CallExpression(nodePath) {
nodePath.traverse({
Identifier(innerPath) {
if (innerPath.node.name === 'styles') {
path = innerPath;
}
},
});
},
});

expect(path).not.toEqual(null);
const meta: Metadata = {
parentPath: path!.parentPath,
state: {
filename: '',
},
} as any;

const { css, variables } = buildCss(path!.node, meta);
expect(css).toEqual([{ css: 'background: red;', type: 'unconditional' }]);
expect(variables).toEqual([]);
});

it('returns a css string and variables array for a member expression node', () => {
const file = parse(`
const styles = { option1: { background: 'red' } };

run(styles.option1);
`);

let path: NodePath<MemberExpression> | null = null;
traverse(file, {
CallExpression(nodePath) {
nodePath.traverse({
MemberExpression(innerPath) {
path = innerPath;
},
});
},
});

expect(path).not.toEqual(null);

const meta: Metadata = {
parentPath: path!.parentPath,
state: {
cssMap: {},
filename: '',
},
} as any;

const { css, variables } = buildCss(path!.node, meta);
expect(css).toEqual([{ css: 'background: red;', type: 'unconditional' }]);
expect(variables).toEqual([]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { parse } from '@babel/parser';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file looks good!

import type { NodePath } from '@babel/traverse';
import traverse from '@babel/traverse';
import type { Identifier, MemberExpression } from '@babel/types';
import { identifier, memberExpression, stringLiteral } from '@babel/types';

import type { Metadata } from '../../types';
import { evaluateExpression } from '../evaluate-expression';

describe('evaluateExpression', () => {
it('should evaluate a variable reference to its value', () => {
const file = parse(`
const color = 'red';
const styles = color;

run(styles);
`);

let path: NodePath<Identifier> | null = null;
traverse(file, {
CallExpression(nodePath) {
nodePath.traverse({
Identifier(innerPath) {
if (innerPath.node.name === 'styles') {
path = innerPath;
}
},
});
},
});

expect(path).not.toEqual(null);
const meta: Metadata = {
parentPath: path!.parentPath,
} as any;

const { value } = evaluateExpression(path!.node, meta);
expect(value).toEqual(stringLiteral('red'));
});

it('should evaluate a member expression', () => {
const file = parse(`
const styles = foo.bar;
`);

let path: NodePath<MemberExpression> | null = null;
traverse(file, {
MemberExpression(nodePath) {
path = nodePath;
},
});

expect(path).not.toEqual(null);
const meta: Metadata = {
parentPath: path!.parentPath,
} as any;

const { value } = evaluateExpression(path!.node, meta);
const expected = memberExpression(identifier('foo'), identifier('bar'));
delete expected.optional;
expect(value).toMatchObject(expected);
});
});
Loading