Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 24x 12x 12x 12x 12x 12x 5x 5x 5x 12x 15x 7x 8x 12x 12x 12x 12x 15x 15x 15x 15x 12x 12x 12x 12x 12x 12x | export interface ReactTestElement {
type: string;
props: ReactProps;
children: ReactTestChild[];
$$typeof?: Symbol;
}
const JSON_TYPE = Symbol && Symbol.for('react.test.json');
export interface ReactProps {
[name: string]: any;
}
export type ReactTestChild = ReactTestElement | string | number | null;
function getTagName(element: HTMLElement | SVGElement) {
Eif ('tagName' in element) {
return element.tagName.toLowerCase();
} else {
return (element as any).nodeName as string;
}
}
function getAttributes(element: HTMLElement | SVGElement) {
const attrs: ReactProps = {};
const xs = element.attributes;
for (let i = 0; i < xs.length; i += 1) {
const x = xs.item(i);
Eif (x !== null) {
attrs[x.nodeName] = [x.nodeValue];
}
}
return attrs;
}
function toJSONChild(node: Node): ReactTestChild {
switch (node.nodeType) {
case node.ELEMENT_NODE:
return toJSON(node as Element);
case node.TEXT_NODE:
return node.nodeValue;
default:
// ignore
return null;
}
}
function getChildren(element: HTMLElement | SVGElement) {
const result = [];
Eif (element.hasChildNodes()) {
const childNodes = element.childNodes;
for (let i = 0; i < childNodes.length; i += 1) {
const child = childNodes.item(i);
const jsonChild = toJSONChild(child);
Eif (jsonChild !== null) {
result.push(jsonChild);
}
}
}
return result;
}
/**
* Transform to object-tree for snapshot testing
* which is compatible to "react.test.json" in Jest.
*/
export default function toJSON(element: Element): ReactTestElement {
Iif (!(element instanceof HTMLElement) && !(element instanceof SVGElement)) {
throw new Error('element must be HTMLElement or SVGElement');
}
const type = getTagName(element);
const props = getAttributes(element);
const children = getChildren(element);
return {
type,
props,
children,
$$typeof: JSON_TYPE,
};
}
|