Skip to content

Commit b499611

Browse files
committed
test: covering nested replacement help-me-mom#4486
1 parent 57957d9 commit b499611

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed

tests/issue-4486/angular.spec.ts

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
});

tests/issue-4486/test.spec.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { Component, NgModule, VERSION } from '@angular/core';
2+
3+
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';
4+
5+
@Component({
6+
selector: 'real',
7+
template: 'real',
8+
})
9+
class RealComponent {}
10+
11+
@NgModule({
12+
declarations: [RealComponent],
13+
exports: [RealComponent],
14+
})
15+
class RealModule {}
16+
17+
@Component({
18+
selector: 'nested',
19+
template: '<real></real>',
20+
})
21+
class NestedComponent {}
22+
23+
@NgModule({
24+
imports: [RealModule],
25+
declarations: [NestedComponent],
26+
exports: [NestedComponent],
27+
})
28+
class NestedModule {}
29+
30+
@Component({
31+
selector: 'target',
32+
standalone: true,
33+
imports: [NestedModule],
34+
template: `<nested></nested>`,
35+
} as never)
36+
class StandaloneComponent {}
37+
38+
@Component({
39+
selector: 'real',
40+
template: 'test',
41+
})
42+
class RealTestingComponent {}
43+
44+
@NgModule({
45+
declarations: [RealTestingComponent],
46+
exports: [RealTestingComponent],
47+
})
48+
class RealTestingModule {}
49+
50+
@NgModule({
51+
declarations: [NestedComponent],
52+
exports: [NestedComponent],
53+
imports: [RealTestingModule],
54+
})
55+
class NestedTestingModule {}
56+
57+
// @see https://github.com/help-me-mom/ng-mocks/issues/4486
58+
describe('issue-4486', () => {
59+
if (Number.parseInt(VERSION.major, 10) < 14) {
60+
it('a14', () => {
61+
// pending('Need Angular >= 14');
62+
expect(true).toBeTruthy();
63+
});
64+
65+
return;
66+
}
67+
68+
beforeEach(() =>
69+
MockBuilder(StandaloneComponent).replace(
70+
NestedModule,
71+
NestedTestingModule,
72+
),
73+
);
74+
75+
it('renders RealTestingComponent', () => {
76+
const fixture = MockRender(StandaloneComponent);
77+
78+
expect(ngMocks.formatText(fixture)).toEqual('test');
79+
});
80+
});

0 commit comments

Comments
 (0)