Skip to content

Commit

Permalink
Extract isPrintable to helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs139 committed Dec 5, 2019
1 parent e16b653 commit c2e66af
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions packages/jest-emotion/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ function isShallowEnzymeElement(element: any, classNames: string[]) {
return !hasIntersection(classNames, childClassNames)
}

const creatConvertEmotionElements = (keys: string[], printer: *) => (
node: any
) => {
const creatConvertEmotionElements = (
keys: string[],
printer: *,
isPrintable: (node: *) => boolean
) => (node: any) => {
if (isPrimitive(node)) {
return node
}
Expand Down Expand Up @@ -156,7 +158,7 @@ const creatConvertEmotionElements = (keys: string[], printer: *) => (
[TRANSFORMED]: true
}
}
if (isReactElement(node) || isDOMElement(node)) {
if (isPrintable(node)) {
return {
...node,
[TRANSFORMED]: true
Expand Down Expand Up @@ -198,11 +200,17 @@ export function createSerializer({
DOMElements = true
}: Options = {}) {
const cache = new WeakSet()
const isPrintable = (node: *) =>
isReactElement(node) || (DOMElements && isDOMElement(node))
function print(val: *, printer: Function) {
const seen = cache.has(val)
const isNestedPrint = cache.has(val)
const elements = getStyleElements()
const keys = getKeys(elements)
const convertEmotionElements = creatConvertEmotionElements(keys, printer)
const convertEmotionElements = creatConvertEmotionElements(
keys,
printer,
isPrintable
)
const converted = deepTransform(val, convertEmotionElements)
const nodes = getNodes(converted)
nodes.forEach(cache.add, cache)
Expand All @@ -211,7 +219,7 @@ export function createSerializer({
clean(converted, classNames)
const printedVal = printer(converted)
nodes.forEach(cache.delete, cache)
return seen
return isNestedPrint
? printedVal
: replaceClassNames(
classNames,
Expand All @@ -223,12 +231,9 @@ export function createSerializer({
}

function test(val: *) {
return (
val &&
(!val[TRANSFORMED] &&
(isReactElement(val) || (DOMElements && isDOMElement(val))))
)
return val && !val[TRANSFORMED] && isPrintable(val)
}

return { test, print }
}

Expand Down

0 comments on commit c2e66af

Please sign in to comment.