From aa1e3108d6e28ba36656d85c2c6cdb7f01aa719f Mon Sep 17 00:00:00 2001 From: Carlos Purificacao Date: Tue, 24 May 2016 09:26:42 -0300 Subject: [PATCH] Added removed listener to DiscussionBlockComponent --- src/app/article/types/blog/blog.component.spec.ts | 123 +++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------- src/lib/ng-noosfero-api/http/article.service.ts | 2 -- src/lib/util/arrays.ts | 10 ++++++++++ src/plugins/comment_paragraph/block/discussion/discussion-block.component.spec.ts | 18 +++++++++++++++++- src/plugins/comment_paragraph/block/discussion/discussion-block.component.ts | 13 +++++++++++-- 5 files changed, 79 insertions(+), 87 deletions(-) create mode 100644 src/lib/util/arrays.ts diff --git a/src/app/article/types/blog/blog.component.spec.ts b/src/app/article/types/blog/blog.component.spec.ts index ca7f5da..5d617e8 100644 --- a/src/app/article/types/blog/blog.component.spec.ts +++ b/src/app/article/types/blog/blog.component.spec.ts @@ -1,22 +1,11 @@ -import { -providers -} from 'ng-forward/cjs/testing/providers'; - -import { -Input, -Component -} from 'ng-forward'; -import { -ArticleBlogComponent -} from './blog.component'; - -import { -createComponentFromClass, -quickCreateComponent, -provideEmptyObjects, -createProviderToValue, -provideFilters -} from "../../../../spec/helpers.ts"; +import {providers} from 'ng-forward/cjs/testing/providers'; + +import {Input, provide, Component} from 'ng-forward'; +import {ArticleBlogComponent} from './blog.component'; + +import {createComponentFromClass, quickCreateComponent, provideEmptyObjects, createProviderToValue, provideFilters} from "../../../../spec/helpers.ts"; + +import {ComponentTestHelper, createClass} from './../../../../spec/component-test-helper'; // this htmlTemplate will be re-used between the container components in this spec file const htmlTemplate: string = ''; @@ -42,38 +31,30 @@ describe("Blog Component", () => { } } + let article1 = { + id: 1, + title: 'The article test' + }; + + let article2 = { + id: 1, + title: 'The article test' + }; + + let articles = [ article1, article2 ]; + let articleService = { getChildren: (article_id: number, filters: {}) => { return promiseResultTemplate(null); - } + }, + subscribeToArticleRemoved: (fn: Function) => {} }; - @Component({ - selector: 'test-container-component', - template: htmlTemplate, - directives: [ArticleBlogComponent], - providers: [ - provideEmptyObjects('Restangular'), - createProviderToValue('ArticleService', articleService), - provideFilters('truncateFilter') - ] - }) - class BlogContainerComponent { - article = { - type: 'anyArticleType' - }; - profile = { - name: 'profile-name' - }; - } + let helper: ComponentTestHelper; - beforeEach(() => { + beforeEach(angular.mock.module("templates")); - // the karma preprocessor html2js transform the templates html into js files which put - // the templates to the templateCache into the module templates - // we need to load the module templates here as the template for the - // component Noosfero ArtileView will be load on our tests - angular.mock.module("templates"); + beforeEach((done) => { providers((provide: any) => { return [ @@ -82,48 +63,26 @@ describe("Blog Component", () => { }) ]; }); - }); - - it("renders the blog content", (done: Function) => { - - createComponentFromClass(BlogContainerComponent).then((fixture) => { - - expect(fixture.debugElement.query('div.blog').length).toEqual(1); - - done(); + let providersHelper = [ + provide('ArticleService', { useValue: articleService }) + ]; + let cls = createClass({ + template: htmlTemplate, + directives: [ArticleBlogComponent], + providers: providersHelper, + properties: { + posts: articles + } }); + helper = new ComponentTestHelper(cls, done); }); - it("verify the blog data", (done: Function) => { - - let articles = [{ - id: 1, - title: 'The article test' - }]; - - let result = { data: articles, headers: (name: string) => { return 1; } }; - - // defining a mock result to articleService.getChildren method - articleService.getChildren = (article_id: number, filters: {}) => { - return promiseResultTemplate(result); - }; - - createComponentFromClass(BlogContainerComponent).then((fixture) => { - - // gets the children component of BlogContainerComponent - let articleBlog: BlogContainerComponent = fixture.debugElement.componentViewChildren[0].componentInstance; - - // check if the component property are the provided by the mocked articleService - let post = { - id: 1, - title: 'The article test' - }; - expect((articleBlog)["posts"][0]).toEqual(jasmine.objectContaining(post)); - expect((articleBlog)["totalPosts"]).toEqual(1); - - done(); - }); + it("renders the blog content", () => { + expect(helper.debugElement.query('div.blog').length).toEqual(1); + }); + it("verify the blog data", () => { + expect(helper.component["posts"][0]).toEqual(jasmine.objectContaining(article1)); }); }); \ No newline at end of file diff --git a/src/lib/ng-noosfero-api/http/article.service.ts b/src/lib/ng-noosfero-api/http/article.service.ts index b9c38f4..c752687 100644 --- a/src/lib/ng-noosfero-api/http/article.service.ts +++ b/src/lib/ng-noosfero-api/http/article.service.ts @@ -37,8 +37,6 @@ export class ArticleService extends RestangularService { * Notify listeners that this article has been removed */ private notifyArticleRemovedListeners(article: noosfero.Article) { - // let listener = this.events.get(this.removed); - // listener.next(article); this.articleRemoved.next(article); } diff --git a/src/lib/util/arrays.ts b/src/lib/util/arrays.ts new file mode 100644 index 0000000..e8349f1 --- /dev/null +++ b/src/lib/util/arrays.ts @@ -0,0 +1,10 @@ +export class Arrays { + + static remove(elements: T[], element: T) { + elements.forEach((value: T, index: number, array: T[]) => { + if (value.id == element.id) { + array.splice(index, 1); + } + }); + } +} \ No newline at end of file diff --git a/src/plugins/comment_paragraph/block/discussion/discussion-block.component.spec.ts b/src/plugins/comment_paragraph/block/discussion/discussion-block.component.spec.ts index b9e9299..63e9ef8 100644 --- a/src/plugins/comment_paragraph/block/discussion/discussion-block.component.spec.ts +++ b/src/plugins/comment_paragraph/block/discussion/discussion-block.component.spec.ts @@ -3,6 +3,7 @@ import {Provider, Input, provide, Component} from 'ng-forward'; import {provideFilters} from '../../../../spec/helpers'; import {DiscussionBlockComponent} from './discussion-block.component'; import {ComponentTestHelper, createClass} from './../../../../spec/component-test-helper'; +import * as helpers from "./../../../../spec/helpers"; const htmlTemplate: string = ''; @@ -13,11 +14,13 @@ describe("Components", () => { let helper: ComponentTestHelper; let settingsObj = {}; + let article = { name: "article1" }; let mockedBlockService = { getApiContent: (content: any): any => { - return Promise.resolve({ articles: [{ name: "article1" }], headers: (name: string) => { return name; } }); + return Promise.resolve({ articles: [ article ], headers: (name: string) => { return name; } }); } }; + let articleService: any = helpers.mocks.articleService; let profile = { name: 'profile-name' }; let state = jasmine.createSpyObj("state", ["go"]); @@ -27,6 +30,7 @@ describe("Components", () => { new Provider('BlockService', { useValue: mockedBlockService }), + new Provider('ArticleService', { useValue: articleService }) ].concat(provideFilters("truncateFilter", "stripTagsFilter", "translateFilter", "amDateFormatFilter")); beforeEach(angular.mock.module("templates")); @@ -51,5 +55,17 @@ describe("Components", () => { block.openDocument({ path: "path", profile: { identifier: "identifier" } }); expect(state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "identifier" }); }); + + it("verify removed article has been removed from list", () => { + expect(helper.component.documents.length).toEqual(1); + simulateRemovedEvent(); + expect(helper.component.documents.length).toEqual(0); + }); + /** + * Simulate the ArticleService ArticleEvent.removed event + */ + function simulateRemovedEvent() { + helper.component.articleService["notifyArticleRemovedListeners"](article); + } }); }); diff --git a/src/plugins/comment_paragraph/block/discussion/discussion-block.component.ts b/src/plugins/comment_paragraph/block/discussion/discussion-block.component.ts index 854c9c5..5dd2e6f 100644 --- a/src/plugins/comment_paragraph/block/discussion/discussion-block.component.ts +++ b/src/plugins/comment_paragraph/block/discussion/discussion-block.component.ts @@ -1,11 +1,13 @@ import {Component, Inject, Input} from "ng-forward"; import {BlockService} from "../../../../lib/ng-noosfero-api/http/block.service"; +import {ArticleService} from "./../../../../lib/ng-noosfero-api/http/article.service" +import {Arrays} from "./../../../../lib/util/arrays" @Component({ selector: "noosfero-comment-paragraph-plugin-discussion-block", templateUrl: 'plugins/comment_paragraph/block/discussion/discussion-block.html' }) -@Inject(BlockService, "$state") +@Inject(BlockService, "$state", ArticleService) export class DiscussionBlockComponent { @Input() block: any; @@ -14,7 +16,7 @@ export class DiscussionBlockComponent { profile: noosfero.Profile; documents: Array; - constructor(private blockService: BlockService, private $state: any) { } + constructor(private blockService: BlockService, private $state: any, public articleService: ArticleService) { } ngOnInit() { this.profile = this.owner; @@ -22,6 +24,13 @@ export class DiscussionBlockComponent { this.documents = content.articles; this.block.hide = !this.documents || this.documents.length === 0; }); + this.watchArticles(); + } + + watchArticles() { + this.articleService.subscribeToArticleRemoved((article: noosfero.Article) => { + Arrays.remove(this.documents, article); + }); } openDocument(article: any) { -- libgit2 0.21.2