Skip to content

Commit

Permalink
bugfix(core/testing): override provider only if exists in the scope
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Mar 18, 2019
1 parent ee19d1e commit 8e2280d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
33 changes: 25 additions & 8 deletions packages/core/injector/module.ts
Expand Up @@ -207,9 +207,7 @@ export class Module {
provider: CustomFactory | CustomValue | CustomClass,
collection: Map<string, any>,
): string {
const { provide } = provider;
const name = isFunction(provide) ? provide.name : provide;

const name = this.getProviderStaticToken(provider.provide) as string;
provider = {
...provider,
name,
Expand Down Expand Up @@ -359,13 +357,32 @@ export class Module {
}

public replace(toReplace: string | symbol | Type<any>, options: any) {
if (options.isProvider) {
if (options.isProvider && this.hasProvider(toReplace)) {
return this.addProvider({ provide: toReplace, ...options });
} else if (!options.isProvider && this.hasInjectable(toReplace)) {
this.addInjectable({
provide: toReplace,
...options,
});
}
this.addInjectable({
provide: toReplace,
...options,
});
}

public hasProvider(token: string | symbol | Type<any>): boolean {
const name = this.getProviderStaticToken(token);
return this._providers.has(name);
}

public hasInjectable(token: string | symbol | Type<any>): boolean {
const name = this.getProviderStaticToken(token);
return this._injectables.has(name);
}

public getProviderStaticToken(
provider: string | symbol | Type<any>,
): string | symbol {
return isFunction(provider)
? (provider as Function).name
: (provider as string | symbol);
}

public getProviderByKey<T = any>(name: string | symbol): InstanceWrapper<T> {
Expand Down
34 changes: 34 additions & 0 deletions packages/core/test/injector/module.spec.ts
Expand Up @@ -274,13 +274,17 @@ describe('Module', () => {
describe('when provider', () => {
it('should call `addProvider`', () => {
const addProviderSpy = sinon.spy(module, 'addProvider');
sinon.stub(module, 'hasProvider').callsFake(() => true);

module.replace(null, { isProvider: true });
expect(addProviderSpy.called).to.be.true;
});
});
describe('when guard', () => {
it('should call `addInjectable`', () => {
const addInjectableSpy = sinon.spy(module, 'addInjectable');
sinon.stub(module, 'hasInjectable').callsFake(() => true);

module.replace(null, {});
expect(addInjectableSpy.called).to.be.true;
});
Expand Down Expand Up @@ -379,4 +383,34 @@ describe('Module', () => {
});
});
});

describe('hasProvider', () => {
describe('when module has provider', () => {
it('should return true', () => {
const token = 'test';
module.providers.set(token, new InstanceWrapper());
expect(module.hasProvider(token)).to.be.true;
});
});
describe('otherwise', () => {
it('should return false', () => {
expect(module.hasProvider('_')).to.be.false;
});
});
});

describe('hasInjectable', () => {
describe('when module has injectable', () => {
it('should return true', () => {
const token = 'test';
module.injectables.set(token, new InstanceWrapper());
expect(module.hasInjectable(token)).to.be.true;
});
});
describe('otherwise', () => {
it('should return false', () => {
expect(module.hasInjectable('_')).to.be.false;
});
});
});
});
4 changes: 2 additions & 2 deletions packages/testing/testing-module.builder.ts
Expand Up @@ -81,8 +81,8 @@ export class TestingModuleBuilder {
}

private applyOverloadsMap() {
[...this.overloadsMap.entries()].forEach(([component, options]) => {
this.container.replace(component, options);
[...this.overloadsMap.entries()].forEach(([item, options]) => {
this.container.replace(item, options);
});
}

Expand Down

0 comments on commit 8e2280d

Please sign in to comment.