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 | 12 | beforeEach(angular.mock.module("templates")); |
13 | 13 | |
14 | 14 | let commentService = jasmine.createSpyObj("commentService", ["getByArticle"]); |
15 | + commentService.onSave = helpers.mocks.commentService.onSave; | |
15 | 16 | |
16 | 17 | let comments = [{ id: 2 }, { id: 3 }]; |
17 | 18 | commentService.getByArticle = jasmine.createSpy("getByArticle") |
... | ... | @@ -19,7 +20,7 @@ describe("Components", () => { |
19 | 20 | |
20 | 21 | let properties = { article: { id: 1 }, parent: <any>null }; |
21 | 22 | function createComponent() { |
22 | - // postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["subscribe"]); | |
23 | + | |
23 | 24 | let providers = [ |
24 | 25 | helpers.createProviderToValue('CommentService', commentService), |
25 | 26 | helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), |
... | ... | @@ -52,7 +53,9 @@ describe("Components", () => { |
52 | 53 | it("update comments list when receive an reply", done => { |
53 | 54 | properties.parent = { id: 3 }; |
54 | 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 | 59 | fixture.detectChanges(); |
57 | 60 | expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(3); |
58 | 61 | done(); | ... | ... |
src/app/article/comment/comments.component.ts
... | ... | @@ -6,8 +6,7 @@ import { CommentComponent } from "./comment.component"; |
6 | 6 | @Component({ |
7 | 7 | selector: 'noosfero-comments', |
8 | 8 | templateUrl: 'app/article/comment/comments.html', |
9 | - directives: [PostCommentComponent, CommentComponent], | |
10 | - outputs: ['commentAdded'] | |
9 | + directives: [PostCommentComponent, CommentComponent] | |
11 | 10 | }) |
12 | 11 | @Inject(CommentService, "$element") |
13 | 12 | export class CommentsComponent { |
... | ... | @@ -22,7 +21,7 @@ export class CommentsComponent { |
22 | 21 | |
23 | 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 | 26 | ngOnInit() { |
28 | 27 | if (this.parent) { |
... | ... | @@ -30,11 +29,11 @@ export class CommentsComponent { |
30 | 29 | } else { |
31 | 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 | 39 | private resetShowReply() { | ... | ... |
src/app/article/comment/comments.html
1 | 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 | 4 | <div class="comments-list"> |
5 | 5 | <noosfero-comment ng-repeat="comment in ctrl.comments | orderBy: 'created_at':true" [comment]="comment" [article]="ctrl.article"></noosfero-comment> |
6 | 6 | </div> | ... | ... |
src/app/article/comment/post-comment/post-comment.component.spec.ts
... | ... | @@ -12,6 +12,7 @@ describe("Components", () => { |
12 | 12 | beforeEach(angular.mock.module("templates")); |
13 | 13 | |
14 | 14 | let commentService = jasmine.createSpyObj("commentService", ["createInArticle"]); |
15 | + commentService.onSave = jasmine.createSpyObj("onSave", ["subscribe", "next"]); | |
15 | 16 | let user = {}; |
16 | 17 | let providers = [ |
17 | 18 | new Provider('CommentService', { useValue: commentService }), |
... | ... | @@ -43,10 +44,9 @@ describe("Components", () => { |
43 | 44 | it("emit an event when create comment", done => { |
44 | 45 | helpers.createComponentFromClass(ContainerComponent).then(fixture => { |
45 | 46 | let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; |
46 | - component.commentSaved.next = jasmine.createSpy("next"); | |
47 | 47 | commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); |
48 | 48 | component.save(); |
49 | - expect(component.commentSaved.next).toHaveBeenCalled(); | |
49 | + expect(component.commentService.onSave.next).toHaveBeenCalled(); | |
50 | 50 | done(); |
51 | 51 | }); |
52 | 52 | }); | ... | ... |
src/app/article/comment/post-comment/post-comment.component.ts
... | ... | @@ -17,11 +17,11 @@ export class PostCommentComponent { |
17 | 17 | |
18 | 18 | @Input() article: noosfero.Article; |
19 | 19 | @Input() parent: noosfero.Comment; |
20 | - @Output() commentSaved: EventEmitter<Comment> = new EventEmitter<Comment>(); | |
21 | 20 | @Input() comment = <noosfero.Comment>{}; |
22 | 21 | private currentUser: noosfero.User; |
23 | 22 | |
24 | - constructor(private commentService: CommentService, | |
23 | + constructor( | |
24 | + public commentService: CommentService, | |
25 | 25 | private notificationService: NotificationService, |
26 | 26 | private session: SessionService) { |
27 | 27 | this.currentUser = this.session.currentUser(); |
... | ... | @@ -32,7 +32,8 @@ export class PostCommentComponent { |
32 | 32 | this.comment.reply_of_id = this.parent.id; |
33 | 33 | } |
34 | 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 | 37 | this.comment.body = ""; |
37 | 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 | 2 | import {RestangularService} from "./restangular_service"; |
3 | 3 | import {ArticleService} from "./article.service"; |
4 | 4 | |
... | ... | @@ -6,6 +6,8 @@ import {ArticleService} from "./article.service"; |
6 | 6 | @Inject("Restangular", "$q", "$log", ArticleService) |
7 | 7 | export class CommentService extends RestangularService<noosfero.Comment> { |
8 | 8 | |
9 | + public onSave: EventEmitter<noosfero.Comment> = new EventEmitter<noosfero.Comment>(); | |
10 | + | |
9 | 11 | constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService, protected articleService: ArticleService) { |
10 | 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 | 24 | |
25 | 25 | let providers = [ |
26 | 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 | 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 | 2 | import {SideCommentsComponent} from "../side-comments/side-comments.component"; |
3 | 3 | import {CommentParagraphEventService} from "../events/comment-paragraph-event.service"; |
4 | 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 | 8 | @Component({ |
7 | 9 | selector: "comment-paragraph-plugin-allow-comment", |
8 | 10 | templateUrl: "plugins/comment_paragraph/allow-comment/allow-comment.html", |
9 | 11 | directives: [SideCommentsComponent] |
10 | 12 | }) |
11 | -@Inject("$scope", CommentParagraphEventService, CommentParagraphService) | |
13 | +@Inject("$scope", CommentParagraphEventService, CommentParagraphService, CommentService) | |
12 | 14 | export class AllowCommentComponent { |
13 | 15 | |
14 | 16 | @Input() content: string; |
... | ... | @@ -19,16 +21,22 @@ export class AllowCommentComponent { |
19 | 21 | |
20 | 22 | constructor(private $scope: ng.IScope, |
21 | 23 | private commentParagraphEventService: CommentParagraphEventService, |
22 | - private commentParagraphService: CommentParagraphService) { } | |
24 | + private commentParagraphService: CommentParagraphService, | |
25 | + private commentService: CommentService) { } | |
23 | 26 | |
24 | 27 | ngOnInit() { |
25 | 28 | this.commentParagraphEventService.subscribeToggleCommentParagraph((article: noosfero.Article) => { |
26 | 29 | this.article = article; |
27 | 30 | this.$scope.$apply(); |
28 | 31 | }); |
32 | + | |
29 | 33 | this.commentParagraphService.commentParagraphCount(this.article, this.paragraphUuid).then((count: number) => { |
30 | 34 | this.commentsCount = count; |
31 | 35 | }); |
36 | + | |
37 | + this.commentService.onSave.subscribe((comment: noosfero.Comment) => { | |
38 | + this.commentsCount++; | |
39 | + }); | |
32 | 40 | } |
33 | 41 | |
34 | 42 | isActivated() { | ... | ... |
src/plugins/comment_paragraph/side-comments/side-comments.component.spec.ts
... | ... | @@ -12,14 +12,9 @@ describe("Components", () => { |
12 | 12 | let serviceMock = jasmine.createSpyObj("CommentParagraphService", ["getByArticle"]); |
13 | 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 | 15 | let providers = [ |
20 | 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 | 19 | let helper: ComponentTestHelper<SideCommentsComponent>; |
25 | 20 | ... | ... |
src/spec/mocks.ts
... | ... | @@ -124,6 +124,15 @@ export var mocks: any = { |
124 | 124 | commentService: { |
125 | 125 | getByArticle: (article: noosfero.Article) => { |
126 | 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 | 138 | sessionWithCurrentUser: (user: any) => { | ... | ... |