-
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
Open
yamadapc
wants to merge
6
commits into
atlassian-labs:master
Choose a base branch
from
yamadapc:issue/add-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−0
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e516bf4
Write a few unit-tests for babel plugin
pyamada-atlassian e5e4255
Fix newly added tests
pyamada-atlassian dc82e49
Remove error snapshot from tests
yamadapc efcd750
Remove failure test
pyamada-atlassian c44c274
Merge remote-tracking branch 'yamadapc/issue/add-tests' into issue/ad…
pyamada-atlassian 885558a
Remove test enforcing bug
pyamada-atlassian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
packages/babel-plugin/src/utils/__tests__/css-builders.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import generate from '@babel/generator'; | ||
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([]); | ||
}); | ||
|
||
it('returns a css string for a variable member expression', () => { | ||
const file = parse( | ||
` | ||
import { css } from '@compiled/react'; | ||
|
||
const styles = { option1: css({ background: 'red' }) }; | ||
|
||
run(styles[key]); | ||
`, | ||
{ sourceType: 'module' } | ||
); | ||
|
||
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); | ||
// TODO: This should not happen | ||
expect(css).toEqual([{ css: 'option1: var(--_g48cyt);', type: 'unconditional' }]); | ||
expect(variables.length).toEqual(1); | ||
expect(variables[0].name).toEqual('--_g48cyt'); | ||
expect(generate(variables[0].expression).code).toMatchInlineSnapshot(` | ||
"css({ | ||
background: 'red' | ||
})" | ||
`); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
packages/babel-plugin/src/utils/__tests__/evaluate-expression.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { parse } from '@babel/parser'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
isn't this a bug? i'm not sure why we're adding a test case to enforce buggy behaviour here - perhaps creating a github issue would be better than having this test case here, as the former is more easily discoverable (we don't really check back on test cases unless they get broken)
the other test cases in this file look great, thank you for adding them 🙌
(sorry, had a meeting earlier so i couldn't review the whole PR at once)
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.
Sure, let me remove some of these; generally I'm only trying to reproduce bugs with these.
It's a bit of a structural issue regarding what
css
+cssMap
APIs do.