Commit 3f5563c8199dcbd4716e71f3b997d17d495faece

Authored by Victor Costa
1 parent 88c2b583

Send event when remove a comment to update comments list

src/app/article/comment/comment.component.spec.ts
@@ -8,6 +8,8 @@ describe("Components", () => { @@ -8,6 +8,8 @@ describe("Components", () => {
8 describe("Comment Component", () => { 8 describe("Comment Component", () => {
9 9
10 let properties: any; 10 let properties: any;
  11 + let notificationService = helpers.mocks.notificationService;
  12 + let commentService = jasmine.createSpyObj("commentService", ["removeFromArticle"]);
11 13
12 beforeEach(angular.mock.module("templates")); 14 beforeEach(angular.mock.module("templates"));
13 beforeEach(() => { 15 beforeEach(() => {
@@ -18,9 +20,8 @@ describe("Components", () => { @@ -18,9 +20,8 @@ describe("Components", () => {
18 }); 20 });
19 21
20 function createComponent() { 22 function createComponent() {
21 - let commentService = jasmine.createSpyObj("commentService", ["removeFromArticle"]);  
22 let providers = [ 23 let providers = [
23 - helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), 24 + helpers.createProviderToValue('NotificationService', notificationService),
24 helpers.createProviderToValue("CommentService", commentService) 25 helpers.createProviderToValue("CommentService", commentService)
25 ].concat(helpers.provideFilters("translateFilter")); 26 ].concat(helpers.provideFilters("translateFilter"));
26 27
@@ -78,5 +79,16 @@ describe("Components", () => { @@ -78,5 +79,16 @@ describe("Components", () => {
78 done(); 79 done();
79 }); 80 });
80 }); 81 });
  82 +
  83 + it("call comment service to remove comment", done => {
  84 + notificationService.confirmation = (params: any, func: Function) => { func(); };
  85 + commentService.removeFromArticle = jasmine.createSpy("removeFromArticle").and.returnValue(Promise.resolve());
  86 + createComponent().then(fixture => {
  87 + let component = fixture.debugElement.componentViewChildren[0].componentInstance;
  88 + component.remove();
  89 + expect(commentService.removeFromArticle).toHaveBeenCalled();
  90 + done();
  91 + });
  92 + });
81 }); 93 });
82 }); 94 });
src/app/article/comment/comment.component.ts
@@ -5,6 +5,7 @@ import { NotificationService } from "../../shared/services/notification.service" @@ -5,6 +5,7 @@ import { NotificationService } from "../../shared/services/notification.service"
5 5
6 @Component({ 6 @Component({
7 selector: 'noosfero-comment', 7 selector: 'noosfero-comment',
  8 + outputs: ['commentRemoved'],
8 templateUrl: 'app/article/comment/comment.html' 9 templateUrl: 'app/article/comment/comment.html'
9 }) 10 })
10 @Inject(CommentService, NotificationService) 11 @Inject(CommentService, NotificationService)
@@ -14,6 +15,7 @@ export class CommentComponent { @@ -14,6 +15,7 @@ export class CommentComponent {
14 @Input() article: noosfero.Article; 15 @Input() article: noosfero.Article;
15 @Input() displayActions = true; 16 @Input() displayActions = true;
16 @Input() displayReplies = true; 17 @Input() displayReplies = true;
  18 + @Output() commentRemoved: EventEmitter<Comment> = new EventEmitter<Comment>();
17 19
18 showReply() { 20 showReply() {
19 return this.comment && this.comment.__show_reply === true; 21 return this.comment && this.comment.__show_reply === true;
@@ -33,7 +35,7 @@ export class CommentComponent { @@ -33,7 +35,7 @@ export class CommentComponent {
33 remove() { 35 remove() {
34 this.notificationService.confirmation({ title: "comment.remove.confirmation.title", message: "comment.remove.confirmation.message" }, () => { 36 this.notificationService.confirmation({ title: "comment.remove.confirmation.title", message: "comment.remove.confirmation.message" }, () => {
35 this.commentService.removeFromArticle(this.article, this.comment).then((result: noosfero.RestResult<noosfero.Comment>) => { 37 this.commentService.removeFromArticle(this.article, this.comment).then((result: noosfero.RestResult<noosfero.Comment>) => {
36 - // FIXME send event 38 + this.commentRemoved.next(this.comment);
37 this.notificationService.success({ title: "comment.remove.success.title", message: "comment.remove.success.message" }); 39 this.notificationService.success({ title: "comment.remove.success.title", message: "comment.remove.success.message" });
38 }); 40 });
39 }); 41 });
src/app/article/comment/comments.component.spec.ts
@@ -83,5 +83,26 @@ describe(&quot;Components&quot;, () =&gt; { @@ -83,5 +83,26 @@ describe(&quot;Components&quot;, () =&gt; {
83 }); 83 });
84 }); 84 });
85 85
  86 + it("remove comment when calling commentRemoved", done => {
  87 + createComponent().then(fixture => {
  88 + let component: CommentsComponent = fixture.debugElement.componentViewChildren[0].componentInstance;
  89 + let comment = { id: 1 };
  90 + component.comments = <any>[comment];
  91 + component.commentRemoved(<any>comment);
  92 + expect(component.comments).toEqual([]);
  93 + done();
  94 + });
  95 + });
  96 +
  97 + it("do nothing when call commentRemoved with a comment that doesn't belongs to the comments list", done => {
  98 + createComponent().then(fixture => {
  99 + let component: CommentsComponent = fixture.debugElement.componentViewChildren[0].componentInstance;
  100 + let comment = { id: 1 };
  101 + component.comments = <any>[comment];
  102 + component.commentRemoved(<any>{ id: 2 });
  103 + expect(component.comments).toEqual([comment]);
  104 + done();
  105 + });
  106 + });
86 }); 107 });
87 }); 108 });
src/app/article/comment/comments.component.ts
@@ -37,6 +37,13 @@ export class CommentsComponent { @@ -37,6 +37,13 @@ export class CommentsComponent {
37 this.resetShowReply(); 37 this.resetShowReply();
38 } 38 }
39 39
  40 + commentRemoved(comment: noosfero.Comment): void {
  41 + let index = this.comments.indexOf(comment, 0);
  42 + if (index >= 0) {
  43 + this.comments.splice(index, 1);
  44 + }
  45 + }
  46 +
