Merge Request #5

Merged
noosfero-themes/angular-theme!5
Created by Carlos Purificação

People block refactoring and component-test-helper

Refactored people-block and members-block to use the newly created component-test-helper

Assignee: Michel Felipe
Milestone: 2016.04

Merged by Michel Felipe

Source branch has been removed
Commits (3)
2 participants
  • Me
    Michel Felipe @mfdeveloper

    Reassigned to @mfdeveloper

    Choose File ...   File name...
    Cancel
  • Me
    Michel Felipe @mfdeveloper

    @carloseugenio Please, like us face-to-face conversation, release a git rebase in your local development environment. After that, perform the git push -f to force update the people-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 :)

    Choose File ...   File name...
    Cancel
  • 2c5c4299d62769e3da7d432cd2823dd6?s=40&d=identicon
    Carlos Purificação @carloseugenio

    Added 3 new commits:

    • 556724ef - Fixed home link
    • efa64a13 - Fix helpers.ts type problems and added conditional to show profile custom-fields table
    • 6d10324a - People block refactoring. Created component-test-helper
    Choose File ...   File name...
    Cancel
  • Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Michel Felipe
    src/spec/component-test-helper.ts 0 → 100644
      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
    • Me
      Michel Felipe @mfdeveloper (Edited )

      Please, remove the unused rebuild() global function. The feature to redo the tcb.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

      Choose File ...   File name...
      Cancel
    Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Michel Felipe
    src/spec/component-test-helper.ts 0 → 100644
      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
    • Me
      Michel Felipe @mfdeveloper (Edited )

      In mockComponent param, replace the any type to ngClass. For recognize this type into this file, add the import {ngClass} from "...test-component-builder.ts" together with the others import declarations.

      Replace too done param type to Function

      Choose File ...   File name...
      Cancel
    Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Michel Felipe
    src/spec/component-test-helper.ts 0 → 100644
      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
    Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Michel Felipe
    src/spec/component-test-helper.ts 0 → 100644
      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
    Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Carlos Purificação
    src/spec/component-test-helper.ts 0 → 100644
      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
    Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Carlos Purificação
    src/app/layout/blocks/people-block/people-block.component.spec.ts
      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
    • Me
      Michel Felipe @mfdeveloper

      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!

      Choose File ...   File name...
      Cancel
    • 2c5c4299d62769e3da7d432cd2823dd6?s=40&d=identicon
      Carlos Purificação @carloseugenio

      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.

      Choose File ...   File name...
      Cancel
    Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Michel Felipe
    src/spec/component-test-helper.ts 0 → 100644
      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
    • Me
      Michel Felipe @mfdeveloper

      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 the quickCreateComponent() implementation on helpers.ts file :)

      Choose File ...   File name...
      Cancel
    Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Michel Felipe
    src/spec/component-test-helper.ts 0 → 100644
      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
    • Me
      Michel Felipe @mfdeveloper (Edited )

      For create this usage example, Can you use your solution for angular classes docs?

      Choose File ...   File name...
      Cancel
    Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Carlos Purificação
    src/spec/component-test-helper.ts 0 → 100644
      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
    Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Michel Felipe
    src/spec/mocks.ts
    72 72 };
    73 73 }
    74 74 },
      75 + environmentService: {
      76 + getEnvironmentPeople: (params: any) => {
      77 + return mocks.promiseResultTemplate({
      78 + people: {}
    1
    • Me
      Michel Felipe @mfdeveloper

      Can you use the params value into people on promise, instead a empty object({}) ?

      Choose File ...   File name...
      Cancel
    Me
    Michel Felipe started a discussion on commit 6d10324a
    last updated by Michel Felipe
    src/app/layout/blocks/people-block/people-block.component.spec.ts
    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
  • Me
    Michel Felipe @mfdeveloper

    Reassigned to @carloseugenio

    Choose File ...   File name...
    Cancel
  • 2c5c4299d62769e3da7d432cd2823dd6?s=40&d=identicon
    Carlos Purificação @carloseugenio

    Added 1 new commit:

    Choose File ...   File name...
    Cancel
  • Me
    Michel Felipe @mfdeveloper

    Reassigned to @mfdeveloper

    Choose File ...   File name...
    Cancel
  • Me
    Michel Felipe @mfdeveloper
    Choose File ...   File name...
    Cancel