-
Notifications
You must be signed in to change notification settings - Fork 67
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
base: master
Are you sure you want to change the base?
Changes from all commits
e516bf4
e5e4255
dc82e49
efcd750
c44c274
885558a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 GitHub Actions / build
Check failure on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts GitHub Actions / build (14.x)
Check warning on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts GitHub Actions / build (14.x)
Check failure on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts GitHub Actions / build
Check warning on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts GitHub Actions / build (16.x)
Check failure on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts GitHub Actions / build (16.x)
Check warning on line 1 in packages/babel-plugin/src/utils/__tests__/css-builders.test.ts GitHub Actions / build (18.x)
|
||
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'; | ||
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); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file looks good!