Commit 4de49a37ba5215c9d4f0541eae6a5fe2e2b6fba5
1 parent
aa1e3108
Exists in
master
and in
18 other branches
Added removed listener to RecentDocuments Block
Showing
2 changed files
with
34 additions
and
4 deletions
Show diff stats
src/app/layout/blocks/recent-documents/recent-documents-block.component.spec.ts
@@ -2,6 +2,7 @@ import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builde | @@ -2,6 +2,7 @@ import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builde | ||
2 | import {Provider, Input, provide, Component} from 'ng-forward'; | 2 | import {Provider, Input, provide, Component} from 'ng-forward'; |
3 | import {provideFilters} from '../../../../spec/helpers'; | 3 | import {provideFilters} from '../../../../spec/helpers'; |
4 | import {RecentDocumentsBlockComponent} from './recent-documents-block.component'; | 4 | import {RecentDocumentsBlockComponent} from './recent-documents-block.component'; |
5 | +import * as helpers from "./../../../../spec/helpers"; | ||
5 | 6 | ||
6 | const htmlTemplate: string = '<noosfero-recent-documents-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-recent-documents-block>'; | 7 | const htmlTemplate: string = '<noosfero-recent-documents-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-recent-documents-block>'; |
7 | 8 | ||
@@ -11,11 +12,13 @@ describe("Components", () => { | @@ -11,11 +12,13 @@ describe("Components", () => { | ||
11 | describe("Recent Documents Block Component", () => { | 12 | describe("Recent Documents Block Component", () => { |
12 | 13 | ||
13 | let settingsObj = {}; | 14 | let settingsObj = {}; |
15 | + let article = <noosfero.Article>{ name: "article1" }; | ||
14 | let mockedBlockService = { | 16 | let mockedBlockService = { |
15 | getApiContent: (block: noosfero.Block): any => { | 17 | getApiContent: (block: noosfero.Block): any => { |
16 | - return Promise.resolve({ articles: [{ name: "article1" }], headers: (name: string) => { return name; } }); | 18 | + return Promise.resolve({ articles: [ article ], headers: (name: string) => { return name; } }); |
17 | } | 19 | } |
18 | }; | 20 | }; |
21 | + let articleService: any = helpers.mocks.articleService; | ||
19 | let profile = { name: 'profile-name' }; | 22 | let profile = { name: 'profile-name' }; |
20 | beforeEach(angular.mock.module("templates")); | 23 | beforeEach(angular.mock.module("templates")); |
21 | 24 | ||
@@ -28,6 +31,7 @@ describe("Components", () => { | @@ -28,6 +31,7 @@ describe("Components", () => { | ||
28 | new Provider('BlockService', { | 31 | new Provider('BlockService', { |
29 | useValue: mockedBlockService | 32 | useValue: mockedBlockService |
30 | }), | 33 | }), |
34 | + new Provider('ArticleService', { useValue: articleService }) | ||
31 | ].concat(provideFilters("truncateFilter", "stripTagsFilter")); | 35 | ].concat(provideFilters("truncateFilter", "stripTagsFilter")); |
32 | } | 36 | } |
33 | let componentClass: any = null; | 37 | let componentClass: any = null; |
@@ -47,7 +51,7 @@ describe("Components", () => { | @@ -47,7 +51,7 @@ describe("Components", () => { | ||
47 | it("get recent documents from the block service", done => { | 51 | it("get recent documents from the block service", done => { |
48 | tcb.createAsync(getComponent()).then(fixture => { | 52 | tcb.createAsync(getComponent()).then(fixture => { |
49 | let recentDocumentsBlock: RecentDocumentsBlockComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | 53 | let recentDocumentsBlock: RecentDocumentsBlockComponent = fixture.debugElement.componentViewChildren[0].componentInstance; |
50 | - expect(recentDocumentsBlock.documents).toEqual([{ name: "article1" }]); | 54 | + expect(recentDocumentsBlock.documents).toEqual([ article ]); |
51 | done(); | 55 | done(); |
52 | }); | 56 | }); |
53 | }); | 57 | }); |
@@ -61,5 +65,22 @@ describe("Components", () => { | @@ -61,5 +65,22 @@ describe("Components", () => { | ||
61 | }); | 65 | }); |
62 | }); | 66 | }); |
63 | 67 | ||
68 | + it("verify removed article has been removed from list", done => { | ||
69 | + tcb.createAsync(getComponent()).then(fixture => { | ||
70 | + let recentDocumentsBlock: RecentDocumentsBlockComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | ||
71 | + expect(recentDocumentsBlock.documents.length).toEqual(1); | ||
72 | + simulateRemovedEvent(recentDocumentsBlock); | ||
73 | + expect(recentDocumentsBlock.documents.length).toEqual(0); | ||
74 | + done(); | ||
75 | + }) | ||
76 | + }); | ||
77 | + | ||
78 | + /** | ||
79 | + * Simulate the ArticleService ArticleEvent.removed event | ||
80 | + */ | ||
81 | + function simulateRemovedEvent(recentDocumentsBlock: RecentDocumentsBlockComponent) { | ||
82 | + recentDocumentsBlock.articleService["notifyArticleRemovedListeners"](article); | ||
83 | + } | ||
84 | + | ||
64 | }); | 85 | }); |
65 | }); | 86 | }); |
src/app/layout/blocks/recent-documents/recent-documents-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 | 5 | ||
4 | @Component({ | 6 | @Component({ |
5 | selector: "noosfero-recent-documents-block", | 7 | selector: "noosfero-recent-documents-block", |
6 | templateUrl: 'app/layout/blocks/recent-documents/recent-documents-block.html' | 8 | templateUrl: 'app/layout/blocks/recent-documents/recent-documents-block.html' |
7 | }) | 9 | }) |
8 | -@Inject(BlockService, "$state") | 10 | +@Inject(BlockService, "$state", ArticleService) |
9 | export class RecentDocumentsBlockComponent { | 11 | export class RecentDocumentsBlockComponent { |
10 | 12 | ||
11 | @Input() block: any; | 13 | @Input() block: any; |
@@ -15,7 +17,7 @@ export class RecentDocumentsBlockComponent { | @@ -15,7 +17,7 @@ export class RecentDocumentsBlockComponent { | ||
15 | documents: any; | 17 | documents: any; |
16 | documentsLoaded: boolean = false; | 18 | documentsLoaded: boolean = false; |
17 | 19 | ||
18 | - constructor(private blockService: BlockService, private $state: any) { } | 20 | + constructor(private blockService: BlockService, private $state: any, public articleService: ArticleService) { } |
19 | 21 | ||
20 | ngOnInit() { | 22 | ngOnInit() { |
21 | this.profile = this.owner; | 23 | this.profile = this.owner; |
@@ -24,7 +26,14 @@ export class RecentDocumentsBlockComponent { | @@ -24,7 +26,14 @@ export class RecentDocumentsBlockComponent { | ||
24 | this.documents = content.articles; | 26 | this.documents = content.articles; |
25 | this.documentsLoaded = true; | 27 | this.documentsLoaded = true; |
26 | }); | 28 | }); |
29 | + this.watchArticles(); | ||
27 | } | 30 | } |
31 | + | ||
32 | + watchArticles() { | ||
33 | + this.articleService.subscribeToArticleRemoved((article: noosfero.Article) => { | ||
34 | + Arrays.remove(this.documents, article); | ||
35 | + }); | ||
36 | + } | ||
28 | 37 | ||
29 | openDocument(article: any) { | 38 | openDocument(article: any) { |
30 | this.$state.go("main.profile.page", { page: article.path, profile: article.profile.identifier }); | 39 | this.$state.go("main.profile.page", { page: article.path, profile: article.profile.identifier }); |