Commit af9fc87e08a4c8da2691da9c135ae3af6f8444a4

Authored by ABNER SILVA DE OLIVEIRA
1 parent 490f391a

refactory of articles watchers

src/app/article/article-default-view-component.spec.ts
@@ -68,13 +68,13 @@ describe("Components", () => { @@ -68,13 +68,13 @@ describe("Components", () => {
68 */ 68 */
69 function doDeleteArticle() { 69 function doDeleteArticle() {
70 // Create a mock for the ArticleService removeArticle method 70 // Create a mock for the ArticleService removeArticle method
71 - spyOn(helper.component.articleService, 'removeArticle').and.callFake(function(param: noosfero.Article) { 71 + spyOn(helper.component.articleService, 'remove').and.callFake(function(param: noosfero.Article) {
72 return { 72 return {
73 catch: () => {} 73 catch: () => {}
74 }; 74 };
75 }); 75 });
76 helper.component.delete(); 76 helper.component.delete();
77 - expect(articleService.removeArticle).toHaveBeenCalled(); 77 + expect(articleService.remove).toHaveBeenCalled();
78 // After the component delete method execution, fire the 78 // After the component delete method execution, fire the
79 // ArticleEvent.removed event 79 // ArticleEvent.removed event
80 simulateRemovedEvent(); 80 simulateRemovedEvent();
@@ -84,7 +84,7 @@ describe("Components", () => { @@ -84,7 +84,7 @@ describe("Components", () => {
84 * Simulate the ArticleService ArticleEvent.removed event 84 * Simulate the ArticleService ArticleEvent.removed event
85 */ 85 */
86 function simulateRemovedEvent() { 86 function simulateRemovedEvent() {
87 - helper.component.articleService["notifyArticleRemovedListeners"](article); 87 + helper.component.articleService["modelRemovedEventEmitter"].next(article);
88 } 88 }
89 }); 89 });
90 90
src/app/article/article-default-view.component.ts
@@ -17,13 +17,13 @@ import {NotificationService} from "./../shared/services/notification.service"; @@ -17,13 +17,13 @@ import {NotificationService} from "./../shared/services/notification.service";
17 selector: 'noosfero-default-article', 17 selector: 'noosfero-default-article',
18 templateUrl: 'app/article/article.html' 18 templateUrl: 'app/article/article.html'
19 }) 19 })
20 -@Inject("$state", ArticleService, NotificationService) 20 +@Inject("$state", ArticleService)
21 export class ArticleDefaultViewComponent { 21 export class ArticleDefaultViewComponent {
22 22
23 @Input() article: noosfero.Article; 23 @Input() article: noosfero.Article;
24 @Input() profile: noosfero.Profile; 24 @Input() profile: noosfero.Profile;
25 25
26 - constructor(private $state: ng.ui.IStateService, public articleService: ArticleService, protected notificationService: NotificationService) { 26 + constructor(private $state: ng.ui.IStateService, public articleService: ArticleService) {
27 // Subscribe to the Article Removed Event 27 // Subscribe to the Article Removed Event
28 this.articleService.subscribeToModelRemoved((article: noosfero.Article) => { 28 this.articleService.subscribeToModelRemoved((article: noosfero.Article) => {
29 if (this.article.parent) { 29 if (this.article.parent) {
src/app/layout/blocks/recent-documents/recent-documents-block.component.spec.ts
@@ -15,7 +15,7 @@ describe("Components", () => { @@ -15,7 +15,7 @@ describe("Components", () => {
15 let article = <noosfero.Article>{ name: "article1" }; 15 let article = <noosfero.Article>{ name: "article1" };
16 let mockedBlockService = { 16 let mockedBlockService = {
17 getApiContent: (block: noosfero.Block): any => { 17 getApiContent: (block: noosfero.Block): any => {
18 - return Promise.resolve({ articles: [ article ], headers: (name: string) => { return name; } }); 18 + return Promise.resolve({ articles: [article], headers: (name: string) => { return name; } });
19 } 19 }
20 }; 20 };
21 let articleService: any = helpers.mocks.articleService; 21 let articleService: any = helpers.mocks.articleService;
@@ -51,7 +51,7 @@ describe(&quot;Components&quot;, () =&gt; { @@ -51,7 +51,7 @@ describe(&quot;Components&quot;, () =&gt; {
51 it("get recent documents from the block service", done => { 51 it("get recent documents from the block service", done => {
52 tcb.createAsync(getComponent()).then(fixture => { 52 tcb.createAsync(getComponent()).then(fixture => {
53 let recentDocumentsBlock: RecentDocumentsBlockComponent = fixture.debugElement.componentViewChildren[0].componentInstance; 53 let recentDocumentsBlock: RecentDocumentsBlockComponent = fixture.debugElement.componentViewChildren[0].componentInstance;
54 - expect(recentDocumentsBlock.documents).toEqual([ article ]); 54 + expect(recentDocumentsBlock.documents).toEqual([article]);
55 done(); 55 done();
56 }); 56 });
57 }); 57 });
@@ -71,16 +71,16 @@ describe(&quot;Components&quot;, () =&gt; { @@ -71,16 +71,16 @@ describe(&quot;Components&quot;, () =&gt; {
71 expect(recentDocumentsBlock.documents.length).toEqual(1); 71 expect(recentDocumentsBlock.documents.length).toEqual(1);
72 simulateRemovedEvent(recentDocumentsBlock); 72 simulateRemovedEvent(recentDocumentsBlock);
73 expect(recentDocumentsBlock.documents.length).toEqual(0); 73 expect(recentDocumentsBlock.documents.length).toEqual(0);
74 - done();  
75 - }) 74 + done();
  75 + });
76 }); 76 });
77 - 77 +
78 /** 78 /**
79 * Simulate the ArticleService ArticleEvent.removed event 79 * Simulate the ArticleService ArticleEvent.removed event
80 */ 80 */
81 function simulateRemovedEvent(recentDocumentsBlock: RecentDocumentsBlockComponent) { 81 function simulateRemovedEvent(recentDocumentsBlock: RecentDocumentsBlockComponent) {
82 - recentDocumentsBlock.articleService["notifyArticleRemovedListeners"](article); 82 + recentDocumentsBlock.articleService["modelRemovedEventEmitter"].next(article);
83 } 83 }
84 - 84 +
85 }); 85 });
86 }); 86 });
src/app/layout/blocks/recent-documents/recent-documents-block.component.ts
@@ -30,7 +30,7 @@ export class RecentDocumentsBlockComponent { @@ -30,7 +30,7 @@ export class RecentDocumentsBlockComponent {
30 } 30 }
31 31
32 watchArticles() { 32 watchArticles() {
33 - this.articleService.subscribeToArticleRemoved((article: noosfero.Article) => { 33 + this.articleService.subscribeToModelRemoved((article: noosfero.Article) => {
34 Arrays.remove(this.documents, article); 34 Arrays.remove(this.documents, article);
35 }); 35 });
36 } 36 }
src/app/layout/blocks/statistics/statistics-block.component.spec.ts
  1 +import {provide} from 'ng-forward';
1 import {ComponentTestHelper, createClass} from './../../../../spec/component-test-helper'; 2 import {ComponentTestHelper, createClass} from './../../../../spec/component-test-helper';
2 import {StatisticsBlockComponent} from './statistics-block.component'; 3 import {StatisticsBlockComponent} from './statistics-block.component';
3 import * as helpers from "../../../../spec/helpers"; 4 import * as helpers from "../../../../spec/helpers";
4 5
5 const htmlTemplate: string = '<noosfero-statistics-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-statistics-block>'; 6 const htmlTemplate: string = '<noosfero-statistics-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-statistics-block>';
6 7
  8 +
7 describe("Components", () => { 9 describe("Components", () => {
8 10
9 describe("Statistics Block Component", () => { 11 describe("Statistics Block Component", () => {
@@ -11,10 +13,15 @@ describe(&quot;Components&quot;, () =&gt; { @@ -11,10 +13,15 @@ describe(&quot;Components&quot;, () =&gt; {
11 beforeEach(angular.mock.module("templates")); 13 beforeEach(angular.mock.module("templates"));
12 14
13 beforeEach((done) => { 15 beforeEach((done) => {
  16 + let articleService: any = helpers.mocks.articleService;
  17 + let blockService: any = jasmine.createSpyObj("blockService", ["getBlock"]);
14 let cls = createClass({ 18 let cls = createClass({
15 template: htmlTemplate, 19 template: htmlTemplate,
16 directives: [StatisticsBlockComponent], 20 directives: [StatisticsBlockComponent],
17 - providers: helpers.provideFilters("translateFilter"), 21 + providers: [
  22 + provide('ArticleService', { useValue: articleService }),
  23 + provide('BlockService', { useValue: blockService })
  24 + ].concat(helpers.provideFilters("translateFilter")),
18 properties: { 25 properties: {
19 block: { 26 block: {
20 statistics: [ 27 statistics: [
src/lib/ng-noosfero-api/http/article.service.spec.ts
@@ -23,7 +23,7 @@ describe(&quot;Services&quot;, () =&gt; { @@ -23,7 +23,7 @@ describe(&quot;Services&quot;, () =&gt; {
23 it("should remove article", (done) => { 23 it("should remove article", (done) => {
24 let articleId = 1; 24 let articleId = 1;
25 $httpBackend.expectDELETE(`/api/v1/articles/${articleId}`).respond(200, { success: "true" }); 25 $httpBackend.expectDELETE(`/api/v1/articles/${articleId}`).respond(200, { success: "true" });
26 - articleService.removeArticle(<noosfero.Article>{id: articleId}); 26 + articleService.remove(<noosfero.Article>{id: articleId});
27 $httpBackend.flush(); 27 $httpBackend.flush();
28 $httpBackend.verifyNoOutstandingExpectation(); 28 $httpBackend.verifyNoOutstandingExpectation();
29 done(); 29 done();
src/lib/util/arrays.ts
1 export class Arrays { 1 export class Arrays {
2 -  
3 static remove<T extends noosfero.RestModel>(elements: T[], element: T) { 2 static remove<T extends noosfero.RestModel>(elements: T[], element: T) {
4 elements.forEach((value: T, index: number, array: T[]) => { 3 elements.forEach((value: T, index: number, array: T[]) => {
5 - if (value.id == element.id) { 4 + if (value.id === element.id) {
6 array.splice(index, 1); 5 array.splice(index, 1);
7 } 6 }
8 }); 7 });
src/plugins/comment_paragraph/block/discussion/discussion-block.component.spec.ts
@@ -17,7 +17,7 @@ describe(&quot;Components&quot;, () =&gt; { @@ -17,7 +17,7 @@ describe(&quot;Components&quot;, () =&gt; {
17 let article = <noosfero.Article>{ name: "article1" }; 17 let article = <noosfero.Article>{ name: "article1" };
18 let mockedBlockService = { 18 let mockedBlockService = {
19 getApiContent: (content: any): any => { 19 getApiContent: (content: any): any => {
20 - return Promise.resolve({ articles: [ article ], headers: (name: string) => { return name; } }); 20 + return Promise.resolve({ articles: [article], headers: (name: string) => { return name; } });
21 } 21 }
22 }; 22 };
23 let articleService: any = helpers.mocks.articleService; 23 let articleService: any = helpers.mocks.articleService;
@@ -55,7 +55,7 @@ describe(&quot;Components&quot;, () =&gt; { @@ -55,7 +55,7 @@ describe(&quot;Components&quot;, () =&gt; {
55 block.openDocument({ path: "path", profile: { identifier: "identifier" } }); 55 block.openDocument({ path: "path", profile: { identifier: "identifier" } });
56 expect(state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "identifier" }); 56 expect(state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "identifier" });
57 }); 57 });
58 - 58 +
59 it("verify removed article has been removed from list", () => { 59 it("verify removed article has been removed from list", () => {
60 expect(helper.component.documents.length).toEqual(1); 60 expect(helper.component.documents.length).toEqual(1);
61 simulateRemovedEvent(); 61 simulateRemovedEvent();
@@ -65,7 +65,7 @@ describe(&quot;Components&quot;, () =&gt; { @@ -65,7 +65,7 @@ describe(&quot;Components&quot;, () =&gt; {
65 * Simulate the ArticleService ArticleEvent.removed event 65 * Simulate the ArticleService ArticleEvent.removed event
66 */ 66 */
67 function simulateRemovedEvent() { 67 function simulateRemovedEvent() {
68 - helper.component.articleService["notifyArticleRemovedListeners"](article);  
69 - } 68 + helper.component.articleService["modelRemovedEventEmitter"].next(article);
  69 + }
70 }); 70 });
71 }); 71 });
src/plugins/comment_paragraph/block/discussion/discussion-block.component.ts
1 import {Component, Inject, Input} from "ng-forward"; 1 import {Component, Inject, Input} from "ng-forward";
2 import {BlockService} from "../../../../lib/ng-noosfero-api/http/block.service"; 2 import {BlockService} from "../../../../lib/ng-noosfero-api/http/block.service";
3 -import {ArticleService} from "./../../../../lib/ng-noosfero-api/http/article.service"  
4 -import {Arrays} from "./../../../../lib/util/arrays" 3 +import {ArticleService} from "./../../../../lib/ng-noosfero-api/http/article.service";
  4 +import {Arrays} from "./../../../../lib/util/arrays";
5 5
6 @Component({ 6 @Component({
7 selector: "noosfero-comment-paragraph-plugin-discussion-block", 7 selector: "noosfero-comment-paragraph-plugin-discussion-block",
@@ -26,9 +26,9 @@ export class DiscussionBlockComponent { @@ -26,9 +26,9 @@ export class DiscussionBlockComponent {
26 }); 26 });
27 this.watchArticles(); 27 this.watchArticles();
28 } 28 }
29 - 29 +
30 watchArticles() { 30 watchArticles() {
31 - this.articleService.subscribeToArticleRemoved((article: noosfero.Article) => { 31 + this.articleService.subscribeToModelRemoved((article: noosfero.Article) => {
32 Arrays.remove(this.documents, article); 32 Arrays.remove(this.documents, article);
33 }); 33 });
34 } 34 }
src/spec/mocks.ts
@@ -77,10 +77,14 @@ export var mocks: any = { @@ -77,10 +77,14 @@ export var mocks: any = {
77 }, 77 },
78 articleService: { 78 articleService: {
79 articleRemovedFn: null, 79 articleRemovedFn: null,
80 - subscribeToArticleRemoved: (fn: Function) => { 80 + articleAddedFn: null,
  81 + subscribeToModelRemoved: (fn: Function) => {
81 mocks.articleService.articleRemovedFn = fn; 82 mocks.articleService.articleRemovedFn = fn;
82 }, 83 },
83 - articleRemoved: 84 + subscribeToModelAdded: (fn: Function) => {
  85 + mocks.articleService.articleAddedFn = fn;
  86 + },
  87 + modelRemovedEventEmitter:
84 { 88 {
85 subscribe: (fn: Function) => { 89 subscribe: (fn: Function) => {
86 mocks.articleService.articleRemovedFn = fn; 90 mocks.articleService.articleRemovedFn = fn;
@@ -90,18 +94,22 @@ export var mocks: any = { @@ -90,18 +94,22 @@ export var mocks: any = {
90 } 94 }
91 } 95 }
92 , 96 ,
93 - removeArticle: (article: noosfero.Article) => { 97 + modelAddedEventEmitter:
  98 + {
  99 + subscribe: (fn: Function) => {
  100 + mocks.articleService.articleAddedFn = fn;
  101 + },
  102 + next: (param: any) => {
  103 + mocks.articleService.articleAddedFn(param);
  104 + }
  105 + }
  106 + ,
  107 + remove: (article: noosfero.Article) => {
94 return { 108 return {
95 catch: (func?: Function) => { 109 catch: (func?: Function) => {
96 } 110 }
97 }; 111 };
98 }, 112 },
99 - notifyArticleRemovedListeners: (article: noosfero.Article) => {  
100 - mocks.articleService.articleRemoved.next(article);  
101 - },  
102 - subscribe: (eventType: any, fn: Function) => {  
103 - mocks.articleService.articleRemoved.subscribe(fn);  
104 - },  
105 getByProfile: (profileId: number, params?: any) => { 113 getByProfile: (profileId: number, params?: any) => {
106 return { 114 return {
107 then: (func?: Function) => { 115 then: (func?: Function) => {