diff --git a/src/app/article/comment/comments.component.spec.ts b/src/app/article/comment/comments.component.spec.ts index 2c33512..771ed50 100644 --- a/src/app/article/comment/comments.component.spec.ts +++ b/src/app/article/comment/comments.component.spec.ts @@ -12,6 +12,7 @@ describe("Components", () => { beforeEach(angular.mock.module("templates")); let commentService = jasmine.createSpyObj("commentService", ["getByArticle"]); + commentService.onSave = helpers.mocks.commentService.onSave; let comments = [{ id: 2 }, { id: 3 }]; commentService.getByArticle = jasmine.createSpy("getByArticle") @@ -19,7 +20,7 @@ describe("Components", () => { let properties = { article: { id: 1 }, parent: null }; function createComponent() { - // postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["subscribe"]); + let providers = [ helpers.createProviderToValue('CommentService', commentService), helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), @@ -52,7 +53,9 @@ describe("Components", () => { it("update comments list when receive an reply", done => { properties.parent = { id: 3 }; createComponent().then(fixture => { - (fixture.debugElement.componentViewChildren[0].componentInstance).commentAdded({ id: 1, reply_of: { id: 3 } }); + let component = (fixture.debugElement.componentViewChildren[0].componentInstance); + component.commentService.onSave.next.call(component, { id: 1, reply_of: { id: 3 } }); + console.log('Comments: ' + component.comments.length); fixture.detectChanges(); expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(3); done(); diff --git a/src/app/article/comment/comments.component.ts b/src/app/article/comment/comments.component.ts index 7da9533..c1153f9 100644 --- a/src/app/article/comment/comments.component.ts +++ b/src/app/article/comment/comments.component.ts @@ -6,8 +6,7 @@ import { CommentComponent } from "./comment.component"; @Component({ selector: 'noosfero-comments', templateUrl: 'app/article/comment/comments.html', - directives: [PostCommentComponent, CommentComponent], - outputs: ['commentAdded'] + directives: [PostCommentComponent, CommentComponent] }) @Inject(CommentService, "$element") export class CommentsComponent { @@ -22,7 +21,7 @@ export class CommentsComponent { newComment = {}; - constructor(protected commentService: CommentService, private $scope: ng.IScope) { } + constructor(public commentService: CommentService, private $scope: ng.IScope) { } ngOnInit() { if (this.parent) { @@ -30,11 +29,11 @@ export class CommentsComponent { } else { this.loadNextPage(); } - } - commentAdded(comment: noosfero.Comment): void { - this.comments.push(comment); - this.resetShowReply(); + this.commentService.onSave.subscribe((comment: noosfero.Comment) => { + this.comments.push(comment); + this.resetShowReply(); + }); } private resetShowReply() { diff --git a/src/app/article/comment/comments.html b/src/app/article/comment/comments.html index cf08d05..24fcb02 100644 --- a/src/app/article/comment/comments.html +++ b/src/app/article/comment/comments.html @@ -1,6 +1,6 @@
- - + +
diff --git a/src/app/article/comment/post-comment/post-comment.component.spec.ts b/src/app/article/comment/post-comment/post-comment.component.spec.ts index 73ed3e4..634b280 100644 --- a/src/app/article/comment/post-comment/post-comment.component.spec.ts +++ b/src/app/article/comment/post-comment/post-comment.component.spec.ts @@ -12,6 +12,7 @@ describe("Components", () => { beforeEach(angular.mock.module("templates")); let commentService = jasmine.createSpyObj("commentService", ["createInArticle"]); + commentService.onSave = jasmine.createSpyObj("onSave", ["subscribe", "next"]); let user = {}; let providers = [ new Provider('CommentService', { useValue: commentService }), @@ -43,10 +44,9 @@ describe("Components", () => { it("emit an event when create comment", done => { helpers.createComponentFromClass(ContainerComponent).then(fixture => { let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; - component.commentSaved.next = jasmine.createSpy("next"); commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); component.save(); - expect(component.commentSaved.next).toHaveBeenCalled(); + expect(component.commentService.onSave.next).toHaveBeenCalled(); done(); }); }); diff --git a/src/app/article/comment/post-comment/post-comment.component.ts b/src/app/article/comment/post-comment/post-comment.component.ts index db42377..8392122 100644 --- a/src/app/article/comment/post-comment/post-comment.component.ts +++ b/src/app/article/comment/post-comment/post-comment.component.ts @@ -17,11 +17,11 @@ export class PostCommentComponent { @Input() article: noosfero.Article; @Input() parent: noosfero.Comment; - @Output() commentSaved: EventEmitter = new EventEmitter(); @Input() comment = {}; private currentUser: noosfero.User; - constructor(private commentService: CommentService, + constructor( + public commentService: CommentService, private notificationService: NotificationService, private session: SessionService) { this.currentUser = this.session.currentUser(); @@ -32,7 +32,8 @@ export class PostCommentComponent { this.comment.reply_of_id = this.parent.id; } this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult) => { - this.commentSaved.next(result.data); + + this.commentService.onSave.next(result.data); this.comment.body = ""; this.notificationService.success({ title: "comment.post.success.title", message: "comment.post.success.message" }); }); diff --git a/src/lib/ng-noosfero-api/http/comment.service.ts b/src/lib/ng-noosfero-api/http/comment.service.ts index 62e7146..e051007 100644 --- a/src/lib/ng-noosfero-api/http/comment.service.ts +++ b/src/lib/ng-noosfero-api/http/comment.service.ts @@ -1,4 +1,4 @@ -import { Injectable, Inject } from "ng-forward"; +import { Injectable, Inject, EventEmitter } from "ng-forward"; import {RestangularService} from "./restangular_service"; import {ArticleService} from "./article.service"; @@ -6,6 +6,8 @@ import {ArticleService} from "./article.service"; @Inject("Restangular", "$q", "$log", ArticleService) export class CommentService extends RestangularService { + public onSave: EventEmitter = new EventEmitter(); + constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService, protected articleService: ArticleService) { super(Restangular, $q, $log); } diff --git a/src/plugins/comment_paragraph/allow-comment/allow-comment.component.spec.ts b/src/plugins/comment_paragraph/allow-comment/allow-comment.component.spec.ts index a394338..ffd4a50 100644 --- a/src/plugins/comment_paragraph/allow-comment/allow-comment.component.spec.ts +++ b/src/plugins/comment_paragraph/allow-comment/allow-comment.component.spec.ts @@ -24,7 +24,8 @@ describe("Components", () => { let providers = [ new Provider('CommentParagraphService', { useValue: serviceMock }), - new Provider('CommentParagraphEventService', { useValue: eventServiceMock }) + new Provider('CommentParagraphEventService', { useValue: eventServiceMock }), + new Provider('CommentService', { useValue: helpers.mocks.commentService }) ]; let helper: ComponentTestHelper; diff --git a/src/plugins/comment_paragraph/allow-comment/allow-comment.component.ts b/src/plugins/comment_paragraph/allow-comment/allow-comment.component.ts index 556d8c3..6ee559b 100644 --- a/src/plugins/comment_paragraph/allow-comment/allow-comment.component.ts +++ b/src/plugins/comment_paragraph/allow-comment/allow-comment.component.ts @@ -1,14 +1,16 @@ -import {Component, Input, Inject} from "ng-forward"; +import {Component, Input, Inject, bundleStore} from "ng-forward"; import {SideCommentsComponent} from "../side-comments/side-comments.component"; import {CommentParagraphEventService} from "../events/comment-paragraph-event.service"; import {CommentParagraphService} from "../http/comment-paragraph.service"; +import {CommentService} from "../../../lib/ng-noosfero-api/http/comment.service"; +import {PostCommentComponent} from '../../../app/article/comment/post-comment/post-comment.component'; @Component({ selector: "comment-paragraph-plugin-allow-comment", templateUrl: "plugins/comment_paragraph/allow-comment/allow-comment.html", directives: [SideCommentsComponent] }) -@Inject("$scope", CommentParagraphEventService, CommentParagraphService) +@Inject("$scope", CommentParagraphEventService, CommentParagraphService, CommentService) export class AllowCommentComponent { @Input() content: string; @@ -19,16 +21,22 @@ export class AllowCommentComponent { constructor(private $scope: ng.IScope, private commentParagraphEventService: CommentParagraphEventService, - private commentParagraphService: CommentParagraphService) { } + private commentParagraphService: CommentParagraphService, + private commentService: CommentService) { } ngOnInit() { this.commentParagraphEventService.subscribeToggleCommentParagraph((article: noosfero.Article) => { this.article = article; this.$scope.$apply(); }); + this.commentParagraphService.commentParagraphCount(this.article, this.paragraphUuid).then((count: number) => { this.commentsCount = count; }); + + this.commentService.onSave.subscribe((comment: noosfero.Comment) => { + this.commentsCount++; + }); } isActivated() { diff --git a/src/plugins/comment_paragraph/side-comments/side-comments.component.spec.ts b/src/plugins/comment_paragraph/side-comments/side-comments.component.spec.ts index 34eed73..f59f460 100644 --- a/src/plugins/comment_paragraph/side-comments/side-comments.component.spec.ts +++ b/src/plugins/comment_paragraph/side-comments/side-comments.component.spec.ts @@ -12,14 +12,9 @@ describe("Components", () => { let serviceMock = jasmine.createSpyObj("CommentParagraphService", ["getByArticle"]); serviceMock.getByArticle = jasmine.createSpy("getByArticle").and.returnValue(Promise.resolve({ data: {} })); - let commentServiceMock = {}; - let postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["emit", "subscribe"]); - postCommentEventService.subscribe = jasmine.createSpy("subscribe"); - let providers = [ new Provider('CommentParagraphService', { useValue: serviceMock }), - new Provider('CommentService', { useValue: commentServiceMock }), - new Provider('PostCommentEventService', { useValue: postCommentEventService }) + new Provider('CommentService', { useValue: helpers.mocks.commentService }), ]; let helper: ComponentTestHelper; diff --git a/src/spec/mocks.ts b/src/spec/mocks.ts index 8be322b..dd8ded7 100644 --- a/src/spec/mocks.ts +++ b/src/spec/mocks.ts @@ -124,6 +124,15 @@ export var mocks: any = { commentService: { getByArticle: (article: noosfero.Article) => { return Promise.resolve({ data: {} }); + }, + onSave: { + event: Function, + subscribe: (fn: Function) => { + mocks.commentService['onSave'].event = fn; + }, + next: (param: any) => { + mocks.commentService['onSave'].event.call(this, param); + } } }, sessionWithCurrentUser: (user: any) => { -- libgit2 0.21.2