-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Cleanup @emotion/jest entrypoints #1920
Merged
+334
−348
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@emotion/jest': major | ||
--- | ||
|
||
The root entry (`@emotion/jest`) no longer has `default` and `serializer` exports. You can still import `createSerializer` from it to create your own serializer if needed. |
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,5 @@ | ||
--- | ||
'@emotion/jest': major | ||
--- | ||
|
||
`@emotion/jest/enzyme` entrypoint has been renamed to `@emotion/jest/enzyme-serializer`. It's main purpose is compatibility with Jest's `snapshotSerializers` option, so it no longer has a default export. You can import `createEnzymeSerializer` from the root entry (`@emotion/jest`) and create your own serializer if needed. |
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,5 @@ | ||
--- | ||
'@emotion/jest': major | ||
--- | ||
|
||
`@emotion/jest/serializer`'s main purpose is compatibility with Jest's `snapshotSerializers` option, so it no longer has a default export. You can import `createSerializer` from the root entry (`@emotion/jest`) and create your own serializer if needed. | ||
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
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 @@ | ||
export * from '../src/create-enzyme-serializer' |
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,8 @@ | ||
{ | ||
"main": "dist/jest.cjs.js", | ||
"module": "dist/jest.esm.js", | ||
"types": "../types/enzyme-serializer", | ||
"preconstruct": { | ||
"source": "../src/enzyme-serializer" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export * from '../src/create-serializer' |
4 changes: 2 additions & 2 deletions
4
packages/jest/enzyme/package.json → packages/jest/serializer/package.json
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"main": "dist/jest.cjs.js", | ||
"module": "dist/jest.esm.js", | ||
"types": "../types/enzyme", | ||
"types": "../types/serializer", | ||
"preconstruct": { | ||
"source": "../src/enzyme" | ||
"source": "../src/serializer" | ||
} | ||
} |
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
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,236 @@ | ||
// @flow | ||
import prettify from '@emotion/css-prettifier' | ||
import { replaceClassNames } from './replace-class-names' | ||
import { | ||
getClassNamesFromNodes, | ||
isReactElement, | ||
isEmotionCssPropElementType, | ||
isEmotionCssPropEnzymeElement, | ||
isDOMElement, | ||
getStylesFromClassNames, | ||
getStyleElements, | ||
getKeys, | ||
flatMap, | ||
isPrimitive, | ||
hasIntersection | ||
} from './utils' | ||
|
||
function getNodes(node, nodes = []) { | ||
if (Array.isArray(node)) { | ||
for (let child of node) { | ||
getNodes(child, nodes) | ||
} | ||
return nodes | ||
} | ||
|
||
if (node.children) { | ||
for (let child of node.children) { | ||
getNodes(child, nodes) | ||
} | ||
} | ||
|
||
if (typeof node === 'object') { | ||
nodes.push(node) | ||
} | ||
|
||
return nodes | ||
} | ||
|
||
function copyProps(src, target) { | ||
return Object.defineProperties(src, { | ||
...Object.getOwnPropertyDescriptors(target) | ||
}) | ||
} | ||
|
||
function deepTransform(node, transform) { | ||
if (Array.isArray(node)) { | ||
return node.map(child => deepTransform(child, transform)) | ||
} | ||
|
||
const transformed: any = transform(node) | ||
|
||
if (transformed !== node) { | ||
if (transformed.props) { | ||
copyProps(transformed, { | ||
props: Object.entries(transformed.props).reduce( | ||
(props, [key, value]) => | ||
Object.assign(props, { | ||
[key]: deepTransform(value, transform) | ||
}), | ||
{} | ||
) | ||
}) | ||
} | ||
if (transformed.children) { | ||
return copyProps(transformed, { | ||
// flatMap to allow a child of <A><B /><C /></A> to be transformed to <B /><C /> | ||
children: flatMap( | ||
(deepTransform(transformed.children, transform): any), | ||
id => id | ||
) | ||
}) | ||
} | ||
} | ||
|
||
return transformed | ||
} | ||
|
||
function getPrettyStylesFromClassNames( | ||
classNames: Array<string>, | ||
elements: Array<HTMLStyleElement>, | ||
indentation: string | ||
) { | ||
return prettify(getStylesFromClassNames(classNames, elements), indentation) | ||
} | ||
|
||
export type Options = { | ||
classNameReplacer?: (className: string, index: number) => string, | ||
DOMElements?: boolean | ||
} | ||
|
||
function filterEmotionProps(props = {}) { | ||
const { | ||
css, | ||
__EMOTION_TYPE_PLEASE_DO_NOT_USE__, | ||
__EMOTION_LABEL_PLEASE_DO_NOT_USE__, | ||
...rest | ||
} = props | ||
|
||
rest.css = 'unknown styles' | ||
|
||
return rest | ||
} | ||
|
||
function isShallowEnzymeElement(element: any, classNames: string[]) { | ||
const delimiter = ' ' | ||
const childClassNames = flatMap(element.children || [], ({ props = {} }) => | ||
(props.className || '').split(delimiter) | ||
).filter(Boolean) | ||
return !hasIntersection(classNames, childClassNames) | ||
} | ||
|
||
const createConvertEmotionElements = (keys: string[], printer: *) => ( | ||
node: any | ||
) => { | ||
if (isPrimitive(node)) { | ||
return node | ||
} | ||
if (isEmotionCssPropEnzymeElement(node)) { | ||
const cssClassNames = (node.props.css.name || '').split(' ') | ||
const expectedClassNames = flatMap(cssClassNames, cssClassName => | ||
keys.map(key => `${key}-${cssClassName}`) | ||
) | ||
// if this is a shallow element, we need to manufacture the className | ||
// since the underlying component is not rendered. | ||
if (isShallowEnzymeElement(node, expectedClassNames)) { | ||
const className = [node.props.className] | ||
.concat(expectedClassNames) | ||
.filter(Boolean) | ||
.join(' ') | ||
const emotionType = node.props.__EMOTION_TYPE_PLEASE_DO_NOT_USE__ | ||
// emotionType will be a string for DOM elements | ||
const type = | ||
typeof emotionType === 'string' ? emotionType : emotionType.name | ||
return { | ||
...node, | ||
props: filterEmotionProps({ | ||
...node.props, | ||
className | ||
}), | ||
type | ||
} | ||
} else { | ||
return node.children | ||
} | ||
} | ||
if (isEmotionCssPropElementType(node)) { | ||
return { | ||
...node, | ||
props: filterEmotionProps(node.props), | ||
type: node.props.__EMOTION_TYPE_PLEASE_DO_NOT_USE__ | ||
} | ||
} | ||
if (isReactElement(node)) { | ||
return copyProps({}, node) | ||
} | ||
return node | ||
} | ||
|
||
function clean(node: any, classNames: string[]) { | ||
if (Array.isArray(node)) { | ||
for (const child of node) { | ||
clean(child, classNames) | ||
} | ||
return | ||
} | ||
if (node.children) { | ||
for (const child of node.children) { | ||
clean(child, classNames) | ||
} | ||
} | ||
if (node.props) { | ||
const { className } = node.props | ||
if (!className) { | ||
// if it's empty, remove it | ||
delete node.props.className | ||
} else { | ||
const hasKnownClass = hasIntersection(className.split(' '), classNames) | ||
if (hasKnownClass) { | ||
delete node.props.css | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function createSerializer({ | ||
classNameReplacer, | ||
DOMElements = true | ||
}: Options = {}) { | ||
const cache = new WeakSet() | ||
const isTransformed = val => cache.has(val) | ||
|
||
function serialize( | ||
val: *, | ||
config: *, | ||
indentation: string, | ||
depth: number, | ||
refs: *, | ||
printer: Function | ||
) { | ||
const elements = getStyleElements() | ||
const keys = getKeys(elements) | ||
const convertEmotionElements = createConvertEmotionElements(keys, printer) | ||
const converted = deepTransform(val, convertEmotionElements) | ||
const nodes = getNodes(converted) | ||
const classNames = getClassNamesFromNodes(nodes) | ||
const styles = getPrettyStylesFromClassNames( | ||
classNames, | ||
elements, | ||
config.indent | ||
) | ||
clean(converted, classNames) | ||
|
||
nodes.forEach(cache.add, cache) | ||
const printedVal = printer(converted, config, indentation, depth, refs) | ||
nodes.forEach(cache.delete, cache) | ||
|
||
return replaceClassNames( | ||
classNames, | ||
styles, | ||
printedVal, | ||
keys, | ||
classNameReplacer | ||
) | ||
} | ||
|
||
return { | ||
test(val: *) { | ||
return ( | ||
val && | ||
(!isTransformed(val) && | ||
(isReactElement(val) || (DOMElements && isDOMElement(val)))) | ||
) | ||
}, | ||
serialize | ||
} | ||
} |
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,3 @@ | ||
// @flow | ||
import { createEnzymeSerializer } from './create-enzyme-serializer' | ||
export const { test, serialize } = createEnzymeSerializer() |
Oops, something went wrong.
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.
I assume
import * as serializer from '@emotion/jest/serializer'
would work?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.
And if so, in this changeset and the one for enzyme stuff, explain that they're individual exports now.
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.
Yes, it is used like that somewhere in our tests.
Done - gonna go ahead and merge this PR, but if you have any other comments then please leave them here and I will make sure to address them.