Skip to content

Commit

Permalink
test: covering nested replacement help-me-mom#4486
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jan 22, 2023
1 parent 57957d9 commit b499611
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 0 deletions.
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');
});
});

0 comments on commit b499611

Please sign in to comment.