Skip to content

Commit

Permalink
fix: bring back checks on undefined/null for no-ng-attributes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Nov 9, 2021
1 parent 0ab958d commit 1e7dbf8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
moduleNameMapper: {
'@angular/compiler-cli/ngcc': '<rootDir>/node_modules/@angular/compiler-cli/bundles/ngcc/main-ngcc.js',
},
testEnvironment: 'jsdom',
modulePathIgnorePatterns: ['examples/.*', 'website/.*'],
resolver: '<rootDir>/build/resolvers/ng-jest-resolver',
snapshotSerializers: [require.resolve('jest-snapshot-serializer-raw')],
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/ng-snapshot.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ngSnapshot from '../serializers/ng-snapshot';

describe('ng-snapshot', () => {
describe('ng-snapshot snapshot serializer', () => {
test('should return true when matching the condition', () => {
expect(ngSnapshot.test({ componentRef: 'foo' })).toBe(true);
});
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/no-ng-attributes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import noNgAttr from '../serializers/no-ng-attributes';

describe('no-ng-attributes snapshot serializer', () => {
test('should return true when matching the condition with attributes to remove', () => {
const validElement = document.createElement('DIV');
['ng-reflect', '_nghost', '_ngcontent', 'ng-version'].forEach((attrToRemove) => {
validElement.setAttribute(attrToRemove, 'foo');
});

expect(noNgAttr.test(validElement)).toBe(true);

validElement.remove();
});

test('should return true when matching the condition with attributes to clean', () => {
const validElement = document.createElement('SECTION');
['class', 'id', 'for', 'aria-owns', 'aria-labelledby', 'aria-controls'].forEach((attrToClean) => {
validElement.setAttribute(attrToClean, 'foo');
});

expect(noNgAttr.test(validElement)).toBe(true);

validElement.remove();
});

test.each([undefined, null, 1, document.createElement('INPUT')])(
'should return false when not matching the condition',
(val) => {
expect(noNgAttr.test(val)).toBe(false);
},
);
});
4 changes: 2 additions & 2 deletions src/serializers/no-ng-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ const serialize = (node: Element, ...rest: any): string => {
};

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

export = {
serialize,
Expand Down

0 comments on commit 1e7dbf8

Please sign in to comment.