diff --git a/src/app/layout/blocks/members-block/members-block.component.spec.ts b/src/app/layout/blocks/members-block/members-block.component.spec.ts index f6ed115..d2ca953 100644 --- a/src/app/layout/blocks/members-block/members-block.component.spec.ts +++ b/src/app/layout/blocks/members-block/members-block.component.spec.ts @@ -28,7 +28,12 @@ describe("Components", () => { // Custom properties for the component let properties = { owner: { id: 1 } }; // Create the component bed for the test. - let cls = createClass(htmlTemplate, [MembersBlockComponent], providers, properties); + let cls = createClass({ + template: htmlTemplate, + directives: [MembersBlockComponent], + providers: providers, + properties: properties + }); helper = new ComponentTestHelper(cls, done); }); diff --git a/src/app/layout/blocks/people-block/people-block.component.spec.ts b/src/app/layout/blocks/people-block/people-block.component.spec.ts index 6b9ecee..c22025b 100644 --- a/src/app/layout/blocks/people-block/people-block.component.spec.ts +++ b/src/app/layout/blocks/people-block/people-block.component.spec.ts @@ -1,11 +1,9 @@ import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder'; -import {Provider, provide} from 'ng-forward'; +import {Provider} from 'ng-forward'; import {ComponentTestHelper, createClass} from './../../../../spec/component-test-helper'; import {providers} from 'ng-forward/cjs/testing/providers'; import {PeopleBlockComponent} from './people-block.component'; -import { INgForwardJQuery } from "ng-forward/cjs/util/jqlite-extensions"; - const htmlTemplate: string = ''; describe("Components", () => { @@ -31,7 +29,12 @@ describe("Components", () => { beforeEach( (done) => { // Create the component bed for the test. Optionally, this could be done // in each test if one needs customization of these parameters per test - let cls = createClass(htmlTemplate, [PeopleBlockComponent], providers, {}); + let cls = createClass({ + template: htmlTemplate, + directives: [PeopleBlockComponent], + providers: providers, + properties: {} + }); helper = new ComponentTestHelper(cls, done); }); diff --git a/src/spec/component-test-helper.ts b/src/spec/component-test-helper.ts index deb9b5a..0150c88 100644 --- a/src/spec/component-test-helper.ts +++ b/src/spec/component-test-helper.ts @@ -1,49 +1,82 @@ import { Component } from "ng-forward"; -import { TestComponentBuilder } from 'ng-forward/cjs/testing/test-component-builder'; +import { TestComponentBuilder, ngClass } from 'ng-forward/cjs/testing/test-component-builder'; import { INgForwardJQuery } from "ng-forward/cjs/util/jqlite-extensions"; import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder'; -export function createClass(template: any, directives: any, providers: any, properties: any): any { - @Component({ selector: 'component-test-helper-container', template, directives, providers }) - class Test { - constructor() { - Object.keys(properties).forEach((key: any) => { - (this)[key] = properties[key]; - }); - } - } - return Test; -} - -export function rebuild(factory: any, done: any): any { - return new ComponentTestHelper(factory, done); -} - /** + * @ngdoc object + * @name spec.ComponentTestHelper + * @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) => {
  *  helper = new ComponentTestHelper(cls, tcb);
  * }
+ * 
*/ export class ComponentTestHelper { - mockComponent: any; + /** + * @ngdoc property + * @name mockComponent + * @propertyOf spec.ComponentTestHelper + * @description + * The component we are mocking. + */ + mockComponent: ngClass; + /** + * @ngdoc property + * @name tcb + * @propertyOf spec.ComponentTestHelper + * @description + * The NgForward TestComponentBuilder + */ tcb: TestComponentBuilder; + /** + * @ngdoc property + * @name component + * @propertyOf spec.ComponentTestHelper + * @description + * The parsed component instance + */ component: any; + /** + * @ngdoc property + * @name debugElement + * @propertyOf spec.ComponentTestHelper + * @description + * The debugElement representing a JQuery element attached to the component + * on mock page. + */ debugElement: INgForwardJQuery; - constructor(mockComponent: any, done: any) { + /** + * @ngdoc method + * @name constructor + * @methodOf spec.ComponentTestHelper + * @description + * The constructor for this component. + */ + constructor(mockComponent: any, done: Function) { this.mockComponent = mockComponent; this.tcb = new TestComponentBuilder(); this.init(done); } - init(done: any): any { - let promisse = this.tcb.createAsync(this.mockComponent) as any; + /** + * @ngdoc method + * @name init + * @methodOf spec.ComponentTestHelper + * @description + * The initializer function. It is called inside the constructor + */ + init(done: Function): any { + let promisse = this.tcb.createAsync(this.mockComponent) as Promise; return promisse.then((fixture: any) => { // Fire all angular events and parsing fixture.detectChanges(); @@ -57,18 +90,54 @@ export class ComponentTestHelper { } /** - * Return all elements matching the given selector + * @ngdoc method + * @name all + * @methodOf spec.ComponentTestHelper + * @description + * Return all elements matching the given selector */ all(selector: string): INgForwardJQuery[] { return this.debugElement.queryAll(selector); } + /** + * @ngdoc method + * @name find + * @methodOf spec.ComponentTestHelper + * @description + * Return the first element matching the given selector + */ find(selector: string): INgForwardJQuery { return this.all(selector)[0]; } + /** + * @ngdoc method + * @name findChildren + * @methodOf spec.ComponentTestHelper + * @description + * Return the first element of parent element that matches the given selector + */ findChildren(parentSelector: string, childSelector: string) { let parentComponent = this.find(parentSelector); return parentComponent.find(childSelector)[0]; } -} \ No newline at end of file +} + +export function createClass({ + template = '
', + directives = [], + providers = [], + properties = {} +}): any { + @Component({ selector: 'component-test-helper-container', template, directives, providers }) + class Test { + constructor() { + Object.keys(properties).forEach((key: any) => { + (this)[key] = properties[key]; + }); + } + } + return Test; +} + -- libgit2 0.21.2