Skip to content

Commit

Permalink
fix: add testbed test that exposed now fixed issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ike18t committed Feb 2, 2018
1 parent 49b2272 commit 610cbdc
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
11 changes: 9 additions & 2 deletions karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
module.exports = (config: any) => {
config.set({
autoWatch: false,
browsers: ['Chrome'],
browsers: ['ChromeHeadless'],
colors: true,
files: [
'node_modules/zone.js/dist/zone.js',
'node_modules/zone.js/dist/long-stack-trace-zone.js',
'node_modules/zone.js/dist/proxy.js',
'node_modules/zone.js/dist/sync-test.js',
'node_modules/zone.js/dist/jasmine-patch.js',
'node_modules/zone.js/dist/async-test.js',
'node_modules/zone.js/dist/fake-async-test.js',
{ pattern: 'lib/**/*.ts' }
],
frameworks: ['jasmine', 'karma-typescript'],
Expand All @@ -16,7 +23,7 @@ module.exports = (config: any) => {
'**/*.ts': ['karma-typescript']
},
reporters: ['dots', 'karma-typescript', 'kjhtml'],
singleRun: false,
singleRun: true,

karmaTypescriptConfig: {
compilerOptions: {
Expand Down
50 changes: 48 additions & 2 deletions lib/mock-module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
import { ParentModule } from './test-fixtures';
import { Component } from '@angular/core';
import { async, ComponentFixture, getTestBed, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

import { ExampleComponent, ParentModule } from './test-fixtures';
import { MockModule } from './mock-module';

@Component({
selector: 'component-subject',
template: `
<example-component></example-component>
<span example-directive></span>
{{ test | examplePipe }}
`
})
class ComponentSubject {
test = 'test';
}

describe('MockModule', () => {
let fixture: ComponentFixture<ComponentSubject>;

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

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
ComponentSubject
],
imports: [
MockModule(ParentModule)
],
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(ComponentSubject);
fixture.detectChanges();
});
}));

it('should do stuff', () => {
MockModule(ParentModule);
const mockedComponent = fixture.debugElement
.query(By.css('example-component'))
.componentInstance as ExampleComponent;
expect(mockedComponent).not.toBeNull();
});
});
24 changes: 19 additions & 5 deletions lib/mock-module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '@angular/core';
import { NgModule, Type } from '@angular/core';
import { MockComponent } from 'mock-component';
import { MockDirective } from 'mock-directive';
import { MockPipe } from 'mock-pipe';
Expand All @@ -18,14 +18,28 @@ const mockProvider = (provider: any) => ({
provide: provider, useValue: {}
});

export function MockModule<TModule>(module: Type<TModule>): Type<TModule> {
class MockedModule {}

export function MockModule<TModule>(module: Type<TModule>): any {
return NgModule(MockIt(module))(MockedModule);
}

function MockIt(module: any): any {
const mockedModule = { declarations: [] as any[],
exports: [] as any[],
providers: [] as any[] };
const declarations = (module as any).__annotations__[0].declarations || [];
const exports = (module as any).__annotations__[0].exports || [];
const imports = (module as any).__annotations__[0].imports || [];
const providers = (module as any).__annotations__[0].providers || [];
mockedModule.declarations = [...imports.map(MockModule),
...declarations.map(mockDeclaration)];
mockedModule.declarations = [...declarations.map(mockDeclaration)];
mockedModule.exports = exports;
mockedModule.providers = providers.map(mockProvider);
return (mockedModule as any) as Type<TModule>;
imports.reduce((acc: any, im: any) => {
const result = MockIt(im);
acc.declarations.push(...result.declarations);
acc.providers.push(...result.providers);
acc.exports.push(...result.declarations);
}, mockedModule);
return mockedModule;
}
10 changes: 8 additions & 2 deletions lib/test-fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NgModule, Component, Injectable, Directive, Pipe, PipeTransform } from '@angular/core';

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

@Pipe({name: 'examplePipe'})
Expand All @@ -17,14 +17,20 @@ export class ExampleService {
}
}

@Component({
selector: 'example-private-component',
template: '<span>Private thing</span>'
})
export class ExamplePrivateComponent { }

@Component({
selector: 'example-component',
template: '<span>My Example</span>'
})
export class ExampleComponent { }

@NgModule({
declarations: [ ExampleComponent, ExamplePipe, ExampleDirective ],
declarations: [ ExamplePrivateComponent, ExampleComponent, ExamplePipe, ExampleDirective ],
exports: [ ExampleComponent, ExamplePipe, ExampleDirective ],
providers: [ ExampleService ]
})
Expand Down
5 changes: 5 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "tslint:all",
"rules": {
"align": false,
"comment-format": false,
"completed-docs": false,
"interface-name": false,
Expand All @@ -10,11 +11,15 @@
"newline-per-chained-call": false,
"no-any": false,
"no-empty": false,
"no-floating-promises": false,
"no-implicit-dependencies": false,
"no-import-side-effect": false,
"no-magic-numbers": false,
"no-unnecessary-type-assertion": false,
"no-parameter-properties": false,
"no-shadowed-variable": false,
"no-submodule-imports": false,
"no-unsafe-any": false,
"only-arrow-functions": false,
"ordered-imports": false,
"prefer-function-over-method": false,
Expand Down

0 comments on commit 610cbdc

Please sign in to comment.