1
0
mirror of synced 2026-01-20 03:07:18 -05:00
Files
airbyte/airbyte-webapp/scripts/classname-serializer.js
Tim Roes bd9eedf4d7 🪟 🔧 Add ConnectorIds utility (#19826)
* Add ConnectorIds utility

* Fix typo

* Update airbyte-webapp/packages/eslint-plugin/no-hardcoded-connector-ids.js

Co-authored-by: Lake Mossman <lake@airbyte.io>

* Change variable name

* Use in tests

* Remove unnecessary comment

* Adjust linting errors

Co-authored-by: Lake Mossman <lake@airbyte.io>
2022-12-02 23:54:03 +01:00

37 lines
1.2 KiB
JavaScript

import { prettyDOM } from "@testing-library/react";
/**
* Traverse a tree of nodes and replace all class names with "<removed-for-snapshot-test>"
*/
const traverseAndRedactClasses = (node) => {
if (
node.className &&
(typeof node.className === "string" || (node.className instanceof SVGAnimatedString && node.className.baseVal))
) {
// We need to use setAttribute here, since on SVGElement we can't
// set `className` to a string for the `SVGAnimatedString` case.
node.setAttribute("class", `<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;
},
};