From fc86b9f5e4bf94f82a189a17b363af3d4f278ce1 Mon Sep 17 00:00:00 2001 From: Michel Felipe Date: Wed, 27 Apr 2016 13:45:08 -0300 Subject: [PATCH] Refactory into ComponentTestHelper and helpers.ts with improvements --- src/spec/component-test-helper.ts | 30 +++++++++++++++++------------- src/spec/helpers.ts | 17 +++++++++-------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/spec/component-test-helper.ts b/src/spec/component-test-helper.ts index 0150c88..2441994 100644 --- a/src/spec/component-test-helper.ts +++ b/src/spec/component-test-helper.ts @@ -6,12 +6,12 @@ import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder' /** * @ngdoc object * @name spec.ComponentTestHelper - * @description - * + * @description + * * Helper class for creating tests. It encapsulates the TestComponentBuilder initialization, * allowing the test to be DRY. To use, one must declare a beforeEach function in the * test, and inside construct this object like: - * + * *
  * let helper = let helper : ComponentTestHelper;
  * beforeEach( (done) => {
@@ -19,7 +19,7 @@ import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder'
  * }
  * 
*/ -export class ComponentTestHelper { +export class ComponentTestHelper { /** * @ngdoc property @@ -35,7 +35,7 @@ export class ComponentTestHelper { * @propertyOf spec.ComponentTestHelper * @description * The NgForward TestComponentBuilder - */ + */ tcb: TestComponentBuilder; /** * @ngdoc property @@ -43,8 +43,8 @@ export class ComponentTestHelper { * @propertyOf spec.ComponentTestHelper * @description * The parsed component instance - */ - component: any; + */ + component: T; /** * @ngdoc property * @name debugElement @@ -52,7 +52,7 @@ export class ComponentTestHelper { * @description * The debugElement representing a JQuery element attached to the component * on mock page. - */ + */ debugElement: INgForwardJQuery; /** @@ -62,7 +62,7 @@ export class ComponentTestHelper { * @description * The constructor for this component. */ - constructor(mockComponent: any, done: Function) { + constructor(mockComponent: ngClass, done: Function) { this.mockComponent = mockComponent; this.tcb = new TestComponentBuilder(); this.init(done); @@ -82,7 +82,12 @@ export class ComponentTestHelper { fixture.detectChanges(); // The main debug element this.debugElement = fixture.debugElement; - this.component = this.debugElement.componentViewChildren[0].componentInstance; + this.component = this.debugElement.componentViewChildren[0].componentInstance; + let mockObj = new this.mockComponent(); + Object.keys(mockObj).forEach((key: any) => { + (this.component)[key] = mockObj[key]; + }); + }).then(() => { // Force the resolution of components and sync done(); @@ -126,8 +131,8 @@ export class ComponentTestHelper { export function createClass({ template = '
', - directives = [], - providers = [], + directives = [], + providers = [], properties = {} }): any { @Component({ selector: 'component-test-helper-container', template, directives, providers }) @@ -140,4 +145,3 @@ export function createClass({ } return Test; } - diff --git a/src/spec/helpers.ts b/src/spec/helpers.ts index d1e4770..aa1babb 100644 --- a/src/spec/helpers.ts +++ b/src/spec/helpers.ts @@ -1,7 +1,7 @@ import {ngClass, TestComponentBuilder, ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder'; import {providers} from 'ng-forward/cjs/testing/providers'; -import {Injectable, Inject, Provider, Input, provide, Component} from 'ng-forward'; +import {Injectable, Inject, Provider, Input, Output, EventEmitter, provide, Component} from 'ng-forward'; export var ngforward = { @@ -19,8 +19,8 @@ export interface ComponentFixtureTemplate { export let tcb: TestComponentBuilder = new TestComponentBuilder(); export function quickCreateComponent({ - providers = [], - directives = [], + providers = [], + directives = [], template = '
', properties = {}, }): Promise { @@ -62,7 +62,6 @@ export function provideFilters(...filters: string[]) { return providers; } - @Component({ selector: 'helper_get_angular_service', template: 'not-used', @@ -78,9 +77,11 @@ class AngularServiceHookComponent { * This helper class allows get angular services to be used in integration tests * i.e: '$http', '$q', '$location', etc... */ -class AngularServiceFactory { +export class AngularServiceFactory { + + private fixtureComponentHookPoint: ComponentFixture; - constructor(private fixtureComponentHookPoint: ComponentFixture) { + constructor() { this.fixtureComponentHookPoint = (tcb)["create"](AngularServiceHookComponent); } @@ -97,8 +98,8 @@ class AngularServiceFactory { } } -export function getAngularServiceFactory(fixture: ComponentFixture) { - return new AngularServiceFactory(fixture); +export function getAngularServiceFactory() { + return new AngularServiceFactory(); } export {mocks} from "./mocks"; -- libgit2 0.21.2