|
| 1 | +import { Component, NgModule, VERSION } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | + |
| 4 | +@Component({ |
| 5 | + selector: 'real', |
| 6 | + template: 'real', |
| 7 | +}) |
| 8 | +class RealComponent {} |
| 9 | + |
| 10 | +@NgModule({ |
| 11 | + declarations: [RealComponent], |
| 12 | + exports: [RealComponent], |
| 13 | +}) |
| 14 | +class RealModule {} |
| 15 | + |
| 16 | +@Component({ |
| 17 | + selector: 'nested', |
| 18 | + template: '<real></real>', |
| 19 | +}) |
| 20 | +class NestedComponent {} |
| 21 | + |
| 22 | +@NgModule({ |
| 23 | + imports: [RealModule], |
| 24 | + declarations: [NestedComponent], |
| 25 | + exports: [NestedComponent], |
| 26 | +}) |
| 27 | +class NestedModule {} |
| 28 | + |
| 29 | +@Component({ |
| 30 | + selector: 'target', |
| 31 | + standalone: true, |
| 32 | + imports: [NestedModule], |
| 33 | + template: `<nested></nested>`, |
| 34 | +} as never) |
| 35 | +class StandaloneComponent {} |
| 36 | + |
| 37 | +@Component({ |
| 38 | + selector: 'real', |
| 39 | + template: 'test', |
| 40 | +}) |
| 41 | +class RealTestingComponent {} |
| 42 | + |
| 43 | +@NgModule({ |
| 44 | + declarations: [RealTestingComponent], |
| 45 | + exports: [RealTestingComponent], |
| 46 | +}) |
| 47 | +class RealTestingModule {} |
| 48 | + |
| 49 | +@NgModule({ |
| 50 | + declarations: [NestedComponent], |
| 51 | + exports: [NestedComponent], |
| 52 | + imports: [RealTestingModule], |
| 53 | +}) |
| 54 | +class NestedTestingModule {} |
| 55 | + |
| 56 | +// @see https://github.com/help-me-mom/ng-mocks/issues/4486 |
| 57 | +describe('issue-4486:angular', () => { |
| 58 | + if (Number.parseInt(VERSION.major, 10) < 14) { |
| 59 | + it('a14', () => { |
| 60 | + // pending('Need Angular >= 14'); |
| 61 | + expect(true).toBeTruthy(); |
| 62 | + }); |
| 63 | + |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // Here we check default behavior of the standalone component. |
| 68 | + describe('default', () => { |
| 69 | + beforeEach(async () => { |
| 70 | + await TestBed.configureTestingModule({ |
| 71 | + imports: [StandaloneComponent], |
| 72 | + }).compileComponents(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('renders RealComponent', () => { |
| 76 | + const fixture = TestBed.createComponent(StandaloneComponent); |
| 77 | + fixture.detectChanges(); |
| 78 | + |
| 79 | + expect(fixture.debugElement.nativeElement.textContent).toEqual( |
| 80 | + 'real', |
| 81 | + ); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + // Here we check whether overrideComponent.set removes imports, and it does. |
| 86 | + describe('overrideComponent:set:empty', () => { |
| 87 | + beforeEach(async () => { |
| 88 | + await TestBed.configureTestingModule({ |
| 89 | + imports: [StandaloneComponent], |
| 90 | + }) |
| 91 | + .overrideComponent(StandaloneComponent, { |
| 92 | + set: { |
| 93 | + imports: [], |
| 94 | + } as never, |
| 95 | + }) |
| 96 | + .compileComponents(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('renders nothing', () => { |
| 100 | + const fixture = TestBed.createComponent(StandaloneComponent); |
| 101 | + fixture.detectChanges(); |
| 102 | + |
| 103 | + expect(fixture.debugElement.nativeElement.textContent).toEqual( |
| 104 | + '', |
| 105 | + ); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + // Here we check whether overrideComponent.set changes imports, and it does not, however, it has to. |
| 110 | + describe('overrideComponent:set:NestedTestingModule', () => { |
| 111 | + beforeEach(async () => { |
| 112 | + await TestBed.configureTestingModule({ |
| 113 | + imports: [StandaloneComponent], |
| 114 | + }) |
| 115 | + .overrideComponent(StandaloneComponent, { |
| 116 | + set: { |
| 117 | + imports: [NestedTestingModule], |
| 118 | + } as never, |
| 119 | + }) |
| 120 | + .compileComponents(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('renders RealTestingComponent', () => { |
| 124 | + const fixture = TestBed.createComponent(StandaloneComponent); |
| 125 | + fixture.detectChanges(); |
| 126 | + |
| 127 | + // The failure is here. |
| 128 | + expect(fixture.debugElement.nativeElement.textContent).toEqual( |
| 129 | + 'test', |
| 130 | + ); |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments