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 | 2 | import {Provider, Input, provide, Component} from 'ng-forward'; |
3 | 3 | import {provideFilters} from '../../../../spec/helpers'; |
4 | 4 | import {RecentDocumentsBlockComponent} from './recent-documents-block.component'; |
5 | +import * as helpers from "./../../../../spec/helpers"; | |
5 | 6 | |
6 | 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 | 12 | describe("Recent Documents Block Component", () => { |
12 | 13 | |
13 | 14 | let settingsObj = {}; |
15 | + let article = <noosfero.Article>{ name: "article1" }; | |
14 | 16 | let mockedBlockService = { |
15 | 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 | 22 | let profile = { name: 'profile-name' }; |
20 | 23 | beforeEach(angular.mock.module("templates")); |
21 | 24 | |
... | ... | @@ -28,6 +31,7 @@ describe("Components", () => { |
28 | 31 | new Provider('BlockService', { |
29 | 32 | useValue: mockedBlockService |
30 | 33 | }), |
34 | + new Provider('ArticleService', { useValue: articleService }) | |
31 | 35 | ].concat(provideFilters("truncateFilter", "stripTagsFilter")); |
32 | 36 | } |
33 | 37 | let componentClass: any = null; |
... | ... | @@ -47,7 +51,7 @@ describe("Components", () => { |
47 | 51 | it("get recent documents from the block service", done => { |
48 | 52 | tcb.createAsync(getComponent()).then(fixture => { |
49 | 53 | let recentDocumentsBlock: RecentDocumentsBlockComponent = fixture.debugElement.componentViewChildren[0].componentInstance; |
50 | - expect(recentDocumentsBlock.documents).toEqual([{ name: "article1" }]); | |
54 | + expect(recentDocumentsBlock.documents).toEqual([ article ]); | |
51 | 55 | done(); |
52 | 56 | }); |
53 | 57 | }); |
... | ... | @@ -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 | 1 | import {Component, Inject, Input} from "ng-forward"; |
2 | 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 | 6 | @Component({ |
5 | 7 | selector: "noosfero-recent-documents-block", |
6 | 8 | templateUrl: 'app/layout/blocks/recent-documents/recent-documents-block.html' |
7 | 9 | }) |
8 | -@Inject(BlockService, "$state") | |
10 | +@Inject(BlockService, "$state", ArticleService) | |
9 | 11 | export class RecentDocumentsBlockComponent { |
10 | 12 | |
11 | 13 | @Input() block: any; |
... | ... | @@ -15,7 +17,7 @@ export class RecentDocumentsBlockComponent { |
15 | 17 | documents: any; |
16 | 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 | 22 | ngOnInit() { |
21 | 23 | this.profile = this.owner; |
... | ... | @@ -24,7 +26,14 @@ export class RecentDocumentsBlockComponent { |
24 | 26 | this.documents = content.articles; |
25 | 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 | 38 | openDocument(article: any) { |
30 | 39 | this.$state.go("main.profile.page", { page: article.path, profile: article.profile.identifier }); | ... | ... |