Commit 0e6cfa00f622e3c43640aed6e74b2a22ff9357ab
Exists in
master
and in
28 other branches
Merge branch 'component-test-improvements' into 'master'
Refactory - Component test improvements Some improvements were made into `ComponentTestHelper` class and `helpers.ts` file. See the list below: **ComponentTestHelper** * Added the **generics** type into this class and pass it to `this.component` attribute. * Remove the unused temp class to `AngularServiceFactory` and unused `getEvents()` method * Added some type casts See merge request !9
Showing
2 changed files
with
26 additions
and
21 deletions
Show diff stats
src/spec/component-test-helper.ts
@@ -6,12 +6,12 @@ import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder' | @@ -6,12 +6,12 @@ import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder' | ||
6 | /** | 6 | /** |
7 | * @ngdoc object | 7 | * @ngdoc object |
8 | * @name spec.ComponentTestHelper | 8 | * @name spec.ComponentTestHelper |
9 | - * @description | ||
10 | - * | 9 | + * @description |
10 | + * | ||
11 | * Helper class for creating tests. It encapsulates the TestComponentBuilder initialization, | 11 | * Helper class for creating tests. It encapsulates the TestComponentBuilder initialization, |
12 | * allowing the test to be DRY. To use, one must declare a beforeEach function in the | 12 | * allowing the test to be DRY. To use, one must declare a beforeEach function in the |
13 | * test, and inside construct this object like: | 13 | * test, and inside construct this object like: |
14 | - * | 14 | + * |
15 | * <pre> | 15 | * <pre> |
16 | * let helper = let helper : ComponentTestHelper; | 16 | * let helper = let helper : ComponentTestHelper; |
17 | * beforeEach( (done) => { | 17 | * beforeEach( (done) => { |
@@ -19,7 +19,7 @@ import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder' | @@ -19,7 +19,7 @@ import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder' | ||
19 | * } | 19 | * } |
20 | * </pre> | 20 | * </pre> |
21 | */ | 21 | */ |
22 | -export class ComponentTestHelper { | 22 | +export class ComponentTestHelper<T extends any> { |
23 | 23 | ||
24 | /** | 24 | /** |
25 | * @ngdoc property | 25 | * @ngdoc property |
@@ -35,7 +35,7 @@ export class ComponentTestHelper { | @@ -35,7 +35,7 @@ export class ComponentTestHelper { | ||
35 | * @propertyOf spec.ComponentTestHelper | 35 | * @propertyOf spec.ComponentTestHelper |
36 | * @description | 36 | * @description |
37 | * The NgForward TestComponentBuilder | 37 | * The NgForward TestComponentBuilder |
38 | - */ | 38 | + */ |
39 | tcb: TestComponentBuilder; | 39 | tcb: TestComponentBuilder; |
40 | /** | 40 | /** |
41 | * @ngdoc property | 41 | * @ngdoc property |
@@ -43,8 +43,8 @@ export class ComponentTestHelper { | @@ -43,8 +43,8 @@ export class ComponentTestHelper { | ||
43 | * @propertyOf spec.ComponentTestHelper | 43 | * @propertyOf spec.ComponentTestHelper |
44 | * @description | 44 | * @description |
45 | * The parsed component instance | 45 | * The parsed component instance |
46 | - */ | ||
47 | - component: any; | 46 | + */ |
47 | + component: T; | ||
48 | /** | 48 | /** |
49 | * @ngdoc property | 49 | * @ngdoc property |
50 | * @name debugElement | 50 | * @name debugElement |
@@ -52,7 +52,7 @@ export class ComponentTestHelper { | @@ -52,7 +52,7 @@ export class ComponentTestHelper { | ||
52 | * @description | 52 | * @description |
53 | * The debugElement representing a JQuery element attached to the component | 53 | * The debugElement representing a JQuery element attached to the component |
54 | * on mock page. | 54 | * on mock page. |
55 | - */ | 55 | + */ |
56 | debugElement: INgForwardJQuery; | 56 | debugElement: INgForwardJQuery; |
57 | 57 | ||
58 | /** | 58 | /** |
@@ -62,7 +62,7 @@ export class ComponentTestHelper { | @@ -62,7 +62,7 @@ export class ComponentTestHelper { | ||
62 | * @description | 62 | * @description |
63 | * The constructor for this component. | 63 | * The constructor for this component. |
64 | */ | 64 | */ |
65 | - constructor(mockComponent: any, done: Function) { | 65 | + constructor(mockComponent: ngClass, done: Function) { |
66 | this.mockComponent = mockComponent; | 66 | this.mockComponent = mockComponent; |
67 | this.tcb = new TestComponentBuilder(); | 67 | this.tcb = new TestComponentBuilder(); |
68 | this.init(done); | 68 | this.init(done); |
@@ -82,7 +82,12 @@ export class ComponentTestHelper { | @@ -82,7 +82,12 @@ export class ComponentTestHelper { | ||
82 | fixture.detectChanges(); | 82 | fixture.detectChanges(); |
83 | // The main debug element | 83 | // The main debug element |
84 | this.debugElement = fixture.debugElement; | 84 | this.debugElement = fixture.debugElement; |
85 | - this.component = this.debugElement.componentViewChildren[0].componentInstance; | 85 | + this.component = <T>this.debugElement.componentViewChildren[0].componentInstance; |
86 | + let mockObj = new this.mockComponent(); | ||
87 | + Object.keys(mockObj).forEach((key: any) => { | ||
88 | + (<any>this.component)[key] = <any>mockObj[key]; | ||
89 | + }); | ||
90 | + | ||
86 | }).then(() => { | 91 | }).then(() => { |
87 | // Force the resolution of components and sync | 92 | // Force the resolution of components and sync |
88 | done(); | 93 | done(); |
@@ -126,8 +131,8 @@ export class ComponentTestHelper { | @@ -126,8 +131,8 @@ export class ComponentTestHelper { | ||
126 | 131 | ||
127 | export function createClass({ | 132 | export function createClass({ |
128 | template = '<div></div>', | 133 | template = '<div></div>', |
129 | - directives = [], | ||
130 | - providers = [], | 134 | + directives = <any[]>[], |
135 | + providers = <any[]>[], | ||
131 | properties = <any>{} | 136 | properties = <any>{} |
132 | }): any { | 137 | }): any { |
133 | @Component({ selector: 'component-test-helper-container', template, directives, providers }) | 138 | @Component({ selector: 'component-test-helper-container', template, directives, providers }) |
@@ -140,4 +145,3 @@ export function createClass({ | @@ -140,4 +145,3 @@ export function createClass({ | ||
140 | } | 145 | } |
141 | return Test; | 146 | return Test; |
142 | } | 147 | } |
143 | - |
src/spec/helpers.ts
1 | 1 | ||
2 | import {ngClass, TestComponentBuilder, ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder'; | 2 | import {ngClass, TestComponentBuilder, ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder'; |
3 | import {providers} from 'ng-forward/cjs/testing/providers'; | 3 | import {providers} from 'ng-forward/cjs/testing/providers'; |
4 | -import {Injectable, Inject, Provider, Input, provide, Component} from 'ng-forward'; | 4 | +import {Injectable, Inject, Provider, Input, Output, EventEmitter, provide, Component} from 'ng-forward'; |
5 | 5 | ||
6 | 6 | ||
7 | export var ngforward = { | 7 | export var ngforward = { |
@@ -19,8 +19,8 @@ export interface ComponentFixtureTemplate { | @@ -19,8 +19,8 @@ export interface ComponentFixtureTemplate { | ||
19 | export let tcb: TestComponentBuilder = new TestComponentBuilder(); | 19 | export let tcb: TestComponentBuilder = new TestComponentBuilder(); |
20 | 20 | ||
21 | export function quickCreateComponent({ | 21 | export function quickCreateComponent({ |
22 | - providers = [], | ||
23 | - directives = [], | 22 | + providers = <any[]>[], |
23 | + directives = <any[]>[], | ||
24 | template = '<div></div>', | 24 | template = '<div></div>', |
25 | properties = <any>{}, | 25 | properties = <any>{}, |
26 | }): Promise<ComponentFixture> { | 26 | }): Promise<ComponentFixture> { |
@@ -62,7 +62,6 @@ export function provideFilters(...filters: string[]) { | @@ -62,7 +62,6 @@ export function provideFilters(...filters: string[]) { | ||
62 | return providers; | 62 | return providers; |
63 | } | 63 | } |
64 | 64 | ||
65 | - | ||
66 | @Component({ | 65 | @Component({ |
67 | selector: 'helper_get_angular_service', | 66 | selector: 'helper_get_angular_service', |
68 | template: 'not-used', | 67 | template: 'not-used', |
@@ -78,9 +77,11 @@ class AngularServiceHookComponent { | @@ -78,9 +77,11 @@ class AngularServiceHookComponent { | ||
78 | * This helper class allows get angular services to be used in integration tests | 77 | * This helper class allows get angular services to be used in integration tests |
79 | * i.e: '$http', '$q', '$location', etc... | 78 | * i.e: '$http', '$q', '$location', etc... |
80 | */ | 79 | */ |
81 | -class AngularServiceFactory { | 80 | +export class AngularServiceFactory { |
81 | + | ||
82 | + private fixtureComponentHookPoint: ComponentFixture; | ||
82 | 83 | ||
83 | - constructor(private fixtureComponentHookPoint: ComponentFixture) { | 84 | + constructor() { |
84 | this.fixtureComponentHookPoint = (<any>tcb)["create"](AngularServiceHookComponent); | 85 | this.fixtureComponentHookPoint = (<any>tcb)["create"](AngularServiceHookComponent); |
85 | } | 86 | } |
86 | 87 | ||
@@ -97,8 +98,8 @@ class AngularServiceFactory { | @@ -97,8 +98,8 @@ class AngularServiceFactory { | ||
97 | } | 98 | } |
98 | } | 99 | } |
99 | 100 | ||
100 | -export function getAngularServiceFactory(fixture: ComponentFixture) { | ||
101 | - return new AngularServiceFactory(fixture); | 101 | +export function getAngularServiceFactory() { |
102 | + return new AngularServiceFactory(); | ||
102 | } | 103 | } |
103 | 104 | ||
104 | export {mocks} from "./mocks"; | 105 | export {mocks} from "./mocks"; |