Skip to content

Commit

Permalink
refactor(serializers): improve codes for snapshot serializer
Browse files Browse the repository at this point in the history
- Extract comments into smaller functions
- Use `NewPlugin` type from `pretty-format`
  • Loading branch information
ahnpnl committed Jul 21, 2024
1 parent 03a5f72 commit 047f09a
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/serializers/no-ng-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { plugins } from 'pretty-format';
import { plugins, type NewPlugin } from 'pretty-format';

const jestDOMElementSerializer = plugins.DOMElement;

Expand All @@ -13,24 +13,28 @@ const attributesToClean: Record<string, RegExp[]> = {
};
const hasAttributesToRemove = (attribute: Attr): boolean =>
attributesToRemovePatterns.some((removePattern) => attribute.name.startsWith(removePattern));

const hasAttributesToClean = (attribute: Attr): boolean =>
Object.keys(attributesToClean).some((removePatternKey) => attribute.name === removePatternKey);

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
const serialize = (node: Element, ...rest: any): string => {
const removeAngularAttributes = (node: Element): Element => {
const nodeCopy = node.cloneNode(true) as Element;
// Remove angular-specific attributes
Object.values(nodeCopy.attributes)
.filter(hasAttributesToRemove)
.forEach((attribute) => nodeCopy.attributes.removeNamedItem(attribute.name));
// Remove angular auto-added classes

return nodeCopy;
};

const cleanAngularClasses = (node: Element): Element => {
const nodeCopy = node.cloneNode(true) as Element;
Object.values(nodeCopy.attributes)
.filter(hasAttributesToClean)
.forEach((attribute: Attr) => {
.forEach((attribute) => {
attribute.value = attribute.value
.split(' ')
.filter(
(attrValue: string) =>
(attrValue) =>
!attributesToClean[attribute.name].some((attributeCleanRegex) =>
attributeCleanRegex.test(attrValue),
),
Expand All @@ -43,19 +47,31 @@ const serialize = (node: Element, ...rest: any): string => {
}
});

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return jestDOMElementSerializer.serialize(nodeCopy, ...rest);
return nodeCopy;
};

const serializeTestFn = (val: Element): boolean =>
!!val.attributes &&
Object.values(val.attributes).some(
(attribute: Attr) => hasAttributesToRemove(attribute) || hasAttributesToClean(attribute),
const shouldSerializeElement = (val: Element): boolean => {
return Object.values(val.attributes).some(
(attribute) => hasAttributesToRemove(attribute) || hasAttributesToClean(attribute),
);
const test = (val: unknown): boolean => !!val && jestDOMElementSerializer.test(val) && serializeTestFn(val as Element);
};

const serialize: NewPlugin['serialize'] = (node, ...rest) => {
let nodeCopy = removeAngularAttributes(node);
nodeCopy = cleanAngularClasses(nodeCopy);

return jestDOMElementSerializer.serialize(nodeCopy, ...rest);
};

const test: NewPlugin['test'] = (val) => {
if (!val || !(val instanceof Element)) {
return false;
}

return jestDOMElementSerializer.test(val) && shouldSerializeElement(val);
};

export = {
serialize,
test,
};
} satisfies NewPlugin;

0 comments on commit 047f09a

Please sign in to comment.