Skip to content

Commit

Permalink
feat: mock module providers
Browse files Browse the repository at this point in the history
  • Loading branch information
ike18t committed Feb 2, 2018
1 parent 341c845 commit 49b2272
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/mock-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ const mockLookup: { [key: string]: any } = {
Pipe: MockPipe
};

const mockIt = (declaration: any) => {
const mockDeclaration = (declaration: any) => {
const type = declaration.__annotations__[0].__proto__.ngMetadataName;
return mockLookup[type](declaration);
};

const mockProvider = (provider: any) => ({
provide: provider, useValue: {}
});

export function MockModule<TModule>(module: Type<TModule>): Type<TModule> {
const mockedModule = { declarations: [] as any[] };
const mockedModule = { declarations: [] as any[],
providers: [] as any[] };
const declarations = (module as any).__annotations__[0].declarations || [];
const imports = (module as any).__annotations__[0].imports || [];
const providers = (module as any).__annotations__[0].providers || [];
mockedModule.declarations = [...imports.map(MockModule),
...declarations.map(mockIt)];
...declarations.map(mockDeclaration)];
mockedModule.providers = providers.map(mockProvider);
return (mockedModule as any) as Type<TModule>;
}
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"newline-before-return": false,
"newline-per-chained-call": false,
"no-any": false,
"no-empty": false,
"no-implicit-dependencies": false,
"no-import-side-effect": false,
"no-magic-numbers": false,
Expand Down

0 comments on commit 49b2272

Please sign in to comment.