Commit 921551c269a2664d4fd39e1fd253fdd0e7f8a272

Authored by Michel Felipe
1 parent cc41ff12

Refactor Blog unit test and implement a new spec

src/app/components/noosfero-articles/blog/blog.component.spec.ts
  1 +import {providers} from 'ng-forward/cjs/testing/providers';
  2 +
1 import { 3 import {
2 Input, 4 Input,
3 - provide,  
4 Component 5 Component
5 } from 'ng-forward'; 6 } from 'ng-forward';
6 import { 7 import {
@@ -12,26 +13,16 @@ import { @@ -12,26 +13,16 @@ import {
12 quickCreateComponent, 13 quickCreateComponent,
13 provideEmptyObjects, 14 provideEmptyObjects,
14 createProviderToValue, 15 createProviderToValue,
15 - getAngularService 16 + getAngularService,
  17 + provideFilters
16 } from "../../../../spec/helpers.ts"; 18 } from "../../../../spec/helpers.ts";
17 19
18 -  
19 // this htmlTemplate will be re-used between the container components in this spec file 20 // this htmlTemplate will be re-used between the container components in this spec file
20 const htmlTemplate: string = '<noosfero-blog [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-blog>'; 21 const htmlTemplate: string = '<noosfero-blog [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-blog>';
21 22
22 -let articleService: {  
23 - getChildren: Function  
24 -} = < any > {};  
25 -  
26 describe("Blog Component", () => { 23 describe("Blog Component", () => {
27 -  
28 - // the karma preprocessor html2js transform the templates html into js files which put  
29 - // the templates to the templateCache into the module templates  
30 - // we need to load the module templates here as the template for the  
31 - // component Noosfero ArtileView will be load on our tests  
32 - beforeEach(angular.mock.module("templates"));  
33 -  
34 - function promiseResultTemplate(response ? : {}) { 24 +
  25 + function promiseResultTemplate(response?: {}) {
35 let thenFuncEmpty = (func: Function) => { 26 let thenFuncEmpty = (func: Function) => {
36 // does nothing 27 // does nothing
37 }; 28 };
@@ -50,48 +41,54 @@ describe(&quot;Blog Component&quot;, () =&gt; { @@ -50,48 +41,54 @@ describe(&quot;Blog Component&quot;, () =&gt; {
50 } 41 }
51 } 42 }
52 43
53 - beforeAll(() => {  
54 - // creating mock for articleService  
55 - articleService = {  
56 - getChildren: (article_id: number, filters: {}) => {  
57 - return promiseResultTemplate(null);  
58 - } 44 + let articleService = {
  45 + getChildren: (article_id: number, filters: {}) => {
  46 + return promiseResultTemplate(null);
  47 + }
  48 + };
  49 +
  50 + @Component({
  51 + selector: 'test-container-component',
  52 + template: htmlTemplate,
  53 + directives: [ArticleBlog],
  54 + providers: [
  55 + provideEmptyObjects('Restangular'),
  56 + createProviderToValue('ArticleService', articleService),
  57 + provideFilters('truncateFilter')
  58 + ]
  59 + })
  60 + class BlogContainerComponent {
  61 + article = {
  62 + type: 'anyArticleType'
  63 + };
  64 + profile = {
  65 + name: 'profile-name'
59 }; 66 };
  67 + }
  68 +
  69 + beforeEach(() => {
  70 +
  71 + // the karma preprocessor html2js transform the templates html into js files which put
  72 + // the templates to the templateCache into the module templates
  73 + // we need to load the module templates here as the template for the
  74 + // component Noosfero ArtileView will be load on our tests
  75 + angular.mock.module("templates")
  76 +
  77 + providers((provide: any) => {
  78 + return <any>[
  79 + provide('ArticleService', { useValue: articleService })
  80 + ]
  81 + });
60 }); 82 });
61 83
62 it("renders the blog content", (done: Function) => { 84 it("renders the blog content", (done: Function) => {
63 85
64 - // Creating a container component (ArticleContainerComponent) to include  
65 - // the component under test (ArticleView)  
66 - @Component({  
67 - selector: 'test-container-component',  
68 - template: htmlTemplate,  
69 - directives: [ArticleBlog],  
70 - providers: [provideEmptyObjects('Restangular'), createProviderToValue('ArticleService', articleService)]  
71 - })  
72 - class BlogContainerComponent {  
73 - article = {  
74 - type: 'anyArticleType'  
75 - };  
76 - profile = {  
77 - name: 'profile-name'  
78 - };  
79 - }  
80 -  
81 createComponentFromClass(BlogContainerComponent).then((fixture) => { 86 createComponentFromClass(BlogContainerComponent).then((fixture) => {
82 87
83 expect(fixture.debugElement.query('div.blog').length).toEqual(1); 88 expect(fixture.debugElement.query('div.blog').length).toEqual(1);
84 89
85 done(); 90 done();
86 }); 91 });
87 -  
88 -  
89 -  
90 - });  
91 -  
92 - it("get $q service", () => {  
93 - let $q = getAngularService<ng.IQService>("$q");  
94 - console.log($q);  
95 }); 92 });
96 93
97 it("verify the blog data", (done: Function) => { 94 it("verify the blog data", (done: Function) => {
@@ -102,44 +99,31 @@ describe(&quot;Blog Component&quot;, () =&gt; { @@ -102,44 +99,31 @@ describe(&quot;Blog Component&quot;, () =&gt; {
102 headers: (headerName: string) => { 99 headers: (headerName: string) => {
103 return 1; 100 return 1;
104 }, 101 },
105 - data: < any > {  
106 - articles: [] 102 + data: <any>{
  103 + articles: [{
  104 + id: 1,
  105 + title: 'The article test'
  106 + }]
107 } 107 }
108 }); 108 });
109 }; 109 };
110 110
111 - @Component({  
112 - selector: 'test-container-component',  
113 - template: htmlTemplate,  
114 - directives: [ArticleBlog],  
115 - providers: [provideEmptyObjects('Restangular'), createProviderToValue('ArticleService', articleService)]  
116 - })  
117 - class BlogContainerComponent {  
118 - article = {  
119 - type: 'anyArticleType'  
120 - };  
121 - profile = {  
122 - name: 'profile-name'  
123 - };  
124 - }  
125 -  
126 createComponentFromClass(BlogContainerComponent).then((fixture) => { 111 createComponentFromClass(BlogContainerComponent).then((fixture) => {
127 112
128 // gets the children component of BlogContainerComponent 113 // gets the children component of BlogContainerComponent
129 let articleBlog: BlogContainerComponent = fixture.debugElement.componentViewChildren[0].componentInstance; 114 let articleBlog: BlogContainerComponent = fixture.debugElement.componentViewChildren[0].componentInstance;
130 115
131 - // check if the component property are the provided by the mocked articleService  
132 - expect(( < any > articleBlog)["posts"]).toEqual([]);  
133 - expect(( < any > articleBlog)["totalPosts"]).toEqual(1);  
134 - 116 + // check if the component property are the provided by the mocked articleService
  117 + var post = {
  118 + id: 1,
  119 + title: 'The article test'
  120 + };
  121 + expect((<any>articleBlog)["posts"][0]).toEqual(jasmine.objectContaining(post));
  122 + expect((<any>articleBlog)["totalPosts"]).toEqual(1);
135 123
136 - // done needs to be called (it isn't really needed, as we can read in  
137 - // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync)  
138 - // because createAsync in ng-forward is not really async, but as the intention  
139 - // here is write tests in angular 2 ways, this is recommended  
140 done(); 124 done();
141 }); 125 });
142 126
143 }); 127 });
144 128
145 -});  
146 \ No newline at end of file 129 \ No newline at end of file
  130 +});