Merge Request #5
People block refactoring and component-test-helper
Refactored people-block and members-block to use the newly created component-test-helper
-
Reassigned to @mfdeveloper
-
@carloseugenio Please, like us face-to-face conversation, release a
git rebase
in your local development environment. After that, perform thegit push -f
to force update thepeople-block
branch just with your specific changes.So, I like so much your implementation to more clean unit specs. I will suggest some improvements to overrides mocks like: providers,filters...These suggestions will be in comments at lines of each
.ts
file :) -
Please, remove the unused
rebuild()
global function. The feature to redo thetcb.createAsync()
is not working. I tried some implementations to do that, but Angular 1.x use the $injector singleton to register mocks. Because of this, if you try recreate a component, one error is thrown: Injector already created. can not register a module.My suggestion is: Remove this implementation for now, and use the another approach implemented by @abner that use
quickCreateComponent()
when you need override providers(filters, services...).See this link like a reference: http://stackoverflow.com/questions/24900067/injector-already-created-can-not-register-a-module
-
In
mockComponent
param, replace theany
type tongClass
. For recognize this type into this file, add theimport {ngClass} from "...test-component-builder.ts"
together with the others import declarations.Replace too
done
param type toFunction
-
Replace
done
type toFunction
-
Replace
any
type toPromise<ComponentFixture>
generic class. -
Add the
INgForwardJQuery
type to theparentComponent
variable -
There is no need. The type is inferred by TypeScript by the calling context.
-
Why did you use this mock here? Did you not create a reusable
environmentService
into mocks.ts file? You need choice which these implementations will be here! -
The mock in mocks.ts it is an empty generic mock, like others in this file. It does not return a value and this test needs one value. I think it is more direct approach then using the promiseResultTemplate in this test.
-
This shorthand
createClass
function needs a refactory into your parameters declaration. In this case, will be more legibility if you define like key => value params. Please, see thequickCreateComponent()
implementation on helpers.ts file :) -
For create this usage example, Can you use your solution for angular classes docs?
-
Replace
any
to correct type here too -
component is of type any.
-
Can you use the
params
value intopeople
on promise, instead a empty object({}
) ? -
Please, remove the unused import
-
Reassigned to @carloseugenio
-
Reassigned to @mfdeveloper
-
mentioned in commit 7bab2b410b76a88463fc61b0218f685b054893ec
3 | +import { INgForwardJQuery } from "ng-forward/cjs/util/jqlite-extensions"; | |
4 | +import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder'; | |
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 { | |
1 |
|
24 | + * allowing the test to be DRY. To use, one must declare a beforeEach function in the | |
25 | + * test, and inside construct this object like: | |
26 | + * | |
27 | + * let helper = let helper : ComponentTestHelper; | |
28 | + * beforeEach( (done) => { | |
29 | + * helper = new ComponentTestHelper(cls, tcb); | |
30 | + * } | |
31 | + */ | |
32 | +export class ComponentTestHelper { | |
33 | + | |
34 | + mockComponent: any; | |
35 | + tcb: TestComponentBuilder; | |
36 | + component: any; | |
37 | + debugElement: INgForwardJQuery; | |
38 | + | |
39 | + constructor(mockComponent: any, done: any) { | |
1 |
|
30 | + * } | |
31 | + */ | |
32 | +export class ComponentTestHelper { | |
33 | + | |
34 | + mockComponent: any; | |
35 | + tcb: TestComponentBuilder; | |
36 | + component: any; | |
37 | + debugElement: INgForwardJQuery; | |
38 | + | |
39 | + constructor(mockComponent: any, done: any) { | |
40 | + this.mockComponent = mockComponent; | |
41 | + this.tcb = new TestComponentBuilder(); | |
42 | + this.init(done); | |
43 | + } | |
44 | + | |
45 | + init(done: any): any { | |
1 |
|
31 | + */ | |
32 | +export class ComponentTestHelper { | |
33 | + | |
34 | + mockComponent: any; | |
35 | + tcb: TestComponentBuilder; | |
36 | + component: any; | |
37 | + debugElement: INgForwardJQuery; | |
38 | + | |
39 | + constructor(mockComponent: any, done: any) { | |
40 | + this.mockComponent = mockComponent; | |
41 | + this.tcb = new TestComponentBuilder(); | |
42 | + this.init(done); | |
43 | + } | |
44 | + | |
45 | + init(done: any): any { | |
46 | + let promisse = this.tcb.createAsync(this.mockComponent) as any; | |
1 |
|
56 | + }); | |
57 | + } | |
58 | + | |
59 | + /** | |
60 | + * Return all elements matching the given selector | |
61 | + */ | |
62 | + all(selector: string): INgForwardJQuery[] { | |
63 | + return this.debugElement.queryAll(selector); | |
64 | + } | |
65 | + | |
66 | + find(selector: string): INgForwardJQuery { | |
67 | + return this.all(selector)[0]; | |
68 | + } | |
69 | + | |
70 | + findChildren(parentSelector: string, childSelector: string) { | |
71 | + let parentComponent = this.find(parentSelector); | |
2 |
|
9 | +const htmlTemplate: string = '<noosfero-people-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-people-block>'; | |
9 | 10 | |
10 | 11 | describe("Components", () => { |
11 | - describe("People Block Component", () => { | |
12 | 12 | |
13 | - beforeEach(angular.mock.module("templates")); | |
14 | - | |
15 | - let state = jasmine.createSpyObj("state", ["go"]); | |
16 | - let providers = [ | |
17 | - new Provider('truncateFilter', { useValue: () => { } }), | |
18 | - new Provider('stripTagsFilter', { useValue: () => { } }), | |
19 | - new Provider('$state', { useValue: state }), | |
20 | - new Provider('EnvironmentService', { | |
21 | - useValue: { | |
13 | + describe("People Block Component", () => { | |
14 | + let serviceMock = { | |
2 |
|
1 | +import { Component } from "ng-forward"; | |
2 | +import { TestComponentBuilder } from 'ng-forward/cjs/testing/test-component-builder'; | |
3 | +import { INgForwardJQuery } from "ng-forward/cjs/util/jqlite-extensions"; | |
4 | +import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder'; | |
5 | + | |
6 | +export function createClass(template: any, directives: any, providers: any, properties: any): any { | |
1 |
|
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 | +/** | |
23 | + * 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 | |
25 | + * test, and inside construct this object like: | |
26 | + * | |
27 | + * let helper = let helper : ComponentTestHelper; | |
1 |
|
21 | + | |
22 | +/** | |
23 | + * 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 | |
25 | + * test, and inside construct this object like: | |
26 | + * | |
27 | + * let helper = let helper : ComponentTestHelper; | |
28 | + * beforeEach( (done) => { | |
29 | + * helper = new ComponentTestHelper(cls, tcb); | |
30 | + * } | |
31 | + */ | |
32 | +export class ComponentTestHelper { | |
33 | + | |
34 | + mockComponent: any; | |
35 | + tcb: TestComponentBuilder; | |
36 | + component: any; | |
2 |
|
72 | 72 | }; |
73 | 73 | } |
74 | 74 | }, |
75 | + environmentService: { | |
76 | + getEnvironmentPeople: (params: any) => { | |
77 | + return mocks.promiseResultTemplate({ | |
78 | + people: {} | |
1 |
|
1 | 1 | import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder'; |
2 | -import {Provider, Input, provide, Component} from 'ng-forward'; | |
3 | - | |
2 | +import {Provider, provide} from 'ng-forward'; | |
3 | +import {ComponentTestHelper, createClass} from './../../../../spec/component-test-helper'; | |
4 | +import {providers} from 'ng-forward/cjs/testing/providers'; | |
1 |
|