-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Rewrite of all the functions, using Enzyme internal functions fo…
…r better support/compatibi
- Loading branch information
1 parent
07c82c8
commit 0e06c67
Showing
5 changed files
with
141 additions
and
87 deletions.
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 |
---|---|---|
@@ -1,63 +1,3 @@ | ||
import omit from 'lodash.omit'; | ||
import pickBy from 'lodash.pickby'; | ||
import compact from 'lodash.compact'; | ||
import get from 'lodash.get'; | ||
import getDisplayName from 'react-display-name'; | ||
|
||
export function shallowToJson(wrapper) { | ||
const type = wrapper.type(); | ||
if (!type) { | ||
return wrapper.node; | ||
} | ||
|
||
const children = compact(wrapper.children().map(c => shallowToJson(c))); | ||
const props = pickBy(wrapper.props(), val => val !== undefined); | ||
|
||
const json = { | ||
type: wrapper.name(), | ||
props: omit(props, 'children'), | ||
children: (children.length) ? children : null, | ||
$$typeof: Symbol.for('react.test.json'), | ||
}; | ||
|
||
// If the type of element is not a function, it means that is a DOM element | ||
if (typeof type !== 'function') { | ||
return json; | ||
} | ||
|
||
// We need to get the rendered component in Enzyme internals | ||
// because it won't be returned by `.children()` | ||
const element = get(wrapper, 'node._reactInternalInstance._renderedComponent._currentElement'); | ||
if(!element) { | ||
return json; | ||
} | ||
|
||
const jsonChildren = json.children; | ||
json.children = [{ | ||
type: getDisplayName(element.type), | ||
props: omit(element.props, 'children'), | ||
children: jsonChildren, | ||
$$typeof: Symbol.for('react.test.json'), | ||
}]; | ||
|
||
return json; | ||
} | ||
|
||
export {shallowToJson as mountToJson}; | ||
|
||
const renderChildToJson = child => { | ||
if(!child) return; | ||
|
||
if(child.type === 'tag') { | ||
return { | ||
type: child.name, | ||
props: child.attribs, | ||
children: child.children && child.children.length ? compact(child.children.map(renderChildToJson)) : null, | ||
$$typeof: Symbol.for('react.test.json'), | ||
}; | ||
} else if(child.type === 'text') { | ||
return child.data; | ||
} | ||
}; | ||
export const renderToJson = wrapper => | ||
renderChildToJson(wrapper.children()[0]); | ||
export {mountToJson} from './mount'; | ||
export {shallowToJson} from './shallow'; | ||
export {renderToJson} from './render'; |
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,51 @@ | ||
import compact from 'lodash.compact'; | ||
import omit from 'lodash.omit'; | ||
import {isDOMComponent, isElement} from 'enzyme/build/react-compat'; | ||
import {internalInstance, propsOfNode} from 'enzyme/build/Utils'; | ||
import {typeName} from 'enzyme/build/Debug'; | ||
import {childrenOfNode} from 'enzyme/build/ShallowTraversal'; | ||
|
||
function instToJson(inst) { | ||
if (typeof inst === 'string' || typeof inst === 'number') return inst; | ||
if (!inst) return ''; | ||
|
||
if (!inst.getPublicInstance) { | ||
const internal = internalInstance(inst); | ||
return instToJson(internal); | ||
} | ||
const publicInst = inst.getPublicInstance(); | ||
|
||
if (typeof publicInst === 'string' || typeof publicInst === 'number') return publicInst; | ||
if (!publicInst && !inst._renderedComponent) return ''; | ||
|
||
const currentElement = inst._currentElement; | ||
const type = typeName(currentElement); | ||
const props = omit(propsOfNode(currentElement), 'children'); | ||
const children = []; | ||
if (isDOMComponent(publicInst)) { | ||
const renderedChildren = inst._renderedChildren; | ||
if (!renderedChildren) { | ||
children.push(...childrenOfNode(currentElement)); | ||
} else { | ||
children.push(...Object.values(renderedChildren)); | ||
} | ||
} else if ( | ||
isElement(currentElement) && | ||
typeof currentElement.type === 'function' | ||
) { | ||
children.push(inst._renderedComponent); | ||
} | ||
|
||
const childrenArray = compact(children.map(n => instToJson(n))); | ||
|
||
return { | ||
type, | ||
props, | ||
children: childrenArray.length ? childrenArray : null, | ||
$$typeof: Symbol.for('react.test.json'), | ||
}; | ||
} | ||
|
||
export function mountToJson(wrapper) { | ||
return instToJson(wrapper.node); | ||
} |
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,19 @@ | ||
import compact from 'lodash.compact'; | ||
|
||
const renderChildToJson = child => { | ||
if(!child) return; | ||
|
||
if(child.type === 'tag') { | ||
return { | ||
type: child.name, | ||
props: child.attribs, | ||
children: child.children && child.children.length ? compact(child.children.map(renderChildToJson)) : null, | ||
$$typeof: Symbol.for('react.test.json'), | ||
}; | ||
} else if(child.type === 'text') { | ||
return child.data; | ||
} | ||
}; | ||
|
||
export const renderToJson = wrapper => | ||
renderChildToJson(wrapper.children()[0]); |
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,26 @@ | ||
import compact from 'lodash.compact'; | ||
import omit from 'lodash.omit'; | ||
import {propsOfNode} from 'enzyme/build/Utils'; | ||
import {typeName} from 'enzyme/build/Debug'; | ||
import {childrenOfNode} from 'enzyme/build/ShallowTraversal'; | ||
|
||
function nodeToJson(node) { | ||
if (typeof node === 'string' || typeof node === 'number') { | ||
return node; | ||
} | ||
|
||
const children = compact(childrenOfNode(node).map(n => nodeToJson(n))); | ||
const type = typeName(node); | ||
const props = omit(propsOfNode(node), 'children'); | ||
|
||
return { | ||
type, | ||
props, | ||
children: children.length ? children : null, | ||
$$typeof: Symbol.for('react.test.json'), | ||
}; | ||
} | ||
|
||
export function shallowToJson(wrapper) { | ||
return nodeToJson(wrapper.node); | ||
} |
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