Commit 458f2ccdac5a5722ccf1e8521d687cdf454cb0d2
1 parent
6d10324a
Exists in
master
and in
30 other branches
Review Modifications
Showing
3 changed files
with
105 additions
and
28 deletions
Show diff stats
src/app/layout/blocks/members-block/members-block.component.spec.ts
| @@ -28,7 +28,12 @@ describe("Components", () => { | @@ -28,7 +28,12 @@ describe("Components", () => { | ||
| 28 | // Custom properties for the component | 28 | // Custom properties for the component |
| 29 | let properties = { owner: { id: 1 } }; | 29 | let properties = { owner: { id: 1 } }; |
| 30 | // Create the component bed for the test. | 30 | // Create the component bed for the test. |
| 31 | - let cls = createClass(htmlTemplate, [MembersBlockComponent], providers, properties); | 31 | + let cls = createClass({ |
| 32 | + template: htmlTemplate, | ||
| 33 | + directives: [MembersBlockComponent], | ||
| 34 | + providers: providers, | ||
| 35 | + properties: properties | ||
| 36 | + }); | ||
| 32 | helper = new ComponentTestHelper(cls, done); | 37 | helper = new ComponentTestHelper(cls, done); |
| 33 | }); | 38 | }); |
| 34 | 39 |
src/app/layout/blocks/people-block/people-block.component.spec.ts
| 1 | import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder'; | 1 | import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder'; |
| 2 | -import {Provider, provide} from 'ng-forward'; | 2 | +import {Provider} from 'ng-forward'; |
| 3 | import {ComponentTestHelper, createClass} from './../../../../spec/component-test-helper'; | 3 | import {ComponentTestHelper, createClass} from './../../../../spec/component-test-helper'; |
| 4 | import {providers} from 'ng-forward/cjs/testing/providers'; | 4 | import {providers} from 'ng-forward/cjs/testing/providers'; |
| 5 | import {PeopleBlockComponent} from './people-block.component'; | 5 | import {PeopleBlockComponent} from './people-block.component'; |
| 6 | 6 | ||
| 7 | -import { INgForwardJQuery } from "ng-forward/cjs/util/jqlite-extensions"; | ||
| 8 | - | ||
| 9 | const htmlTemplate: string = '<noosfero-people-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-people-block>'; | 7 | const htmlTemplate: string = '<noosfero-people-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-people-block>'; |
| 10 | 8 | ||
| 11 | describe("Components", () => { | 9 | describe("Components", () => { |
| @@ -31,7 +29,12 @@ describe("Components", () => { | @@ -31,7 +29,12 @@ describe("Components", () => { | ||
| 31 | beforeEach( (done) => { | 29 | beforeEach( (done) => { |
| 32 | // Create the component bed for the test. Optionally, this could be done | 30 | // Create the component bed for the test. Optionally, this could be done |
| 33 | // in each test if one needs customization of these parameters per test | 31 | // in each test if one needs customization of these parameters per test |
| 34 | - let cls = createClass(htmlTemplate, [PeopleBlockComponent], providers, {}); | 32 | + let cls = createClass({ |
| 33 | + template: htmlTemplate, | ||
| 34 | + directives: [PeopleBlockComponent], | ||
| 35 | + providers: providers, | ||
| 36 | + properties: {} | ||
| 37 | + }); | ||
| 35 | helper = new ComponentTestHelper(cls, done); | 38 | helper = new ComponentTestHelper(cls, done); |
| 36 | }); | 39 | }); |
| 37 | 40 |
src/spec/component-test-helper.ts
| 1 | import { Component } from "ng-forward"; | 1 | import { Component } from "ng-forward"; |
| 2 | -import { TestComponentBuilder } from 'ng-forward/cjs/testing/test-component-builder'; | 2 | +import { TestComponentBuilder, ngClass } from 'ng-forward/cjs/testing/test-component-builder'; |
| 3 | import { INgForwardJQuery } from "ng-forward/cjs/util/jqlite-extensions"; | 3 | import { INgForwardJQuery } from "ng-forward/cjs/util/jqlite-extensions"; |
| 4 | import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder'; | 4 | import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder'; |
| 5 | 5 | ||
| 6 | -export function createClass(template: any, directives: any, providers: any, properties: any): any { | ||
| 7 | - @Component({ selector: 'component-test-helper-container', template, directives, providers }) | ||
| 8 | - class Test { | ||
| 9 | - constructor() { | ||
| 10 | - Object.keys(properties).forEach((key: any) => { | ||
| 11 | - (<any>this)[key] = <any>properties[key]; | ||
| 12 | - }); | ||
| 13 | - } | ||
| 14 | - } | ||
| 15 | - return Test; | ||
| 16 | -} | ||
| 17 | - | ||
| 18 | -export function rebuild(factory: any, done: any): any { | ||
| 19 | - return new ComponentTestHelper(factory, done); | ||
| 20 | -} | ||
| 21 | - | ||
| 22 | /** | 6 | /** |
| 7 | + * @ngdoc object | ||
| 8 | + * @name spec.ComponentTestHelper | ||
| 9 | + * @description | ||
| 10 | + * | ||
| 23 | * Helper class for creating tests. It encapsulates the TestComponentBuilder initialization, | 11 | * Helper class for creating tests. It encapsulates the TestComponentBuilder initialization, |
| 24 | * 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 |
| 25 | * test, and inside construct this object like: | 13 | * test, and inside construct this object like: |
| 26 | * | 14 | * |
| 15 | + * <pre> | ||
| 27 | * let helper = let helper : ComponentTestHelper; | 16 | * let helper = let helper : ComponentTestHelper; |
| 28 | * beforeEach( (done) => { | 17 | * beforeEach( (done) => { |
| 29 | * helper = new ComponentTestHelper(cls, tcb); | 18 | * helper = new ComponentTestHelper(cls, tcb); |
| 30 | * } | 19 | * } |
| 20 | + * </pre> | ||
| 31 | */ | 21 | */ |
| 32 | export class ComponentTestHelper { | 22 | export class ComponentTestHelper { |
| 33 | 23 | ||
| 34 | - mockComponent: any; | 24 | + /** |
| 25 | + * @ngdoc property | ||
| 26 | + * @name mockComponent | ||
| 27 | + * @propertyOf spec.ComponentTestHelper | ||
| 28 | + * @description | ||
| 29 | + * The component we are mocking. | ||
| 30 | + */ | ||
| 31 | + mockComponent: ngClass; | ||
| 32 | + /** | ||
| 33 | + * @ngdoc property | ||
| 34 | + * @name tcb | ||
| 35 | + * @propertyOf spec.ComponentTestHelper | ||
| 36 | + * @description | ||
| 37 | + * The NgForward TestComponentBuilder | ||
| 38 | + */ | ||
| 35 | tcb: TestComponentBuilder; | 39 | tcb: TestComponentBuilder; |
| 40 | + /** | ||
| 41 | + * @ngdoc property | ||
| 42 | + * @name component | ||
| 43 | + * @propertyOf spec.ComponentTestHelper | ||
| 44 | + * @description | ||
| 45 | + * The parsed component instance | ||
| 46 | + */ | ||
| 36 | component: any; | 47 | component: any; |
| 48 | + /** | ||
| 49 | + * @ngdoc property | ||
| 50 | + * @name debugElement | ||
| 51 | + * @propertyOf spec.ComponentTestHelper | ||
| 52 | + * @description | ||
| 53 | + * The debugElement representing a JQuery element attached to the component | ||
| 54 | + * on mock page. | ||
| 55 | + */ | ||
| 37 | debugElement: INgForwardJQuery; | 56 | debugElement: INgForwardJQuery; |
| 38 | 57 | ||
| 39 | - constructor(mockComponent: any, done: any) { | 58 | + /** |
| 59 | + * @ngdoc method | ||
| 60 | + * @name constructor | ||
| 61 | + * @methodOf spec.ComponentTestHelper | ||
| 62 | + * @description | ||
| 63 | + * The constructor for this component. | ||
| 64 | + */ | ||
| 65 | + constructor(mockComponent: any, done: Function) { | ||
| 40 | this.mockComponent = mockComponent; | 66 | this.mockComponent = mockComponent; |
| 41 | this.tcb = new TestComponentBuilder(); | 67 | this.tcb = new TestComponentBuilder(); |
| 42 | this.init(done); | 68 | this.init(done); |
| 43 | } | 69 | } |
| 44 | 70 | ||
| 45 | - init(done: any): any { | ||
| 46 | - let promisse = this.tcb.createAsync(this.mockComponent) as any; | 71 | + /** |
| 72 | + * @ngdoc method | ||
| 73 | + * @name init | ||
| 74 | + * @methodOf spec.ComponentTestHelper | ||
| 75 | + * @description | ||
| 76 | + * The initializer function. It is called inside the constructor | ||
| 77 | + */ | ||
| 78 | + init(done: Function): any { | ||
| 79 | + let promisse = this.tcb.createAsync(this.mockComponent) as Promise<ComponentFixture>; | ||
| 47 | return promisse.then((fixture: any) => { | 80 | return promisse.then((fixture: any) => { |
| 48 | // Fire all angular events and parsing | 81 | // Fire all angular events and parsing |
| 49 | fixture.detectChanges(); | 82 | fixture.detectChanges(); |
| @@ -57,18 +90,54 @@ export class ComponentTestHelper { | @@ -57,18 +90,54 @@ export class ComponentTestHelper { | ||
| 57 | } | 90 | } |
| 58 | 91 | ||
| 59 | /** | 92 | /** |
| 60 | - * Return all elements matching the given selector | 93 | + * @ngdoc method |
| 94 | + * @name all | ||
| 95 | + * @methodOf spec.ComponentTestHelper | ||
| 96 | + * @description | ||
| 97 | + * Return all elements matching the given selector | ||
| 61 | */ | 98 | */ |
| 62 | all(selector: string): INgForwardJQuery[] { | 99 | all(selector: string): INgForwardJQuery[] { |
| 63 | return this.debugElement.queryAll(selector); | 100 | return this.debugElement.queryAll(selector); |
| 64 | } | 101 | } |
| 65 | 102 | ||
| 103 | + /** | ||
| 104 | + * @ngdoc method | ||
| 105 | + * @name find | ||
| 106 | + * @methodOf spec.ComponentTestHelper | ||
| 107 | + * @description | ||
| 108 | + * Return the first element matching the given selector | ||
| 109 | + */ | ||
| 66 | find(selector: string): INgForwardJQuery { | 110 | find(selector: string): INgForwardJQuery { |
| 67 | return this.all(selector)[0]; | 111 | return this.all(selector)[0]; |
| 68 | } | 112 | } |
| 69 | 113 | ||
| 114 | + /** | ||
| 115 | + * @ngdoc method | ||
| 116 | + * @name findChildren | ||
| 117 | + * @methodOf spec.ComponentTestHelper | ||
| 118 | + * @description | ||
| 119 | + * Return the first element of parent element that matches the given selector | ||
| 120 | + */ | ||
| 70 | findChildren(parentSelector: string, childSelector: string) { | 121 | findChildren(parentSelector: string, childSelector: string) { |
| 71 | let parentComponent = this.find(parentSelector); | 122 | let parentComponent = this.find(parentSelector); |
| 72 | return parentComponent.find(childSelector)[0]; | 123 | return parentComponent.find(childSelector)[0]; |
| 73 | } | 124 | } |
| 74 | -} | ||
| 75 | \ No newline at end of file | 125 | \ No newline at end of file |
| 126 | +} | ||
| 127 | + | ||
| 128 | +export function createClass({ | ||
| 129 | + template = '<div></div>', | ||
| 130 | + directives = [], | ||
| 131 | + providers = [], | ||
| 132 | + properties = <any>{} | ||
| 133 | +}): any { | ||
| 134 | + @Component({ selector: 'component-test-helper-container', template, directives, providers }) | ||
| 135 | + class Test { | ||
| 136 | + constructor() { | ||
| 137 | + Object.keys(properties).forEach((key: any) => { | ||
| 138 | + (<any>this)[key] = <any>properties[key]; | ||
| 139 | + }); | ||
| 140 | + } | ||
| 141 | + } | ||
| 142 | + return Test; | ||
| 143 | +} | ||
| 144 | + |