1
0
mirror of synced 2026-01-06 15:03:36 -05:00
Files
airbyte/airbyte-webapp/scripts/classname-serializer.js
Tim Roes 6c5a150b81 🪟 🔧 Ignore classnames during jest snapshot comparison (#17773)
* Ignore classnames during jest snapshot comparison

* Replace by simple placeholder

* Update snapshots
2022-10-10 12:43:59 +02:00

33 lines
1.0 KiB
JavaScript

import { prettyDOM } from "@testing-library/react";
/**
* Traverse a tree of nodes and replace all class names with
* the count of classnames instead, e.g. "<3 classnames>"
*/
const traverseAndRedactClasses = (node) => {
if (node.className && typeof node.className === "string") {
node.className = `<removed-for-snapshot-test>`;
}
node.childNodes.forEach(traverseAndRedactClasses);
};
module.exports = {
serialize(val, config) {
// Clone the whole rendered DOM tree, since we're modifying it
const clone = val.baseElement.cloneNode(true);
// Redact all classnames
traverseAndRedactClasses(clone);
// Use prettyDOM to format the modified DOM as a string.
return prettyDOM(clone, Infinity, {
indent: config.indent.length,
highlight: false,
});
},
test(val) {
// Only use this serializer when creating a snapshot of RenderResult, which is
// the return value of testing-library/react's render method.
return val && val.baseElement && val.baseElement instanceof Element;
},
};