Skip to content

Commit

Permalink
fix: detection of empty modules in mock process
Browse files Browse the repository at this point in the history
closes #142
  • Loading branch information
satanTime committed Jun 18, 2020
1 parent a1bc6e4 commit 7427e29
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/mock-module/mock-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
flatten,
getMockedNgDefOf,
isNgDef,
isNgInjectionToken,
isNgModuleDefWithProviders,
Mock,
MockOf,
Expand Down Expand Up @@ -160,7 +161,7 @@ const NEVER_MOCK: Array<Type<any>> = [CommonModule, ApplicationModule];

// tslint:disable-next-line:cyclomatic-complexity
function MockNgModuleDef(ngModuleDef: NgModule, ngModule?: Type<any>): [boolean, NgModule] {
let changed = false;
let changed = !ngMocksUniverse.flags.has('skipMock');
const mockedModuleDef: NgModule = {};
const {
bootstrap = [],
Expand Down Expand Up @@ -204,7 +205,9 @@ function MockNgModuleDef(ngModuleDef: NgModule, ngModule?: Type<any>): [boolean,
mockedDef = MockProvider(def);
}

resolutions.set(provider, mockedDef);
if (!isNgInjectionToken(provider) || def !== mockedDef) {
resolutions.set(provider, mockedDef);
}
changed = changed || mockedDef !== def;
return multi && typeof mockedDef === 'object' ? { ...mockedDef, multi } : mockedDef;
};
Expand Down
47 changes: 47 additions & 0 deletions tests/issue-142/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Component, Inject, InjectionToken, NgModule } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { MockModule } from 'ng-mocks';

export const TARGET_TOKEN = new InjectionToken('TARGET_TOKEN');

@NgModule()
export class TargetModule {
public static forRoot() {
return {
ngModule: TargetModule,
providers: [
{
multi: true,
provide: TARGET_TOKEN,
useValue: true,
},
],
};
}

public readonly targetToken: boolean = false;

constructor(@Inject(TARGET_TOKEN) targetToken: boolean) {
this.targetToken = targetToken;
}
}

@Component({
selector: 'target',
template: 'target',
})
export class TargetComponent {}

describe('issue-142', () => {
beforeEach(() =>
TestBed.configureTestingModule({
declarations: [TargetComponent],
imports: [MockModule(TargetModule.forRoot())],
}).compileComponents()
);

it('test', () => {
const fixture = TestBed.createComponent(TargetComponent);
expect(fixture).toBeDefined();
});
});

0 comments on commit 7427e29

Please sign in to comment.