Commit 64a5063995023f72b6c3f2900ce0bec23651d2a1
1 parent
628d8b3b
Refactor to update comment by a event emitter on CommentService. Needs fix one unit test
Showing
10 changed files
with
45 additions
and
27 deletions
Show diff stats
src/app/article/comment/comments.component.spec.ts
@@ -12,6 +12,7 @@ describe("Components", () => { | @@ -12,6 +12,7 @@ describe("Components", () => { | ||
12 | beforeEach(angular.mock.module("templates")); | 12 | beforeEach(angular.mock.module("templates")); |
13 | 13 | ||
14 | let commentService = jasmine.createSpyObj("commentService", ["getByArticle"]); | 14 | let commentService = jasmine.createSpyObj("commentService", ["getByArticle"]); |
15 | + commentService.onSave = helpers.mocks.commentService.onSave; | ||
15 | 16 | ||
16 | let comments = [{ id: 2 }, { id: 3 }]; | 17 | let comments = [{ id: 2 }, { id: 3 }]; |
17 | commentService.getByArticle = jasmine.createSpy("getByArticle") | 18 | commentService.getByArticle = jasmine.createSpy("getByArticle") |
@@ -19,7 +20,7 @@ describe("Components", () => { | @@ -19,7 +20,7 @@ describe("Components", () => { | ||
19 | 20 | ||
20 | let properties = { article: { id: 1 }, parent: <any>null }; | 21 | let properties = { article: { id: 1 }, parent: <any>null }; |
21 | function createComponent() { | 22 | function createComponent() { |
22 | - // postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["subscribe"]); | 23 | + |
23 | let providers = [ | 24 | let providers = [ |
24 | helpers.createProviderToValue('CommentService', commentService), | 25 | helpers.createProviderToValue('CommentService', commentService), |
25 | helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), | 26 | helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), |
@@ -52,7 +53,9 @@ describe("Components", () => { | @@ -52,7 +53,9 @@ describe("Components", () => { | ||
52 | it("update comments list when receive an reply", done => { | 53 | it("update comments list when receive an reply", done => { |
53 | properties.parent = { id: 3 }; | 54 | properties.parent = { id: 3 }; |
54 | createComponent().then(fixture => { | 55 | createComponent().then(fixture => { |
55 | - (<CommentsComponent>fixture.debugElement.componentViewChildren[0].componentInstance).commentAdded(<noosfero.Comment>{ id: 1, reply_of: { id: 3 } }); | 56 | + let component = (<CommentsComponent>fixture.debugElement.componentViewChildren[0].componentInstance); |
57 | + component.commentService.onSave.next.call(component, <noosfero.Comment>{ id: 1, reply_of: { id: 3 } }); | ||
58 | + console.log('Comments: ' + component.comments.length); | ||
56 | fixture.detectChanges(); | 59 | fixture.detectChanges(); |
57 | expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(3); | 60 | expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(3); |
58 | done(); | 61 | done(); |
src/app/article/comment/comments.component.ts
@@ -6,8 +6,7 @@ import { CommentComponent } from "./comment.component"; | @@ -6,8 +6,7 @@ import { CommentComponent } from "./comment.component"; | ||
6 | @Component({ | 6 | @Component({ |
7 | selector: 'noosfero-comments', | 7 | selector: 'noosfero-comments', |
8 | templateUrl: 'app/article/comment/comments.html', | 8 | templateUrl: 'app/article/comment/comments.html', |
9 | - directives: [PostCommentComponent, CommentComponent], | ||
10 | - outputs: ['commentAdded'] | 9 | + directives: [PostCommentComponent, CommentComponent] |
11 | }) | 10 | }) |
12 | @Inject(CommentService, "$element") | 11 | @Inject(CommentService, "$element") |
13 | export class CommentsComponent { | 12 | export class CommentsComponent { |
@@ -22,7 +21,7 @@ export class CommentsComponent { | @@ -22,7 +21,7 @@ export class CommentsComponent { | ||
22 | 21 | ||
23 | newComment = <noosfero.Comment>{}; | 22 | newComment = <noosfero.Comment>{}; |
24 | 23 | ||
25 | - constructor(protected commentService: CommentService, private $scope: ng.IScope) { } | 24 | + constructor(public commentService: CommentService, private $scope: ng.IScope) { } |
26 | 25 | ||
27 | ngOnInit() { | 26 | ngOnInit() { |
28 | if (this.parent) { | 27 | if (this.parent) { |
@@ -30,11 +29,11 @@ export class CommentsComponent { | @@ -30,11 +29,11 @@ export class CommentsComponent { | ||
30 | } else { | 29 | } else { |
31 | this.loadNextPage(); | 30 | this.loadNextPage(); |
32 | } | 31 | } |
33 | - } | ||
34 | 32 | ||
35 | - commentAdded(comment: noosfero.Comment): void { | ||
36 | - this.comments.push(comment); | ||
37 | - this.resetShowReply(); | 33 | + this.commentService.onSave.subscribe((comment: noosfero.Comment) => { |
34 | + this.comments.push(comment); | ||
35 | + this.resetShowReply(); | ||
36 | + }); | ||
38 | } | 37 | } |
39 | 38 | ||
40 | private resetShowReply() { | 39 | private resetShowReply() { |
src/app/article/comment/comments.html
1 | <div class="comments"> | 1 | <div class="comments"> |
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 | - | 2 | + <noosfero-post-comment ng-if="ctrl.showForm" [article]="ctrl.article" [parent]="ctrl.parent" [comment]="ctrl.newComment"></noosfero-post-comment> |
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 ng-repeat="comment in ctrl.comments | orderBy: 'created_at':true" [comment]="comment" [article]="ctrl.article"></noosfero-comment> |
6 | </div> | 6 | </div> |
src/app/article/comment/post-comment/post-comment.component.spec.ts
@@ -12,6 +12,7 @@ describe("Components", () => { | @@ -12,6 +12,7 @@ describe("Components", () => { | ||
12 | beforeEach(angular.mock.module("templates")); | 12 | beforeEach(angular.mock.module("templates")); |
13 | 13 | ||
14 | let commentService = jasmine.createSpyObj("commentService", ["createInArticle"]); | 14 | let commentService = jasmine.createSpyObj("commentService", ["createInArticle"]); |
15 | + commentService.onSave = jasmine.createSpyObj("onSave", ["subscribe", "next"]); | ||
15 | let user = {}; | 16 | let user = {}; |
16 | let providers = [ | 17 | let providers = [ |
17 | new Provider('CommentService', { useValue: commentService }), | 18 | new Provider('CommentService', { useValue: commentService }), |
@@ -43,10 +44,9 @@ describe("Components", () => { | @@ -43,10 +44,9 @@ describe("Components", () => { | ||
43 | it("emit an event when create comment", done => { | 44 | it("emit an event when create comment", done => { |
44 | helpers.createComponentFromClass(ContainerComponent).then(fixture => { | 45 | helpers.createComponentFromClass(ContainerComponent).then(fixture => { |
45 | let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | 46 | let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; |
46 | - component.commentSaved.next = jasmine.createSpy("next"); | ||
47 | commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); | 47 | commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); |
48 | component.save(); | 48 | component.save(); |
49 | - expect(component.commentSaved.next).toHaveBeenCalled(); | 49 | + expect(component.commentService.onSave.next).toHaveBeenCalled(); |
50 | done(); | 50 | done(); |
51 | }); | 51 | }); |
52 | }); | 52 | }); |
src/app/article/comment/post-comment/post-comment.component.ts
@@ -17,11 +17,11 @@ export class PostCommentComponent { | @@ -17,11 +17,11 @@ export class PostCommentComponent { | ||
17 | 17 | ||
18 | @Input() article: noosfero.Article; | 18 | @Input() article: noosfero.Article; |
19 | @Input() parent: noosfero.Comment; | 19 | @Input() parent: noosfero.Comment; |
20 | - @Output() commentSaved: EventEmitter<Comment> = new EventEmitter<Comment>(); | ||
21 | @Input() comment = <noosfero.Comment>{}; | 20 | @Input() comment = <noosfero.Comment>{}; |
22 | private currentUser: noosfero.User; | 21 | private currentUser: noosfero.User; |
23 | 22 | ||
24 | - constructor(private commentService: CommentService, | 23 | + constructor( |
24 | + public commentService: CommentService, | ||
25 | private notificationService: NotificationService, | 25 | private notificationService: NotificationService, |
26 | private session: SessionService) { | 26 | private session: SessionService) { |
27 | this.currentUser = this.session.currentUser(); | 27 | this.currentUser = this.session.currentUser(); |
@@ -32,7 +32,8 @@ export class PostCommentComponent { | @@ -32,7 +32,8 @@ export class PostCommentComponent { | ||
32 | this.comment.reply_of_id = this.parent.id; | 32 | this.comment.reply_of_id = this.parent.id; |
33 | } | 33 | } |
34 | this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult<noosfero.Comment>) => { | 34 | this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult<noosfero.Comment>) => { |
35 | - this.commentSaved.next(result.data); | 35 | + |
36 | + this.commentService.onSave.next(result.data); | ||
36 | this.comment.body = ""; | 37 | this.comment.body = ""; |
37 | this.notificationService.success({ title: "comment.post.success.title", message: "comment.post.success.message" }); | 38 | this.notificationService.success({ title: "comment.post.success.title", message: "comment.post.success.message" }); |
38 | }); | 39 | }); |
src/lib/ng-noosfero-api/http/comment.service.ts
1 | -import { Injectable, Inject } from "ng-forward"; | 1 | +import { Injectable, Inject, EventEmitter } from "ng-forward"; |
2 | import {RestangularService} from "./restangular_service"; | 2 | import {RestangularService} from "./restangular_service"; |
3 | import {ArticleService} from "./article.service"; | 3 | import {ArticleService} from "./article.service"; |
4 | 4 | ||
@@ -6,6 +6,8 @@ import {ArticleService} from "./article.service"; | @@ -6,6 +6,8 @@ import {ArticleService} from "./article.service"; | ||
6 | @Inject("Restangular", "$q", "$log", ArticleService) | 6 | @Inject("Restangular", "$q", "$log", ArticleService) |
7 | export class CommentService extends RestangularService<noosfero.Comment> { | 7 | export class CommentService extends RestangularService<noosfero.Comment> { |
8 | 8 | ||
9 | + public onSave: EventEmitter<noosfero.Comment> = new EventEmitter<noosfero.Comment>(); | ||
10 | + | ||
9 | constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService, protected articleService: ArticleService) { | 11 | constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService, protected articleService: ArticleService) { |
10 | super(Restangular, $q, $log); | 12 | super(Restangular, $q, $log); |
11 | } | 13 | } |
src/plugins/comment_paragraph/allow-comment/allow-comment.component.spec.ts
@@ -24,7 +24,8 @@ describe("Components", () => { | @@ -24,7 +24,8 @@ describe("Components", () => { | ||
24 | 24 | ||
25 | let providers = [ | 25 | let providers = [ |
26 | new Provider('CommentParagraphService', { useValue: serviceMock }), | 26 | new Provider('CommentParagraphService', { useValue: serviceMock }), |
27 | - new Provider('CommentParagraphEventService', { useValue: eventServiceMock }) | 27 | + new Provider('CommentParagraphEventService', { useValue: eventServiceMock }), |
28 | + new Provider('CommentService', { useValue: helpers.mocks.commentService }) | ||
28 | ]; | 29 | ]; |
29 | let helper: ComponentTestHelper<AllowCommentComponent>; | 30 | let helper: ComponentTestHelper<AllowCommentComponent>; |
30 | 31 |
src/plugins/comment_paragraph/allow-comment/allow-comment.component.ts
1 | -import {Component, Input, Inject} from "ng-forward"; | 1 | +import {Component, Input, Inject, bundleStore} from "ng-forward"; |
2 | import {SideCommentsComponent} from "../side-comments/side-comments.component"; | 2 | import {SideCommentsComponent} from "../side-comments/side-comments.component"; |
3 | import {CommentParagraphEventService} from "../events/comment-paragraph-event.service"; | 3 | import {CommentParagraphEventService} from "../events/comment-paragraph-event.service"; |
4 | import {CommentParagraphService} from "../http/comment-paragraph.service"; | 4 | import {CommentParagraphService} from "../http/comment-paragraph.service"; |
5 | +import {CommentService} from "../../../lib/ng-noosfero-api/http/comment.service"; | ||
6 | +import {PostCommentComponent} from '../../../app/article/comment/post-comment/post-comment.component'; | ||
5 | 7 | ||
6 | @Component({ | 8 | @Component({ |
7 | selector: "comment-paragraph-plugin-allow-comment", | 9 | selector: "comment-paragraph-plugin-allow-comment", |
8 | templateUrl: "plugins/comment_paragraph/allow-comment/allow-comment.html", | 10 | templateUrl: "plugins/comment_paragraph/allow-comment/allow-comment.html", |
9 | directives: [SideCommentsComponent] | 11 | directives: [SideCommentsComponent] |
10 | }) | 12 | }) |
11 | -@Inject("$scope", CommentParagraphEventService, CommentParagraphService) | 13 | +@Inject("$scope", CommentParagraphEventService, CommentParagraphService, CommentService) |
12 | export class AllowCommentComponent { | 14 | export class AllowCommentComponent { |
13 | 15 | ||
14 | @Input() content: string; | 16 | @Input() content: string; |
@@ -19,16 +21,22 @@ export class AllowCommentComponent { | @@ -19,16 +21,22 @@ export class AllowCommentComponent { | ||
19 | 21 | ||
20 | constructor(private $scope: ng.IScope, | 22 | constructor(private $scope: ng.IScope, |
21 | private commentParagraphEventService: CommentParagraphEventService, | 23 | private commentParagraphEventService: CommentParagraphEventService, |
22 | - private commentParagraphService: CommentParagraphService) { } | 24 | + private commentParagraphService: CommentParagraphService, |
25 | + private commentService: CommentService) { } | ||
23 | 26 | ||
24 | ngOnInit() { | 27 | ngOnInit() { |
25 | this.commentParagraphEventService.subscribeToggleCommentParagraph((article: noosfero.Article) => { | 28 | this.commentParagraphEventService.subscribeToggleCommentParagraph((article: noosfero.Article) => { |
26 | this.article = article; | 29 | this.article = article; |
27 | this.$scope.$apply(); | 30 | this.$scope.$apply(); |
28 | }); | 31 | }); |
32 | + | ||
29 | this.commentParagraphService.commentParagraphCount(this.article, this.paragraphUuid).then((count: number) => { | 33 | this.commentParagraphService.commentParagraphCount(this.article, this.paragraphUuid).then((count: number) => { |
30 | this.commentsCount = count; | 34 | this.commentsCount = count; |
31 | }); | 35 | }); |
36 | + | ||
37 | + this.commentService.onSave.subscribe((comment: noosfero.Comment) => { | ||
38 | + this.commentsCount++; | ||
39 | + }); | ||
32 | } | 40 | } |
33 | 41 | ||
34 | isActivated() { | 42 | isActivated() { |
src/plugins/comment_paragraph/side-comments/side-comments.component.spec.ts
@@ -12,14 +12,9 @@ describe("Components", () => { | @@ -12,14 +12,9 @@ describe("Components", () => { | ||
12 | let serviceMock = jasmine.createSpyObj("CommentParagraphService", ["getByArticle"]); | 12 | let serviceMock = jasmine.createSpyObj("CommentParagraphService", ["getByArticle"]); |
13 | serviceMock.getByArticle = jasmine.createSpy("getByArticle").and.returnValue(Promise.resolve({ data: {} })); | 13 | serviceMock.getByArticle = jasmine.createSpy("getByArticle").and.returnValue(Promise.resolve({ data: {} })); |
14 | 14 | ||
15 | - let commentServiceMock = {}; | ||
16 | - let postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["emit", "subscribe"]); | ||
17 | - postCommentEventService.subscribe = jasmine.createSpy("subscribe"); | ||
18 | - | ||
19 | let providers = [ | 15 | let providers = [ |
20 | new Provider('CommentParagraphService', { useValue: serviceMock }), | 16 | new Provider('CommentParagraphService', { useValue: serviceMock }), |
21 | - new Provider('CommentService', { useValue: commentServiceMock }), | ||
22 | - new Provider('PostCommentEventService', { useValue: postCommentEventService }) | 17 | + new Provider('CommentService', { useValue: helpers.mocks.commentService }), |
23 | ]; | 18 | ]; |
24 | let helper: ComponentTestHelper<SideCommentsComponent>; | 19 | let helper: ComponentTestHelper<SideCommentsComponent>; |
25 | 20 |
src/spec/mocks.ts
@@ -124,6 +124,15 @@ export var mocks: any = { | @@ -124,6 +124,15 @@ export var mocks: any = { | ||
124 | commentService: { | 124 | commentService: { |
125 | getByArticle: (article: noosfero.Article) => { | 125 | getByArticle: (article: noosfero.Article) => { |
126 | return Promise.resolve({ data: {} }); | 126 | return Promise.resolve({ data: {} }); |
127 | + }, | ||
128 | + onSave: { | ||
129 | + event: Function, | ||
130 | + subscribe: (fn: Function) => { | ||
131 | + mocks.commentService['onSave'].event = fn; | ||
132 | + }, | ||
133 | + next: (param: any) => { | ||
134 | + mocks.commentService['onSave'].event.call(this, param); | ||
135 | + } | ||
127 | } | 136 | } |
128 | }, | 137 | }, |
129 | sessionWithCurrentUser: (user: any) => { | 138 | sessionWithCurrentUser: (user: any) => { |