|
| 1 | +import { |
| 2 | + Component, |
| 3 | + ElementRef, |
| 4 | + Input, |
| 5 | + contentChild, |
| 6 | + contentChildren, |
| 7 | + input, |
| 8 | + viewChild, |
| 9 | + viewChildren, |
| 10 | +} from '@angular/core'; |
| 11 | +import { MockBuilder, MockRender } from 'ng-mocks'; |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: 'app-nested', |
| 15 | + standalone: true, |
| 16 | + template: ` |
| 17 | + <div #meow>Nested content</div> |
| 18 | +
|
| 19 | + <h3>Queries:</h3> |
| 20 | + <ul> |
| 21 | + <li>meowViewChild: {{ !!meowViewChild() }}</li> |
| 22 | + <li>meowViewChildren: {{ !!meowViewChildren() }}</li> |
| 23 | + <li>meowContentChild: {{ !!meowContentChild() }}</li> |
| 24 | + <li>meowContentChildren: {{ !!meowContentChildren() }}</li> |
| 25 | + </ul> |
| 26 | + `, |
| 27 | +}) |
| 28 | +class NestedComponent { |
| 29 | + readonly meowViewChild = viewChild<ElementRef>('meow'); |
| 30 | + readonly meowViewChildren = viewChildren<ElementRef>('meow'); |
| 31 | + readonly meowContentChild = contentChild<ElementRef>('meow'); |
| 32 | + readonly meowContentChildren = contentChildren<ElementRef>('meow'); |
| 33 | + |
| 34 | + readonly name = input.required<string>(); |
| 35 | +} |
| 36 | + |
| 37 | +@Component({ |
| 38 | + selector: 'app-target', |
| 39 | + standalone: true, |
| 40 | + imports: [NestedComponent], |
| 41 | + template: ` |
| 42 | + <app-nested></app-nested> |
| 43 | + <div>name: {{ name }}</div> |
| 44 | + `, |
| 45 | +}) |
| 46 | +class TargetComponent { |
| 47 | + @Input() public readonly name: string = ''; |
| 48 | +} |
| 49 | + |
| 50 | +@Component({ |
| 51 | + selector: 'app-separate', |
| 52 | + standalone: true, |
| 53 | + template: ` |
| 54 | + <div>Is odd: {{ count() % 2 === 1 }}</div> |
| 55 | +
|
| 56 | + <h3>Queries:</h3> |
| 57 | + <ul> |
| 58 | + <li>meowViewChild: {{ !!meowViewChild() }}</li> |
| 59 | + <li>meowViewChildren: {{ !!meowViewChildren() }}</li> |
| 60 | + <li>meowContentChild: {{ !!meowContentChild() }}</li> |
| 61 | + <li>meowContentChildren: {{ !!meowContentChildren() }}</li> |
| 62 | + </ul> |
| 63 | + `, |
| 64 | +}) |
| 65 | +class SeparateComponent { |
| 66 | + readonly count = input.required(); |
| 67 | + |
| 68 | + readonly meowViewChild = viewChild<ElementRef>('meow'); |
| 69 | + readonly meowViewChildren = viewChildren<ElementRef>('meow'); |
| 70 | + readonly meowContentChild = contentChild<ElementRef>('meow'); |
| 71 | + readonly meowContentChildren = contentChildren<ElementRef>('meow'); |
| 72 | +} |
| 73 | + |
| 74 | +describe('issue-8634', () => { |
| 75 | + describe('child component', () => { |
| 76 | + beforeEach(() => MockBuilder(TargetComponent)); |
| 77 | + |
| 78 | + it('should not fail because of the usage of viewChild/contendChild', () => { |
| 79 | + const fixture = MockRender(TargetComponent, { |
| 80 | + name: 'sandbox', |
| 81 | + }); |
| 82 | + expect(fixture.nativeElement.innerHTML).toContain( |
| 83 | + 'name: sandbox', |
| 84 | + ); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('referenced component', () => { |
| 89 | + beforeEach(() => MockBuilder(SeparateComponent)); |
| 90 | + |
| 91 | + it('should not fail because of the usage of viewChild/contendChild', () => { |
| 92 | + const fixture = MockRender(SeparateComponent, { |
| 93 | + count: 3, |
| 94 | + }); |
| 95 | + expect(fixture.nativeElement.innerHTML).toContain( |
| 96 | + 'Is odd: true', |
| 97 | + ); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments