From bfffd6616ca638c6e94a3982210310cd4535fde2 Mon Sep 17 00:00:00 2001 From: ahnpnl <27772165+ahnpnl@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:43:05 +0100 Subject: [PATCH] refactor: bring back checks on `undefined`/`null` for `no-ng-attributes` --- jest.config.js | 1 + src/__tests__/ng-snapshot.spec.ts | 2 +- src/__tests__/no-ng-attributes.spec.ts | 32 ++++++++++++++++++++++++++ src/serializers/no-ng-attributes.ts | 4 ++-- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/__tests__/no-ng-attributes.spec.ts diff --git a/jest.config.js b/jest.config.js index c810cf596f..9de8e1b522 100644 --- a/jest.config.js +++ b/jest.config.js @@ -9,6 +9,7 @@ module.exports = { moduleNameMapper: { '@angular/compiler-cli/ngcc': '/node_modules/@angular/compiler-cli/bundles/ngcc/main-ngcc.js', }, + testEnvironment: 'jsdom', modulePathIgnorePatterns: ['examples/.*', 'website/.*'], resolver: '/build/resolvers/ng-jest-resolver', snapshotSerializers: [require.resolve('jest-snapshot-serializer-raw')], diff --git a/src/__tests__/ng-snapshot.spec.ts b/src/__tests__/ng-snapshot.spec.ts index 0f08e99814..bca6394c46 100644 --- a/src/__tests__/ng-snapshot.spec.ts +++ b/src/__tests__/ng-snapshot.spec.ts @@ -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); }); diff --git a/src/__tests__/no-ng-attributes.spec.ts b/src/__tests__/no-ng-attributes.spec.ts new file mode 100644 index 0000000000..47201183d5 --- /dev/null +++ b/src/__tests__/no-ng-attributes.spec.ts @@ -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); + }, + ); +}); diff --git a/src/serializers/no-ng-attributes.ts b/src/serializers/no-ng-attributes.ts index f9e1a437b2..e98e0d93a7 100644 --- a/src/serializers/no-ng-attributes.ts +++ b/src/serializers/no-ng-attributes.ts @@ -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,