Skip to content

Commit

Permalink
fix(core): correctly defines TestBed with multiple declarations with …
Browse files Browse the repository at this point in the history
…the same selector #4564
  • Loading branch information
satanTime committed Dec 24, 2022
1 parent 79286f7 commit 1e01f82
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
5 changes: 2 additions & 3 deletions examples/MockActivatedRoute/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, NgModule } from '@angular/core';
import { Component, NgModule, OnInit } from '@angular/core';
import { ActivatedRoute, RouterModule } from '@angular/router';

import { MockBuilder, MockInstance, MockRender } from 'ng-mocks';
Expand All @@ -7,11 +7,10 @@ import { MockBuilder, MockInstance, MockRender } from 'ng-mocks';
selector: 'target',
template: '{{ param }}',
})
class TargetComponent {
class TargetComponent implements OnInit {
public param: string | null = null;

public constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.param = this.route.snapshot.paramMap.get('paramId');
}
Expand Down
4 changes: 1 addition & 3 deletions libs/ng-mocks/src/lib/common/ng-mocks-global-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ const configureTestingModule =
isNgModuleDefWithProviders(declaration) ? declaration.ngModule : declaration,
isMockNgDef(funcGetType(declaration)),
]);
if (key === 'imports') {
hasMocks |= mockBuilder[mockBuilder.length - 1][2] ? 0b10 : 0b01;
}
hasMocks |= mockBuilder[mockBuilder.length - 1][2] ? 0b10 : 0b01;
}
}
// We should do magic only then both mock and real exist.
Expand Down
71 changes: 51 additions & 20 deletions tests/issue-4564/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,57 @@ class SubjectUnderTestComponent {}
describe('issue-4564', () => {
ngMocks.throwOnConsole();

beforeEach(() =>
TestBed.configureTestingModule({
imports: [ComponentModule, MockModule(PipeModule)],
declarations: [
SubjectUnderTestComponent,
MockPipe(TargetPipe, () => 'mock'),
MockPipe(StandardPipe),
],
providers: [
MockProvider(TOKEN, 'mock'),
MockProvider(TargetService, {
func: () => 'mock',
}),
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents(),
);
describe('issue', () => {
beforeEach(() =>
TestBed.configureTestingModule({
imports: [ComponentModule, MockModule(PipeModule)],
declarations: [
SubjectUnderTestComponent,
MockPipe(TargetPipe, () => 'mock'),
MockPipe(StandardPipe),
],
providers: [
MockProvider(TOKEN, 'mock'),
MockProvider(TargetService, {
func: () => 'mock',
}),
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents(),
);

it('customizes pipe', () => {
const fixture = MockRender(SubjectUnderTestComponent);
expect(ngMocks.formatText(fixture)).toEqual('mock:mock:mock');
it('customizes pipes', () => {
const fixture = MockRender(SubjectUnderTestComponent);
expect(ngMocks.formatText(fixture)).toEqual('mock:mock:mock');
});
});

describe('only mock imports', () => {
beforeEach(() =>
TestBed.configureTestingModule({
imports: [
MockModule(ComponentModule),
MockModule(PipeModule),
],
declarations: [
TargetComponent,
SubjectUnderTestComponent,
MockPipe(TargetPipe, () => 'mock'),
MockPipe(StandardPipe),
],
providers: [
MockProvider(TOKEN, 'mock'),
MockProvider(TargetService, {
func: () => 'mock',
}),
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents(),
);

it('customizes pipes', () => {
const fixture = MockRender(SubjectUnderTestComponent);
expect(ngMocks.formatText(fixture)).toEqual('mock:mock:mock');
});
});
});

0 comments on commit 1e01f82

Please sign in to comment.