Skip to content

Commit

Permalink
fix(root): ignore signal-based queries
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-stepanenko committed May 28, 2024
1 parent 5c98e73 commit 6f6b616
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 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 @@ -3,7 +3,7 @@ import { Query, TemplateRef, ViewChild, ViewContainerRef } from '@angular/core';
const viewChildArgs: any = { read: ViewContainerRef, static: false };

const viewChildTemplate = (selector: string, key: string): string =>
`<div *ngIf="ngMocksRender_${key}_${selector}" data-${key}="${selector}"><ng-template #${key}_${selector}></ng-template></div>`;
`@if (ngMocksRender_${key}_${selector}) { <div data-${key}="${selector}"><ng-template #${key}_${selector}></ng-template></div> }`;

const isTemplateRefQuery = (query: Query): boolean => {
if (query.isViewQuery) {
Expand All @@ -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;
}
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
47 changes: 47 additions & 0 deletions tests-e2e/src/issue-8634/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
Component,
ElementRef,
Input,
contentChild,
input,
viewChild,
} from '@angular/core';
import { MockBuilder, MockRender } from 'ng-mocks';

@Component({
selector: 'app-nested',
standalone: true,
template: `<div #meow>Nested content</div>`,
})
class NestedComponent {
public readonly meowV = viewChild<ElementRef>('meow');
public readonly meowC = contentChild<ElementRef>('meow');

public 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 = '';
}

describe('my sandbox', () => {
beforeEach(() => MockBuilder(TargetComponent));

it('should do something', () => {
const fixture = MockRender(TargetComponent, {
name: 'sandbox',
});
expect(fixture.nativeElement.innerHTML).toContain(
'name: sandbox',
);
});
});

0 comments on commit 6f6b616

Please sign in to comment.