From cb4565036c5d5e0aa7424af6ef5c0d58901c189b Mon Sep 17 00:00:00 2001 From: Charlie Harding Date: Tue, 16 Apr 2024 15:45:30 +0200 Subject: [PATCH] Remove ViewChild from metadata ViewChild will always be null, and so there is no point in mocking it. viewChild.required throws an error in mocked components without this change. Fixes GH-8634 --- .../src/lib/common/decorate.queries.ts | 5 ++- .../lib/mock-component/mock-component.spec.ts | 32 ------------------- .../lib/mock-directive/mock-directive.spec.ts | 32 ------------------- 3 files changed, 4 insertions(+), 65 deletions(-) diff --git a/libs/ng-mocks/src/lib/common/decorate.queries.ts b/libs/ng-mocks/src/lib/common/decorate.queries.ts index 8609839342..48dbea29e0 100644 --- a/libs/ng-mocks/src/lib/common/decorate.queries.ts +++ b/libs/ng-mocks/src/lib/common/decorate.queries.ts @@ -27,7 +27,10 @@ const generateFinalQueries = (queries: { for (const key of Object.keys(queries)) { const query: Query & { ngMetadataName?: string } = queries[key]; - final.push([key, query]); + + if (!query.isViewQuery || isInternalKey(key)) { + final.push([key, query]); + } if (!query.isViewQuery && !isInternalKey(key)) { scanKeys.push(key); diff --git a/libs/ng-mocks/src/lib/mock-component/mock-component.spec.ts b/libs/ng-mocks/src/lib/mock-component/mock-component.spec.ts index 896dcba331..f92adcebbe 100644 --- a/libs/ng-mocks/src/lib/mock-component/mock-component.spec.ts +++ b/libs/ng-mocks/src/lib/mock-component/mock-component.spec.ts @@ -393,22 +393,6 @@ describe('MockComponent', () => { ngMetadataName: 'ContentChildren', }), ], - o3: [ - jasmine.objectContaining({ - selector: 'i3', - isViewQuery: true, - read: TemplateRef, - ngMetadataName: 'ViewChild', - }), - ], - o4: [ - jasmine.objectContaining({ - selector: 'i4', - isViewQuery: true, - read: TemplateRef, - ngMetadataName: 'ViewChildren', - }), - ], o5: [ jasmine.objectContaining({ selector: 'i5', @@ -425,22 +409,6 @@ describe('MockComponent', () => { ngMetadataName: 'ContentChildren', }), ], - o7: [ - jasmine.objectContaining({ - selector: 'i7', - isViewQuery: true, - read: ElementRef, - ngMetadataName: 'ViewChild', - }), - ], - o8: [ - jasmine.objectContaining({ - selector: 'i8', - isViewQuery: true, - read: ElementRef, - ngMetadataName: 'ViewChildren', - }), - ], __ngMocksVcr_o1: [ jasmine.objectContaining({ diff --git a/libs/ng-mocks/src/lib/mock-directive/mock-directive.spec.ts b/libs/ng-mocks/src/lib/mock-directive/mock-directive.spec.ts index e3b0982220..2051332d7f 100644 --- a/libs/ng-mocks/src/lib/mock-directive/mock-directive.spec.ts +++ b/libs/ng-mocks/src/lib/mock-directive/mock-directive.spec.ts @@ -300,22 +300,6 @@ describe('MockDirective', () => { ngMetadataName: 'ContentChildren', }), ], - o3: [ - jasmine.objectContaining({ - selector: 'i3', - isViewQuery: true, - read: TemplateRef, - ngMetadataName: 'ViewChild', - }), - ], - o4: [ - jasmine.objectContaining({ - selector: 'i4', - isViewQuery: true, - read: TemplateRef, - ngMetadataName: 'ViewChildren', - }), - ], o5: [ jasmine.objectContaining({ selector: 'i5', @@ -332,22 +316,6 @@ describe('MockDirective', () => { ngMetadataName: 'ContentChildren', }), ], - o7: [ - jasmine.objectContaining({ - selector: 'i7', - isViewQuery: true, - read: ElementRef, - ngMetadataName: 'ViewChild', - }), - ], - o8: [ - jasmine.objectContaining({ - selector: 'i8', - isViewQuery: true, - read: ElementRef, - ngMetadataName: 'ViewChildren', - }), - ], __ngMocksVcr_o1: [ jasmine.objectContaining({