Skip to content

Commit

Permalink
fix(root): ignore signal-based queries
Browse files Browse the repository at this point in the history
closes #8634
  • Loading branch information
dmitry-stepanenko committed May 28, 2024
1 parent 5c98e73 commit f0f46f3
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
9 changes: 6 additions & 3 deletions libs/ng-mocks/src/lib/common/decorate.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ const generateFinalQueries = (queries: {
const scanKeys: string[] = [];

for (const key of Object.keys(queries)) {
const query: Query & { ngMetadataName?: string } = queries[key];
final.push([key, query]);
const query: Query & { ngMetadataName?: string; isSignal?: boolean } = queries[key];

if (!query.isViewQuery && !isInternalKey(key)) {
if (!query.isSignal) {
final.push([key, query]);
}

if (!query.isViewQuery && !query.isSignal && !isInternalKey(key)) {
scanKeys.push(key);
final.push([`__ngMocksVcr_${key}`, cloneVcrQuery(query)]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ export default (queries?: Record<keyof any, any>): string => {
}

for (const key of Object.keys(queries)) {
const query: Query = queries[key];
const query: Query & { isSignal?: boolean } = queries[key];
if (!isTemplateRefQuery(query)) {
continue;
}
if (query.isSignal) {
continue;

Check warning on line 35 in libs/ng-mocks/src/lib/mock-component/render/generate-template.ts

View check run for this annotation

Codecov / codecov/patch

libs/ng-mocks/src/lib/mock-component/render/generate-template.ts#L35

Added line #L35 was not covered by tests
}
if (typeof query.selector === 'string') {
const selector = query.selector.replace(new RegExp('\\W', 'mg'), '_');
queries[`__mockView_key_${selector}`] = new ViewChild(`key_${selector}`, viewChildArgs);
Expand Down
100 changes: 100 additions & 0 deletions tests-e2e/src/issue-8634/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {
Component,
ElementRef,
Input,
contentChild,
contentChildren,
input,
viewChild,
viewChildren,
} from '@angular/core';
import { MockBuilder, MockRender } from 'ng-mocks';

@Component({
selector: 'app-nested',
standalone: true,
template: `
<div #meow>Nested content</div>
<h3>Queries:</h3>
<ul>
<li>meowViewChild: {{ !!meowViewChild() }}</li>
<li>meowViewChildren: {{ !!meowViewChildren() }}</li>
<li>meowContentChild: {{ !!meowContentChild() }}</li>
<li>meowContentChildren: {{ !!meowContentChildren() }}</li>
</ul>
`,
})
class NestedComponent {
readonly meowViewChild = viewChild<ElementRef>('meow');
readonly meowViewChildren = viewChildren<ElementRef>('meow');
readonly meowContentChild = contentChild<ElementRef>('meow');
readonly meowContentChildren = contentChildren<ElementRef>('meow');

readonly name = input.required<string>();
}

@Component({
selector: 'app-target',
standalone: true,
imports: [NestedComponent],
template: `
<app-nested></app-nested>
<div>name: {{ name }}</div>
`,
})
class TargetComponent {
@Input() public readonly name: string = '';
}

@Component({
selector: 'app-separate',
standalone: true,
template: `
<div>Is odd: {{ count() % 2 === 1 }}</div>
<h3>Queries:</h3>
<ul>
<li>meowViewChild: {{ !!meowViewChild() }}</li>
<li>meowViewChildren: {{ !!meowViewChildren() }}</li>
<li>meowContentChild: {{ !!meowContentChild() }}</li>
<li>meowContentChildren: {{ !!meowContentChildren() }}</li>
</ul>
`,
})
class SeparateComponent {
readonly count = input.required();

readonly meowViewChild = viewChild<ElementRef>('meow');
readonly meowViewChildren = viewChildren<ElementRef>('meow');
readonly meowContentChild = contentChild<ElementRef>('meow');
readonly meowContentChildren = contentChildren<ElementRef>('meow');
}

describe('issue-8634', () => {
describe('child component', () => {
beforeEach(() => MockBuilder(TargetComponent));

it('should not fail because of the usage of viewChild/contendChild', () => {
const fixture = MockRender(TargetComponent, {
name: 'sandbox',
});
expect(fixture.nativeElement.innerHTML).toContain(
'name: sandbox',
);
});
});

describe('referenced component', () => {
beforeEach(() => MockBuilder(SeparateComponent));

it('should not fail because of the usage of viewChild/contendChild', () => {
const fixture = MockRender(SeparateComponent, {
count: 3,
});
expect(fixture.nativeElement.innerHTML).toContain(
'Is odd: true',
);
});
});
});

0 comments on commit f0f46f3

Please sign in to comment.