40 private resetShowReply() { 47 private resetShowReply() {
41 this.comments.forEach((comment: noosfero.CommentViewModel) => { 48 this.comments.forEach((comment: noosfero.CommentViewModel) => {
42 comment.__show_reply = false; 49 comment.__show_reply = false;
src/app/article/comment/comments.html
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 <noosfero-post-comment (comment-saved)="ctrl.commentAdded($event.detail)" ng-if="ctrl.showForm" [article]="ctrl.article" [parent]="ctrl.parent" [comment]="ctrl.newComment"></noosfero-post-comment> 2 <noosfero-post-comment (comment-saved)="ctrl.commentAdded($event.detail)" ng-if="ctrl.showForm" [article]="ctrl.article" [parent]="ctrl.parent" [comment]="ctrl.newComment"></noosfero-post-comment>
3 3
4 <div class="comments-list"> 4 <div class="comments-list">
5 - <noosfero-comment ng-repeat="comment in ctrl.comments | orderBy: 'created_at':true" [comment]="comment" [article]="ctrl.article"></noosfero-comment> 5 + <noosfero-comment (comment-removed)="ctrl.commentRemoved($event.detail)" ng-repeat="comment in ctrl.comments | orderBy: 'created_at':true" [comment]="comment" [article]="ctrl.article"></noosfero-comment>
6 </div> 6 </div>
7 <button type="button" ng-if="ctrl.displayMore()" class="more-comments btn btn-default btn-block" ng-click="ctrl.loadNextPage()">{{"comment.pagination.more" | translate}}</button> 7 <button type="button" ng-if="ctrl.displayMore()" class="more-comments btn btn-default btn-block" ng-click="ctrl.loadNextPage()">{{"comment.pagination.more" | translate}}</button>
8 </div> 8 </div>