Skip to content

Commit

Permalink
Merge package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil.mysliwiec committed May 6, 2017
2 parents 373f529 + cd3470e commit 8332f17
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 47 deletions.
5 changes: 4 additions & 1 deletion example/modules/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { ChatGateway } from './chat.gateway';

@Module({
controllers: [ UsersController ],
components: [ UsersService, ChatGateway ],
components: [
{ provide: 'UsersService', useClass: UsersService },
ChatGateway,
],
})
export class UsersModule implements NestModule {
public configure(consumer: MiddlewaresConsumer) {
Expand Down
28 changes: 17 additions & 11 deletions src/core/injector/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RuntimeException } from '../../errors/exceptions/runtime.exception';

export interface CustomComponent {
provide: any;
name: string;
}
export type OpaqueToken = string | symbol | object | Metatype<any>;
export type CustomClass = CustomComponent & { useClass: Metatype<any> };
Expand Down Expand Up @@ -91,10 +92,17 @@ export class Module {
return !isNil((component as CustomComponent).provide);
}

public addCustomComponent(component: ComponentMetatype) {
if (this.isCustomClass(component)) this.addCustomClass(component);
else if (this.isCustomValue(component)) this.addCustomValue(component);
else if (this.isCustomFactory(component)) this.addCustomFactory(component);
public addCustomComponent(component: CustomFactory | CustomValue | CustomClass) {
const { provide } = component;
const name = isFunction(provide) ? provide.name : provide;
const comp = {
...component,
name,
};

if (this.isCustomClass(comp)) this.addCustomClass(comp);
else if (this.isCustomValue(comp)) this.addCustomValue(comp);
else if (this.isCustomFactory(comp)) this.addCustomFactory(comp);
}

public isCustomClass(component): component is CustomClass {
Expand All @@ -110,19 +118,17 @@ export class Module {
}

public addCustomClass(component: CustomClass) {
const { provide: metatype, useClass } = component;
this._components.set(metatype.name, {
name: metatype.name,
const { provide, name, useClass } = component;
this._components.set(name, {
name,
metatype: useClass,
instance: null,
isResolved: false,
});
}

public addCustomValue(component: CustomValue) {
const { provide, useValue: value } = component;
const name = isFunction(provide) ? provide.name : provide;

const { provide, name, useValue: value } = component;
this._components.set(name, {
name,
metatype: null,
Expand All @@ -133,7 +139,7 @@ export class Module {
}

public addCustomFactory(component: CustomFactory){
const { provide: name, useFactory: factory, inject } = component;
const { provide, name, useFactory: factory, inject } = component;
this._components.set(name, {
name,
metatype: factory as any,
Expand Down
55 changes: 20 additions & 35 deletions src/core/test/injector/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('Module', () => {

describe('addCustomClass', () => {
const type = { name: 'TypeTest' };
const component = { provide: type, useClass: type };
const component = { provide: type, useClass: type, name: 'test' };
let setSpy;
beforeEach(() => {
const collection = new Map();
Expand All @@ -102,8 +102,8 @@ describe('Module', () => {
});
it('should store component', () => {
module.addCustomClass(component as any);
expect(setSpy.calledWith(type.name, {
name: type.name,
expect(setSpy.calledWith(component.name, {
name: component.name,
metatype: type,
instance: null,
isResolved: false,
Expand All @@ -113,47 +113,32 @@ describe('Module', () => {

describe('addCustomValue', () => {
let setSpy;
const value = () => ({});
const name = 'test';
const component = { provide: value, name, useValue: value };

beforeEach(() => {
const collection = new Map();
setSpy = sinon.spy(collection, 'set');
(module as any)._components = collection;
});
describe('when value is a function', () => {
const value = () => ({});
const component = { provide: value, useValue: value };

it('should store component', () => {
module.addCustomValue(component as any);
expect(setSpy.calledWith(value.name, {
name: value.name,
metatype: null,
instance: value,
isResolved: true,
isNotMetatype: true,
})).to.be.true;
});
});
describe('when value is not a function', () => {
const value = 'Test';
const component = { provide: value, useValue: value };

it('should store component', () => {
module.addCustomValue(component as any);
expect(setSpy.calledWith(value, {
name: value,
metatype: null,
instance: value,
isResolved: true,
isNotMetatype: true,
})).to.be.true;
});

it('should store component', () => {
module.addCustomValue(component as any);
expect(setSpy.calledWith(name, {
name,
metatype: null,
instance: value,
isResolved: true,
isNotMetatype: true,
})).to.be.true;
});
});

describe('addCustomFactory', () => {
const type = { name: 'TypeTest' };
const inject = [1, 2, 3];
const component = { provide: type, useFactory: type, inject };
const component = { provide: type, useFactory: type, name: 'test', inject };

let setSpy;
beforeEach(() => {
Expand All @@ -163,8 +148,8 @@ describe('Module', () => {
});
it('should store component', () => {
module.addCustomFactory(component as any);
expect(setSpy.getCall(0).args).to.deep.equal([type, {
name: type,
expect(setSpy.getCall(0).args).to.deep.equal([component.name, {
name: component.name,
metatype: type,
instance: null,
isResolved: false,
Expand Down

0 comments on commit 8332f17

Please sign in to comment.