Commit 132b5d04d78480a9257ec73362e9f3136d76fdd6
1 parent
845f879b
Exists in
master
and in
28 other branches
Refactor post comment event to use EventEmitter
Showing
8 changed files
with
97 additions
and
28 deletions
Show diff stats
src/app/article/comment/comment.component.spec.ts
@@ -11,8 +11,12 @@ describe("Components", () => { | @@ -11,8 +11,12 @@ describe("Components", () => { | ||
11 | 11 | ||
12 | beforeEach(angular.mock.module("templates")); | 12 | beforeEach(angular.mock.module("templates")); |
13 | 13 | ||
14 | + let postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["subscribe"]); | ||
15 | + | ||
14 | function createComponent() { | 16 | function createComponent() { |
15 | - let providers = helpers.provideFilters("translateFilter"); | 17 | + let providers = [ |
18 | + new Provider('PostCommentEventService', { useValue: postCommentEventService }) | ||
19 | + ].concat(helpers.provideFilters("translateFilter")); | ||
16 | 20 | ||
17 | @Component({ selector: 'test-container-component', directives: [CommentComponent], template: htmlTemplate, providers: providers }) | 21 | @Component({ selector: 'test-container-component', directives: [CommentComponent], template: htmlTemplate, providers: providers }) |
18 | class ContainerComponent { | 22 | class ContainerComponent { |
@@ -46,10 +50,14 @@ describe("Components", () => { | @@ -46,10 +50,14 @@ describe("Components", () => { | ||
46 | }); | 50 | }); |
47 | 51 | ||
48 | it("close form when receive a reply", done => { | 52 | it("close form when receive a reply", done => { |
53 | + let func: Function; | ||
54 | + postCommentEventService.subscribe = (fn: Function) => { | ||
55 | + func = fn; | ||
56 | + }; | ||
49 | createComponent().then(fixture => { | 57 | createComponent().then(fixture => { |
50 | let component = fixture.debugElement.componentViewChildren[0]; | 58 | let component = fixture.debugElement.componentViewChildren[0]; |
51 | component.componentInstance.showReply = true; | 59 | component.componentInstance.showReply = true; |
52 | - fixture.debugElement.getLocal("$rootScope").$broadcast(PostCommentComponent.EVENT_COMMENT_RECEIVED, {}); | 60 | + func(<noosfero.Comment>{}); |
53 | expect(component.componentInstance.showReply).toEqual(false); | 61 | expect(component.componentInstance.showReply).toEqual(false); |
54 | done(); | 62 | done(); |
55 | }); | 63 | }); |
src/app/article/comment/comment.component.ts
1 | import { Inject, Input, Component } from 'ng-forward'; | 1 | import { Inject, Input, Component } from 'ng-forward'; |
2 | import { PostCommentComponent } from "./post-comment/post-comment.component"; | 2 | import { PostCommentComponent } from "./post-comment/post-comment.component"; |
3 | +import { PostCommentEventService } from "./post-comment/post-comment-event.service"; | ||
3 | 4 | ||
4 | @Component({ | 5 | @Component({ |
5 | selector: 'noosfero-comment', | 6 | selector: 'noosfero-comment', |
6 | templateUrl: 'app/article/comment/comment.html' | 7 | templateUrl: 'app/article/comment/comment.html' |
7 | }) | 8 | }) |
8 | -@Inject("$scope") | 9 | +@Inject(PostCommentEventService, "$scope") |
9 | export class CommentComponent { | 10 | export class CommentComponent { |
10 | 11 | ||
11 | @Input() comment: noosfero.Comment; | 12 | @Input() comment: noosfero.Comment; |
@@ -13,9 +14,10 @@ export class CommentComponent { | @@ -13,9 +14,10 @@ export class CommentComponent { | ||
13 | 14 | ||
14 | showReply: boolean = false; | 15 | showReply: boolean = false; |
15 | 16 | ||
16 | - constructor(private $scope: ng.IScope) { | ||
17 | - $scope.$on(PostCommentComponent.EVENT_COMMENT_RECEIVED, (event: ng.IAngularEvent, comment: noosfero.Comment) => { | 17 | + constructor(postCommentEventService: PostCommentEventService, private $scope: ng.IScope) { |
18 | + postCommentEventService.subscribe((comment: noosfero.Comment) => { | ||
18 | this.showReply = false; | 19 | this.showReply = false; |
20 | + this.$scope.$apply(); | ||
19 | }); | 21 | }); |
20 | } | 22 | } |
21 | 23 |
src/app/article/comment/comments.component.spec.ts
@@ -17,14 +17,23 @@ describe("Components", () => { | @@ -17,14 +17,23 @@ describe("Components", () => { | ||
17 | commentService.getByArticle = jasmine.createSpy("getByArticle") | 17 | commentService.getByArticle = jasmine.createSpy("getByArticle") |
18 | .and.returnValue(helpers.mocks.promiseResultTemplate({ data: comments })); | 18 | .and.returnValue(helpers.mocks.promiseResultTemplate({ data: comments })); |
19 | 19 | ||
20 | - let providers = [ | ||
21 | - helpers.createProviderToValue('CommentService', commentService), | ||
22 | - helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), | ||
23 | - helpers.createProviderToValue('SessionService', helpers.mocks.sessionWithCurrentUser({})) | ||
24 | - ].concat(helpers.provideFilters("translateFilter")); | 20 | + let emitEvent: Function; |
21 | + let postCommentEventService = { | ||
22 | + subscribe: (fn: Function) => { | ||
23 | + emitEvent = fn; | ||
24 | + } | ||
25 | + }; | ||
25 | 26 | ||
26 | let properties = { article: { id: 1 }, parent: <any>null }; | 27 | let properties = { article: { id: 1 }, parent: <any>null }; |
27 | function createComponent() { | 28 | function createComponent() { |
29 | + // postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["subscribe"]); | ||
30 | + let providers = [ | ||
31 | + helpers.createProviderToValue('CommentService', commentService), | ||
32 | + helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), | ||
33 | + helpers.createProviderToValue('SessionService', helpers.mocks.sessionWithCurrentUser({})), | ||
34 | + new Provider('PostCommentEventService', { useValue: postCommentEventService }) | ||
35 | + ].concat(helpers.provideFilters("translateFilter")); | ||
36 | + | ||
28 | return helpers.quickCreateComponent({ | 37 | return helpers.quickCreateComponent({ |
29 | providers: providers, | 38 | providers: providers, |
30 | directives: [CommentsComponent], | 39 | directives: [CommentsComponent], |
@@ -49,10 +58,9 @@ describe("Components", () => { | @@ -49,10 +58,9 @@ describe("Components", () => { | ||
49 | }); | 58 | }); |
50 | 59 | ||
51 | it("update comments list when receive an reply", done => { | 60 | it("update comments list when receive an reply", done => { |
52 | - properties.parent = { id: 2 }; | 61 | + properties.parent = { id: 3 }; |
53 | createComponent().then(fixture => { | 62 | createComponent().then(fixture => { |
54 | - fixture.debugElement.getLocal("$rootScope").$emit(PostCommentComponent.EVENT_COMMENT_RECEIVED, { id: 1, reply_of: properties.parent }); | ||
55 | - fixture.debugElement.getLocal("$rootScope").$apply(); | 63 | + emitEvent(<noosfero.Comment>{ id: 1, reply_of: { id: 3 } }); |
56 | expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(3); | 64 | expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(3); |
57 | done(); | 65 | done(); |
58 | }); | 66 | }); |
src/app/article/comment/comments.component.ts
@@ -2,13 +2,14 @@ import { Inject, Input, Component, provide } from 'ng-forward'; | @@ -2,13 +2,14 @@ import { Inject, Input, Component, provide } from 'ng-forward'; | ||
2 | import { PostCommentComponent } from "./post-comment/post-comment.component"; | 2 | import { PostCommentComponent } from "./post-comment/post-comment.component"; |
3 | import { CommentService } from "../../../lib/ng-noosfero-api/http/comment.service"; | 3 | import { CommentService } from "../../../lib/ng-noosfero-api/http/comment.service"; |
4 | import { CommentComponent } from "./comment.component"; | 4 | import { CommentComponent } from "./comment.component"; |
5 | +import { PostCommentEventService } from "./post-comment/post-comment-event.service"; | ||
5 | 6 | ||
6 | @Component({ | 7 | @Component({ |
7 | selector: 'noosfero-comments', | 8 | selector: 'noosfero-comments', |
8 | templateUrl: 'app/article/comment/comments.html', | 9 | templateUrl: 'app/article/comment/comments.html', |
9 | directives: [PostCommentComponent, CommentComponent] | 10 | directives: [PostCommentComponent, CommentComponent] |
10 | }) | 11 | }) |
11 | -@Inject(CommentService, "$rootScope") | 12 | +@Inject(CommentService, PostCommentEventService, "$scope") |
12 | export class CommentsComponent { | 13 | export class CommentsComponent { |
13 | 14 | ||
14 | comments: noosfero.Comment[] = []; | 15 | comments: noosfero.Comment[] = []; |
@@ -19,14 +20,7 @@ export class CommentsComponent { | @@ -19,14 +20,7 @@ export class CommentsComponent { | ||
19 | protected perPage = 5; | 20 | protected perPage = 5; |
20 | protected total = 0; | 21 | protected total = 0; |
21 | 22 | ||
22 | - constructor(protected commentService: CommentService, protected $rootScope: ng.IScope) { | ||
23 | - $rootScope.$on(PostCommentComponent.EVENT_COMMENT_RECEIVED, (event: ng.IAngularEvent, comment: noosfero.Comment) => { | ||
24 | - if ((!this.parent && !comment.reply_of) || (comment.reply_of && this.parent && comment.reply_of.id === this.parent.id)) { | ||
25 | - if (!this.comments) this.comments = []; | ||
26 | - this.comments.push(comment); | ||
27 | - } | ||
28 | - }); | ||
29 | - } | 23 | + constructor(protected commentService: CommentService, private postCommentEventService: PostCommentEventService, private $scope: ng.IScope) { } |
30 | 24 | ||
31 | ngOnInit() { | 25 | ngOnInit() { |
32 | if (this.parent) { | 26 | if (this.parent) { |
@@ -34,6 +28,13 @@ export class CommentsComponent { | @@ -34,6 +28,13 @@ export class CommentsComponent { | ||
34 | } else { | 28 | } else { |
35 | this.loadNextPage(); | 29 | this.loadNextPage(); |
36 | } | 30 | } |
31 | + this.postCommentEventService.subscribe((comment: noosfero.Comment) => { | ||
32 | + if ((!this.parent && !comment.reply_of) || (comment.reply_of && this.parent && comment.reply_of.id === this.parent.id)) { | ||
33 | + if (!this.comments) this.comments = []; | ||
34 | + this.comments.push(comment); | ||
35 | + this.$scope.$apply(); | ||
36 | + } | ||
37 | + }); | ||
37 | } | 38 | } |
38 | 39 | ||
39 | loadComments() { | 40 | loadComments() { |
src/app/article/comment/post-comment/post-comment-event.service.spec.ts
0 → 100644
@@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
1 | +import {PostCommentEventService} from "./post-comment-event.service"; | ||
2 | +import {ComponentTestHelper, createClass} from '../../../../spec/component-test-helper'; | ||
3 | +import * as helpers from "../../../../spec/helpers"; | ||
4 | +import {Provider} from 'ng-forward'; | ||
5 | +import {ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder'; | ||
6 | + | ||
7 | +describe("Services", () => { | ||
8 | + describe("Comment Paragraph Event Service", () => { | ||
9 | + let eventService: PostCommentEventService; | ||
10 | + | ||
11 | + beforeEach(() => { | ||
12 | + eventService = new PostCommentEventService(); | ||
13 | + eventService['eventEmitter'] = jasmine.createSpyObj("eventEmitter", ["next", "subscribe"]); | ||
14 | + }); | ||
15 | + | ||
16 | + it('subscribe to post comment event', () => { | ||
17 | + eventService.subscribe(() => { }); | ||
18 | + expect(eventService['eventEmitter'].subscribe).toHaveBeenCalled(); | ||
19 | + }); | ||
20 | + | ||
21 | + it('emit event when post comment', () => { | ||
22 | + eventService.emit(<noosfero.Comment>{}); | ||
23 | + expect(eventService['eventEmitter'].next).toHaveBeenCalled(); | ||
24 | + }); | ||
25 | + }); | ||
26 | +}); |
src/app/article/comment/post-comment/post-comment-event.service.ts
0 → 100644
@@ -0,0 +1,19 @@ | @@ -0,0 +1,19 @@ | ||
1 | +import {Injectable, EventEmitter} from "ng-forward"; | ||
2 | + | ||
3 | +@Injectable() | ||
4 | +export class PostCommentEventService { | ||
5 | + | ||
6 | + private eventEmitter: EventEmitter<noosfero.Comment>; | ||
7 | + | ||
8 | + constructor() { | ||
9 | + this.eventEmitter = new EventEmitter(); | ||
10 | + } | ||
11 | + | ||
12 | + emit(comment: noosfero.Comment) { | ||
13 | + this.eventEmitter.next(comment); | ||
14 | + } | ||
15 | + | ||
16 | + subscribe(fn: (comment: noosfero.Comment) => void) { | ||
17 | + this.eventEmitter.subscribe(fn); | ||
18 | + } | ||
19 | +} |
src/app/article/comment/post-comment/post-comment.component.spec.ts
@@ -11,11 +11,13 @@ describe("Components", () => { | @@ -11,11 +11,13 @@ describe("Components", () => { | ||
11 | beforeEach(angular.mock.module("templates")); | 11 | beforeEach(angular.mock.module("templates")); |
12 | 12 | ||
13 | let commentService = jasmine.createSpyObj("commentService", ["createInArticle"]); | 13 | let commentService = jasmine.createSpyObj("commentService", ["createInArticle"]); |
14 | + let postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["emit"]); | ||
14 | let user = {}; | 15 | let user = {}; |
15 | let providers = [ | 16 | let providers = [ |
16 | new Provider('CommentService', { useValue: commentService }), | 17 | new Provider('CommentService', { useValue: commentService }), |
17 | new Provider('NotificationService', { useValue: helpers.mocks.notificationService }), | 18 | new Provider('NotificationService', { useValue: helpers.mocks.notificationService }), |
18 | - new Provider('SessionService', { useValue: helpers.mocks.sessionWithCurrentUser(user) }) | 19 | + new Provider('SessionService', { useValue: helpers.mocks.sessionWithCurrentUser(user) }), |
20 | + new Provider('PostCommentEventService', { useValue: postCommentEventService }) | ||
19 | ].concat(helpers.provideFilters("translateFilter")); | 21 | ].concat(helpers.provideFilters("translateFilter")); |
20 | 22 | ||
21 | @Component({ selector: 'test-container-component', directives: [PostCommentComponent], template: htmlTemplate, providers: providers }) | 23 | @Component({ selector: 'test-container-component', directives: [PostCommentComponent], template: htmlTemplate, providers: providers }) |
@@ -35,9 +37,8 @@ describe("Components", () => { | @@ -35,9 +37,8 @@ describe("Components", () => { | ||
35 | helpers.createComponentFromClass(ContainerComponent).then(fixture => { | 37 | helpers.createComponentFromClass(ContainerComponent).then(fixture => { |
36 | let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | 38 | let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; |
37 | commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); | 39 | commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); |
38 | - component["$scope"].$emit = jasmine.createSpy("$emit"); | ||
39 | component.save(); | 40 | component.save(); |
40 | - expect(component["$scope"].$emit).toHaveBeenCalledWith(PostCommentComponent.EVENT_COMMENT_RECEIVED, jasmine.any(Object)); | 41 | + expect(postCommentEventService.emit).toHaveBeenCalled(); |
41 | done(); | 42 | done(); |
42 | }); | 43 | }); |
43 | }); | 44 | }); |
src/app/article/comment/post-comment/post-comment.component.ts
@@ -2,12 +2,13 @@ import { Inject, Input, Component } from 'ng-forward'; | @@ -2,12 +2,13 @@ import { Inject, Input, Component } from 'ng-forward'; | ||
2 | import { CommentService } from "../../../../lib/ng-noosfero-api/http/comment.service"; | 2 | import { CommentService } from "../../../../lib/ng-noosfero-api/http/comment.service"; |
3 | import { NotificationService } from "../../../shared/services/notification.service"; | 3 | import { NotificationService } from "../../../shared/services/notification.service"; |
4 | import { SessionService } from "../../../login"; | 4 | import { SessionService } from "../../../login"; |
5 | +import { PostCommentEventService } from "./post-comment-event.service"; | ||
5 | 6 | ||
6 | @Component({ | 7 | @Component({ |
7 | selector: 'noosfero-post-comment', | 8 | selector: 'noosfero-post-comment', |
8 | templateUrl: 'app/article/comment/post-comment/post-comment.html' | 9 | templateUrl: 'app/article/comment/post-comment/post-comment.html' |
9 | }) | 10 | }) |
10 | -@Inject(CommentService, NotificationService, "$scope", SessionService) | 11 | +@Inject(CommentService, NotificationService, SessionService, PostCommentEventService) |
11 | export class PostCommentComponent { | 12 | export class PostCommentComponent { |
12 | 13 | ||
13 | public static EVENT_COMMENT_RECEIVED = "comment.received"; | 14 | public static EVENT_COMMENT_RECEIVED = "comment.received"; |
@@ -18,7 +19,10 @@ export class PostCommentComponent { | @@ -18,7 +19,10 @@ export class PostCommentComponent { | ||
18 | comment = <noosfero.Comment>{}; | 19 | comment = <noosfero.Comment>{}; |
19 | private currentUser: noosfero.User; | 20 | private currentUser: noosfero.User; |
20 | 21 | ||
21 | - constructor(private commentService: CommentService, private notificationService: NotificationService, private $scope: ng.IScope, private session: SessionService) { | 22 | + constructor(private commentService: CommentService, |
23 | + private notificationService: NotificationService, | ||
24 | + private session: SessionService, | ||
25 | + private postCommentEventService: PostCommentEventService) { | ||
22 | this.currentUser = this.session.currentUser(); | 26 | this.currentUser = this.session.currentUser(); |
23 | } | 27 | } |
24 | 28 | ||
@@ -27,7 +31,7 @@ export class PostCommentComponent { | @@ -27,7 +31,7 @@ export class PostCommentComponent { | ||
27 | this.comment.reply_of_id = this.parent.id; | 31 | this.comment.reply_of_id = this.parent.id; |
28 | } | 32 | } |
29 | this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult<noosfero.Comment>) => { | 33 | this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult<noosfero.Comment>) => { |
30 | - this.$scope.$emit(PostCommentComponent.EVENT_COMMENT_RECEIVED, result.data); | 34 | + this.postCommentEventService.emit(result.data); |
31 | this.comment.body = ""; | 35 | this.comment.body = ""; |
32 | this.notificationService.success({ title: "comment.post.success.title", message: "comment.post.success.message" }); | 36 | this.notificationService.success({ title: "comment.post.success.title", message: "comment.post.success.message" }); |
33 | }); | 37 | }); |