Skip to content

Commit

Permalink
feat: merge in mock-module
Browse files Browse the repository at this point in the history
  • Loading branch information
ike18t committed Mar 6, 2018
1 parent a1569bd commit 05eaebe
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 66 deletions.
35 changes: 0 additions & 35 deletions gulpfile.ts

This file was deleted.

1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { MockComponent } from './lib/mock-component';
export { MockDirective } from './lib/mock-directive';
export { MockModule } from './lib/mock-module';
export { MockPipe } from './lib/mock-pipe';
1 change: 1 addition & 0 deletions lib/mock-module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MockModule } from './mock-module';
15 changes: 3 additions & 12 deletions lib/mock-module.spec.ts → lib/mock-module/mock-module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { Component } from '@angular/core';
import { async, ComponentFixture, getTestBed, TestBed } from '@angular/core/testing';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
import { MockComponent } from 'mock-component';
import { ExampleComponent, ParentModule } from './test-fixtures';
import { MockComponent } from '../mock-component';
import { MockModule } from './mock-module';
import { ExampleComponent, ParentModule } from './test-fixtures';

@Component({
selector: 'component-subject',
Expand All @@ -24,11 +20,6 @@ class ComponentSubject {
describe('MockModule', () => {
let fixture: ComponentFixture<ComponentSubject>;

getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
Expand Down
27 changes: 15 additions & 12 deletions lib/mock-module.ts → lib/mock-module/mock-module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { NgModule, Type, Component, Pipe, Directive } from '@angular/core';
import { MockComponent } from 'mock-component';
import { MockDirective } from 'mock-directive';
import { MockPipe } from 'mock-pipe';
import { Component, Directive, NgModule, Pipe, Type } from '@angular/core';
import { MockComponent } from '../mock-component';
import { MockDirective } from '../mock-directive';
import { MockPipe } from '../mock-pipe';

type Declaration = Type<Component | Directive | Pipe>;
type moduleOptions = { declarations: Declaration[];
exports: Declaration[];
providers: Array<{ provide: any; useValue: {} }>; };
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,
Expand All @@ -27,18 +30,18 @@ export function MockModule(module: Type<NgModule>): Type<NgModule> {
return NgModule(MockIt(module))(class MockedModule {});
}

function MockIt(module: Type<NgModule>): moduleOptions {
const mockedModule: moduleOptions = { declarations: [],
exports: [],
providers: [] };
function MockIt(module: Type<NgModule>): IModuleOptions {
const mockedModule: IModuleOptions = { declarations: [],
exports: [],
providers: [] };
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.exports = mockedModule.declarations = [...declarations.map(mockDeclaration)];
mockedModule.providers = providers.map(mockProvider);

imports.reduce((acc: moduleOptions, imPort: Type<NgModule>) => {
imports.reduce((acc: IModuleOptions, imPort: Type<NgModule>) => {
const result = MockIt(imPort);
acc.declarations.push(...result.declarations);
acc.providers.push(...result.providers);
Expand Down
12 changes: 5 additions & 7 deletions lib/test-fixtures.ts → lib/mock-module/test-fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { NgModule, Component, Injectable, Directive, Pipe, PipeTransform } from '@angular/core';
/* tslint:disable:max-classes-per-file */

import { Component, Directive, Injectable, NgModule, Pipe, PipeTransform } from '@angular/core';

@Directive({selector: '[example-directive]'})
export class ExampleDirective {}

@Pipe({name: 'examplePipe'})
export class ExamplePipe implements PipeTransform {
transform(text: string) {
return `Example: ${text}`;
}
transform = (text: string) => `Example: ${text}`;
}

@Injectable()
export class ExampleService {
get(id: number) {
return `Got: ${id}`;
}
get = (id: number) => `Got: ${id}`;
}

@Component({
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "tslint:all",
"rules": {
"align": false,
"completed-docs": false,
"member-access": false,
"newline-before-return": false,
Expand Down

0 comments on commit 05eaebe

Please sign in to comment.