Commit 660909615460c30c08435d83310ce85cb82f09ee
1 parent
e8918c10
Exists in
master
and in
30 other branches
Refactor comment received event
Showing
3 changed files
with
8 additions
and
4 deletions
Show diff stats
src/app/article/comment/comments.component.spec.ts
@@ -2,6 +2,7 @@ import {Provider, provide, Component} from 'ng-forward'; | @@ -2,6 +2,7 @@ import {Provider, provide, Component} from 'ng-forward'; | ||
2 | import * as helpers from "../../../spec/helpers"; | 2 | import * as helpers from "../../../spec/helpers"; |
3 | 3 | ||
4 | import {CommentsComponent} from './comments.component'; | 4 | import {CommentsComponent} from './comments.component'; |
5 | +import {PostCommentComponent} from "./post-comment.component"; | ||
5 | 6 | ||
6 | const htmlTemplate: string = '<noosfero-comments [article]="ctrl.article"></noosfero-comments>'; | 7 | const htmlTemplate: string = '<noosfero-comments [article]="ctrl.article"></noosfero-comments>'; |
7 | 8 | ||
@@ -42,8 +43,9 @@ describe("Components", () => { | @@ -42,8 +43,9 @@ describe("Components", () => { | ||
42 | 43 | ||
43 | it("update comments list when receive an event", done => { | 44 | it("update comments list when receive an event", done => { |
44 | helpers.createComponentFromClass(ContainerComponent).then(fixture => { | 45 | helpers.createComponentFromClass(ContainerComponent).then(fixture => { |
45 | - fixture.debugElement.getLocal("$rootScope").$emit("comment.received", {}); | ||
46 | - expect(fixture.debugElement.queryAll("noosfero-post-comment").length).toEqual(1); | 46 | + fixture.debugElement.getLocal("$rootScope").$emit(PostCommentComponent.EVENT_COMMENT_RECEIVED, { id: 1 }); |
47 | + fixture.debugElement.getLocal("$rootScope").$apply(); | ||
48 | + expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(3); | ||
47 | done(); | 49 | done(); |
48 | }); | 50 | }); |
49 | }); | 51 | }); |
src/app/article/comment/comments.component.ts
@@ -15,7 +15,7 @@ export class CommentsComponent { | @@ -15,7 +15,7 @@ export class CommentsComponent { | ||
15 | @Input() article: noosfero.Article; | 15 | @Input() article: noosfero.Article; |
16 | 16 | ||
17 | constructor(private commentService: CommentService, private $rootScope: ng.IScope) { | 17 | constructor(private commentService: CommentService, private $rootScope: ng.IScope) { |
18 | - $rootScope.$on("comment.received", (event: ng.IAngularEvent, comment: noosfero.Comment) => { | 18 | + $rootScope.$on(PostCommentComponent.EVENT_COMMENT_RECEIVED, (event: ng.IAngularEvent, comment: noosfero.Comment) => { |
19 | this.comments.push(comment); | 19 | this.comments.push(comment); |
20 | }); | 20 | }); |
21 | } | 21 | } |
src/app/article/comment/post-comment.component.ts
@@ -9,6 +9,8 @@ import { NotificationService } from "../../shared/services/notification.service" | @@ -9,6 +9,8 @@ import { NotificationService } from "../../shared/services/notification.service" | ||
9 | @Inject(CommentService, NotificationService, "$rootScope") | 9 | @Inject(CommentService, NotificationService, "$rootScope") |
10 | export class PostCommentComponent { | 10 | export class PostCommentComponent { |
11 | 11 | ||
12 | + public static EVENT_COMMENT_RECEIVED = "comment.received"; | ||
13 | + | ||
12 | @Input() article: noosfero.Article; | 14 | @Input() article: noosfero.Article; |
13 | comment: noosfero.Comment; | 15 | comment: noosfero.Comment; |
14 | 16 | ||
@@ -21,7 +23,7 @@ export class PostCommentComponent { | @@ -21,7 +23,7 @@ export class PostCommentComponent { | ||
21 | this.comment.reply_of_id = this.replyOf.id; | 23 | this.comment.reply_of_id = this.replyOf.id; |
22 | } | 24 | } |
23 | this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult<noosfero.Comment>) => { | 25 | this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult<noosfero.Comment>) => { |
24 | - this.$rootScope.$emit("comment.received", result.data); | 26 | + this.$rootScope.$emit(PostCommentComponent.EVENT_COMMENT_RECEIVED, result.data); |
25 | this.notificationService.success({ title: "Good job!", message: "Comment saved!" }); | 27 | this.notificationService.success({ title: "Good job!", message: "Comment saved!" }); |
26 | }); | 28 | }); |
27 | } | 29 | } |