Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: covering nested replacement #4486 #4501

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions tests/issue-4486/angular.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { Component, NgModule, VERSION } from '@angular/core';
import { TestBed } from '@angular/core/testing';

@Component({
selector: 'real',
template: 'real',
})
class RealComponent {}

@NgModule({
declarations: [RealComponent],
exports: [RealComponent],
})
class RealModule {}

@Component({
selector: 'nested',
template: '<real></real>',
})
class NestedComponent {}

@NgModule({
imports: [RealModule],
declarations: [NestedComponent],
exports: [NestedComponent],
})
class NestedModule {}

@Component({
selector: 'target',
standalone: true,
imports: [NestedModule],
template: `<nested></nested>`,
} as never)
class StandaloneComponent {}

@Component({
selector: 'real',
template: 'test',
})
class RealTestingComponent {}

@NgModule({
declarations: [RealTestingComponent],
exports: [RealTestingComponent],
})
class RealTestingModule {}

@NgModule({
declarations: [NestedComponent],
exports: [NestedComponent],
imports: [RealTestingModule],
})
class NestedTestingModule {}

// @see /~https://github.com/help-me-mom/ng-mocks/issues/4486
describe('issue-4486:angular', () => {
if (Number.parseInt(VERSION.major, 10) < 14) {
it('a14', () => {
// pending('Need Angular >= 14');
expect(true).toBeTruthy();
});

return;
}

// Here we check default behavior of the standalone component.
describe('default', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [StandaloneComponent],
}).compileComponents();
});

it('renders RealComponent', () => {
const fixture = TestBed.createComponent(StandaloneComponent);
fixture.detectChanges();

expect(fixture.debugElement.nativeElement.textContent).toEqual(
'real',
);
});
});

// Here we check whether overrideComponent.set removes imports, and it does.
describe('overrideComponent:set:empty', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [StandaloneComponent],
})
.overrideComponent(StandaloneComponent, {
set: {
imports: [],
} as never,
})
.compileComponents();
});

it('renders nothing', () => {
const fixture = TestBed.createComponent(StandaloneComponent);
fixture.detectChanges();

expect(fixture.debugElement.nativeElement.textContent).toEqual(
'',
);
});
});

// Here we check whether overrideComponent.set changes imports, and it does not, however, it has to.
describe('overrideComponent:set:NestedTestingModule', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [StandaloneComponent],
})
.overrideComponent(StandaloneComponent, {
set: {
imports: [NestedTestingModule],
} as never,
})
.compileComponents();
});

it('renders RealTestingComponent', () => {
const fixture = TestBed.createComponent(StandaloneComponent);
fixture.detectChanges();

// The failure is here.
expect(fixture.debugElement.nativeElement.textContent).toEqual(
'test',
);
});
});
});
80 changes: 80 additions & 0 deletions tests/issue-4486/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Component, NgModule, VERSION } from '@angular/core';

import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Component({
selector: 'real',
template: 'real',
})
class RealComponent {}

@NgModule({
declarations: [RealComponent],
exports: [RealComponent],
})
class RealModule {}

@Component({
selector: 'nested',
template: '<real></real>',
})
class NestedComponent {}

@NgModule({
imports: [RealModule],
declarations: [NestedComponent],
exports: [NestedComponent],
})
class NestedModule {}

@Component({
selector: 'target',
standalone: true,
imports: [NestedModule],
template: `<nested></nested>`,
} as never)
class StandaloneComponent {}

@Component({
selector: 'real',
template: 'test',
})
class RealTestingComponent {}

@NgModule({
declarations: [RealTestingComponent],
exports: [RealTestingComponent],
})
class RealTestingModule {}

@NgModule({
declarations: [NestedComponent],
exports: [NestedComponent],
imports: [RealTestingModule],
})
class NestedTestingModule {}

// @see /~https://github.com/help-me-mom/ng-mocks/issues/4486
describe('issue-4486', () => {
if (Number.parseInt(VERSION.major, 10) < 14) {
it('a14', () => {
// pending('Need Angular >= 14');
expect(true).toBeTruthy();
});

return;
}

beforeEach(() =>
MockBuilder(StandaloneComponent).replace(
NestedModule,
NestedTestingModule,
),
);

it('renders RealTestingComponent', () => {
const fixture = MockRender(StandaloneComponent);

expect(ngMocks.formatText(fixture)).toEqual('test');
});
});