Commit 28a3d3f6351eb73d98692cc434822770ac538898

Authored by ABNER SILVA DE OLIVEIRA
1 parent 8fa37152

added helpe getAngularService to allow get angular services like and

src/app/components/noosfero-articles/blog/blog.component.spec.ts
1   -
2   -import {Input, provide, Component} from 'ng-forward';
3   -import {ArticleBlog} from './blog.component';
4   -
5   -import {createComponentFromClass, quickCreateComponent, provideEmptyObjects, createProviderToValue} from "../../../../spec/helpers.ts";
  1 +import {
  2 + Input,
  3 + provide,
  4 + Component
  5 +} from 'ng-forward';
  6 +import {
  7 + ArticleBlog
  8 +} from './blog.component';
  9 +
  10 +import {
  11 + createComponentFromClass,
  12 + quickCreateComponent,
  13 + provideEmptyObjects,
  14 + createProviderToValue,
  15 + getAngularService
  16 +} from "../../../../spec/helpers.ts";
6 17  
7 18  
8 19 // this htmlTemplate will be re-used between the container components in this spec file
9 20 const htmlTemplate: string = '<noosfero-blog [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-blog>';
10 21  
11   -let articleService: { getChildren: Function } = <any>{};
  22 +let articleService: {
  23 + getChildren: Function
  24 +} = < any > {};
12 25  
13 26 describe("Blog Component", () => {
14 27  
... ... @@ -18,7 +31,7 @@ describe(&quot;Blog Component&quot;, () =&gt; {
18 31 // component Noosfero ArtileView will be load on our tests
19 32 beforeEach(angular.mock.module("templates"));
20 33  
21   - function promiseResultTemplate(response?: {}) {
  34 + function promiseResultTemplate(response ? : {}) {
22 35 let thenFuncEmpty = (func: Function) => {
23 36 // does nothing
24 37 };
... ... @@ -57,8 +70,12 @@ describe(&quot;Blog Component&quot;, () =&gt; {
57 70 providers: [provideEmptyObjects('Restangular'), createProviderToValue('ArticleService', articleService)]
58 71 })
59 72 class BlogContainerComponent {
60   - article = { type: 'anyArticleType' };
61   - profile = { name: 'profile-name' };
  73 + article = {
  74 + type: 'anyArticleType'
  75 + };
  76 + profile = {
  77 + name: 'profile-name'
  78 + };
62 79 }
63 80  
64 81 createComponentFromClass(BlogContainerComponent).then((fixture) => {
... ... @@ -72,18 +89,23 @@ describe(&quot;Blog Component&quot;, () =&gt; {
72 89  
73 90 });
74 91  
  92 + it("get $q service", () => {
  93 + let $q = getAngularService<ng.IQService>("$q");
  94 + console.log($q);
  95 + });
  96 +
75 97 it("verify the blog data", (done: Function) => {
76 98  
77 99 // defining a mock result to articleService.getChildren method
78 100 articleService.getChildren = (article_id: number, filters: {}) => {
79 101 return promiseResultTemplate({
80   - headers: (headerName: string) => {
81   - return 1;
82   - },
83   - data: <any>{
84   - articles: []
85   - }
86   - });
  102 + headers: (headerName: string) => {
  103 + return 1;
  104 + },
  105 + data: < any > {
  106 + articles: []
  107 + }
  108 + });
87 109 };
88 110  
89 111 @Component({
... ... @@ -93,8 +115,12 @@ describe(&quot;Blog Component&quot;, () =&gt; {
93 115 providers: [provideEmptyObjects('Restangular'), createProviderToValue('ArticleService', articleService)]
94 116 })
95 117 class BlogContainerComponent {
96   - article = { type: 'anyArticleType' };
97   - profile = { name: 'profile-name' };
  118 + article = {
  119 + type: 'anyArticleType'
  120 + };
  121 + profile = {
  122 + name: 'profile-name'
  123 + };
98 124 }
99 125  
100 126 createComponentFromClass(BlogContainerComponent).then((fixture) => {
... ... @@ -103,8 +129,8 @@ describe(&quot;Blog Component&quot;, () =&gt; {
103 129 let articleBlog: BlogContainerComponent = fixture.debugElement.componentViewChildren[0].componentInstance;
104 130  
105 131 // check if the component property are the provided by the mocked articleService
106   - expect((<any>articleBlog)["posts"]).toEqual([]);
107   - expect((<any>articleBlog)["totalPosts"]).toEqual(1);
  132 + expect(( < any > articleBlog)["posts"]).toEqual([]);
  133 + expect(( < any > articleBlog)["totalPosts"]).toEqual(1);
108 134  
109 135  
110 136 // done needs to be called (it isn't really needed, as we can read in
... ... @@ -116,4 +142,4 @@ describe(&quot;Blog Component&quot;, () =&gt; {
116 142  
117 143 });
118 144  
119 145 -});
  146 +});
120 147 \ No newline at end of file
... ...
src/spec/helpers.ts
1 1  
2 2 import {ngClass, TestComponentBuilder, ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder';
3   -import {Provider, Input, provide, Component} from 'ng-forward';
  3 +import {Injectable, Inject, Provider, Input, provide, Component} from 'ng-forward';
4 4 import {User, Person} from "./../app/models/interfaces";
5 5  
6 6  
... ... @@ -48,6 +48,28 @@ export function provideFilters(...filters: string[]) {
48 48 return providers;
49 49 }
50 50  
  51 +/**
  52 + * This help function allows get angular services to be used in integration tests
  53 + * i.e: '$http', '$q', '$location', etc...
  54 + */
  55 +export function getAngularService<T>(angularService: string) {
  56 + let tcb: TestComponentBuilder = new TestComponentBuilder();
  57 +
  58 + @Component({
  59 + selector: 'helper_get_angular_service',
  60 + template: 'not-used',
  61 + providers: []
  62 + })
  63 + class AnyService {
  64 + constructor() {
  65 +
  66 + }
  67 + }
  68 +
  69 + let fixture: ComponentFixture = (<any>tcb)["create"](AnyService);
  70 + return fixture.debugElement.getLocal(angularService);
  71 +}
  72 +
51 73 export var fixtures = {
52 74 user: {
53 75 id: 1,
... ...