Skip to content

Commit

Permalink
feat: add functions to mass mock
Browse files Browse the repository at this point in the history
  • Loading branch information
ike18t committed Mar 25, 2018
1 parent 5af02cd commit fee5a03
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 20 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './lib/mock-component';
export * from './lib/mock-declaration';
export * from './lib/mock-directive';
export * from './lib/mock-module';
export * from './lib/mock-pipe';
3 changes: 3 additions & 0 deletions lib/common/declaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Component, Directive, Pipe, Type } from '@angular/core';

export type Declaration = Type<Component | Directive | Pipe>;
1 change: 1 addition & 0 deletions lib/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Declaration } from './declaration';
6 changes: 3 additions & 3 deletions lib/mock-component/mock-component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { MockComponent } from './mock-component';
import { MockComponent, MockComponents } from './mock-component';
import { EmptyComponent } from './test-components/empty-component.component';
import { SimpleComponent } from './test-components/simple-component.component';

Expand Down Expand Up @@ -33,8 +33,8 @@ describe('MockComponent', () => {
TestBed.configureTestingModule({
declarations: [
ExampleComponentContainer,
MockComponent(EmptyComponent),
MockComponent(SimpleComponent)
MockComponents(EmptyComponent,
SimpleComponent),
],
imports: [
FormsModule
Expand Down
4 changes: 4 additions & 0 deletions lib/mock-component/mock-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { getInputsOutputs } from '../common/reflect';

const cache = new Map<Type<Component>, Type<Component>>();

export function MockComponents<TComponent>(...components: Array<Type<TComponent>>): Array<Type<TComponent>> {
return components.map(MockComponent);
}

export function MockComponent<TComponent>(component: Type<TComponent>): Type<TComponent> {
const cacheHit = cache.get(component);
if (cacheHit) {
Expand Down
1 change: 1 addition & 0 deletions lib/mock-declaration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './mock-declaration';
20 changes: 20 additions & 0 deletions lib/mock-declaration/mock-declaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Declaration } from '../common';
import { MockComponent } from '../mock-component';
import { MockDirective } from '../mock-directive';
import { MockPipe } from '../mock-pipe';

/* tslint:disable-next-line:ban-types */
const mockLookup: { [key: string]: Function } = {
Component: MockComponent,
Directive: MockDirective,
Pipe: MockPipe
};

export function MockDeclarations(...declarations: Declaration[]): Declaration[] {
return declarations.map(MockDeclaration);
}

export function MockDeclaration(declaration: Declaration): Declaration {
const type = (declaration as any).__annotations__[0].__proto__.ngMetadataName;
return mockLookup[type](declaration);
}
4 changes: 4 additions & 0 deletions lib/mock-directive/mock-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { getInputsOutputs } from '../common/reflect';

const cache = new Map<Type<Directive>, Type<Directive>>();

export function MockDirectives<TDirective>(...directives: Array<Type<TDirective>>): Array<Type<TDirective>> {
return directives.map(MockDirective);
}

export function MockDirective<TDirective>(directive: Type<TDirective>): Type<TDirective> {
const cacheHit = cache.get(directive);
if (cacheHit) {
Expand Down
20 changes: 3 additions & 17 deletions lib/mock-module/mock-module.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
import { Component, Directive, NgModule, Pipe, Type } from '@angular/core';
import { MockComponent } from '../mock-component';
import { MockDirective } from '../mock-directive';
import { MockPipe } from '../mock-pipe';
import { NgModule, Type } from '@angular/core';
import { Declaration } from '../common';
import { MockDeclaration } from '../mock-declaration';

export type Declaration = Type<Component | Directive | Pipe>;
interface IModuleOptions {
declarations: Declaration[];
exports: Declaration[];
providers: Array<{ provide: any; useValue: {} }>;
}

/* tslint:disable-next-line:ban-types */
const mockLookup: { [key: string]: Function } = {
Component: MockComponent,
Directive: MockDirective,
Pipe: MockPipe
};

export function MockDeclaration(declaration: Declaration) {
const type = (declaration as any).__annotations__[0].__proto__.ngMetadataName;
return mockLookup[type](declaration);
}

const mockProvider = (provider: any) => ({
provide: provider, useValue: {}
});
Expand Down
4 changes: 4 additions & 0 deletions lib/mock-pipe/mock-pipe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Pipe, PipeTransform, Type } from '@angular/core';

export function MockPipes<TPipe extends PipeTransform>(...pipes: Array<Type<TPipe>>): Array<Type<TPipe>> {
return pipes.map((pipe) => MockPipe(pipe, undefined));
}

export function MockPipe<TPipe extends PipeTransform>(pipe: Type<TPipe>, transform?: TPipe['transform']): Type<TPipe> {
const pipeName = (pipe as any).__annotations__[0].name;
const defaultTransform = (...args: any[]): void => undefined;
Expand Down

0 comments on commit fee5a03

Please sign in to comment.