diff --git a/src/app/article/comment/comment.component.spec.ts b/src/app/article/comment/comment.component.spec.ts index 0b0c812..e0f2fca 100644 --- a/src/app/article/comment/comment.component.spec.ts +++ b/src/app/article/comment/comment.component.spec.ts @@ -11,8 +11,12 @@ describe("Components", () => { beforeEach(angular.mock.module("templates")); + let postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["subscribe"]); + function createComponent() { - let providers = helpers.provideFilters("translateFilter"); + let providers = [ + new Provider('PostCommentEventService', { useValue: postCommentEventService }) + ].concat(helpers.provideFilters("translateFilter")); @Component({ selector: 'test-container-component', directives: [CommentComponent], template: htmlTemplate, providers: providers }) class ContainerComponent { @@ -46,10 +50,14 @@ describe("Components", () => { }); it("close form when receive a reply", done => { + let func: Function; + postCommentEventService.subscribe = (fn: Function) => { + func = fn; + }; createComponent().then(fixture => { let component = fixture.debugElement.componentViewChildren[0]; component.componentInstance.showReply = true; - fixture.debugElement.getLocal("$rootScope").$broadcast(PostCommentComponent.EVENT_COMMENT_RECEIVED, {}); + func({}); expect(component.componentInstance.showReply).toEqual(false); done(); }); diff --git a/src/app/article/comment/comment.component.ts b/src/app/article/comment/comment.component.ts index adafabd..ac74189 100644 --- a/src/app/article/comment/comment.component.ts +++ b/src/app/article/comment/comment.component.ts @@ -1,11 +1,12 @@ import { Inject, Input, Component } from 'ng-forward'; import { PostCommentComponent } from "./post-comment/post-comment.component"; +import { PostCommentEventService } from "./post-comment/post-comment-event.service"; @Component({ selector: 'noosfero-comment', templateUrl: 'app/article/comment/comment.html' }) -@Inject("$scope") +@Inject(PostCommentEventService, "$scope") export class CommentComponent { @Input() comment: noosfero.Comment; @@ -13,9 +14,10 @@ export class CommentComponent { showReply: boolean = false; - constructor(private $scope: ng.IScope) { - $scope.$on(PostCommentComponent.EVENT_COMMENT_RECEIVED, (event: ng.IAngularEvent, comment: noosfero.Comment) => { + constructor(postCommentEventService: PostCommentEventService, private $scope: ng.IScope) { + postCommentEventService.subscribe((comment: noosfero.Comment) => { this.showReply = false; + this.$scope.$apply(); }); } diff --git a/src/app/article/comment/comments.component.spec.ts b/src/app/article/comment/comments.component.spec.ts index 9c12ebf..17541af 100644 --- a/src/app/article/comment/comments.component.spec.ts +++ b/src/app/article/comment/comments.component.spec.ts @@ -17,14 +17,23 @@ describe("Components", () => { commentService.getByArticle = jasmine.createSpy("getByArticle") .and.returnValue(helpers.mocks.promiseResultTemplate({ data: comments })); - let providers = [ - helpers.createProviderToValue('CommentService', commentService), - helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), - helpers.createProviderToValue('SessionService', helpers.mocks.sessionWithCurrentUser({})) - ].concat(helpers.provideFilters("translateFilter")); + let emitEvent: Function; + let postCommentEventService = { + subscribe: (fn: Function) => { + emitEvent = fn; + } + }; let properties = { article: { id: 1 }, parent: null }; function createComponent() { + // postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["subscribe"]); + let providers = [ + helpers.createProviderToValue('CommentService', commentService), + helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), + helpers.createProviderToValue('SessionService', helpers.mocks.sessionWithCurrentUser({})), + new Provider('PostCommentEventService', { useValue: postCommentEventService }) + ].concat(helpers.provideFilters("translateFilter")); + return helpers.quickCreateComponent({ providers: providers, directives: [CommentsComponent], @@ -49,10 +58,9 @@ describe("Components", () => { }); it("update comments list when receive an reply", done => { - properties.parent = { id: 2 }; + properties.parent = { id: 3 }; createComponent().then(fixture => { - fixture.debugElement.getLocal("$rootScope").$emit(PostCommentComponent.EVENT_COMMENT_RECEIVED, { id: 1, reply_of: properties.parent }); - fixture.debugElement.getLocal("$rootScope").$apply(); + emitEvent({ id: 1, reply_of: { id: 3 } }); expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(3); done(); }); diff --git a/src/app/article/comment/comments.component.ts b/src/app/article/comment/comments.component.ts index 9392c0f..f1612e9 100644 --- a/src/app/article/comment/comments.component.ts +++ b/src/app/article/comment/comments.component.ts @@ -2,13 +2,14 @@ import { Inject, Input, Component, provide } from 'ng-forward'; import { PostCommentComponent } from "./post-comment/post-comment.component"; import { CommentService } from "../../../lib/ng-noosfero-api/http/comment.service"; import { CommentComponent } from "./comment.component"; +import { PostCommentEventService } from "./post-comment/post-comment-event.service"; @Component({ selector: 'noosfero-comments', templateUrl: 'app/article/comment/comments.html', directives: [PostCommentComponent, CommentComponent] }) -@Inject(CommentService, "$rootScope") +@Inject(CommentService, PostCommentEventService, "$scope") export class CommentsComponent { comments: noosfero.Comment[] = []; @@ -19,14 +20,7 @@ export class CommentsComponent { protected perPage = 5; protected total = 0; - constructor(protected commentService: CommentService, protected $rootScope: ng.IScope) { - $rootScope.$on(PostCommentComponent.EVENT_COMMENT_RECEIVED, (event: ng.IAngularEvent, comment: noosfero.Comment) => { - if ((!this.parent && !comment.reply_of) || (comment.reply_of && this.parent && comment.reply_of.id === this.parent.id)) { - if (!this.comments) this.comments = []; - this.comments.push(comment); - } - }); - } + constructor(protected commentService: CommentService, private postCommentEventService: PostCommentEventService, private $scope: ng.IScope) { } ngOnInit() { if (this.parent) { @@ -34,6 +28,13 @@ export class CommentsComponent { } else { this.loadNextPage(); } + this.postCommentEventService.subscribe((comment: noosfero.Comment) => { + if ((!this.parent && !comment.reply_of) || (comment.reply_of && this.parent && comment.reply_of.id === this.parent.id)) { + if (!this.comments) this.comments = []; + this.comments.push(comment); + this.$scope.$apply(); + } + }); } loadComments() { diff --git a/src/app/article/comment/post-comment/post-comment-event.service.spec.ts b/src/app/article/comment/post-comment/post-comment-event.service.spec.ts new file mode 100644 index 0000000..4a96dfc --- /dev/null +++ b/src/app/article/comment/post-comment/post-comment-event.service.spec.ts @@ -0,0 +1,26 @@ +import {PostCommentEventService} from "./post-comment-event.service"; +import {ComponentTestHelper, createClass} from '../../../../spec/component-test-helper'; +import * as helpers from "../../../../spec/helpers"; +import {Provider} from 'ng-forward'; +import {ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder'; + +describe("Services", () => { + describe("Comment Paragraph Event Service", () => { + let eventService: PostCommentEventService; + + beforeEach(() => { + eventService = new PostCommentEventService(); + eventService['eventEmitter'] = jasmine.createSpyObj("eventEmitter", ["next", "subscribe"]); + }); + + it('subscribe to post comment event', () => { + eventService.subscribe(() => { }); + expect(eventService['eventEmitter'].subscribe).toHaveBeenCalled(); + }); + + it('emit event when post comment', () => { + eventService.emit({}); + expect(eventService['eventEmitter'].next).toHaveBeenCalled(); + }); + }); +}); diff --git a/src/app/article/comment/post-comment/post-comment-event.service.ts b/src/app/article/comment/post-comment/post-comment-event.service.ts new file mode 100644 index 0000000..fdbf8f7 --- /dev/null +++ b/src/app/article/comment/post-comment/post-comment-event.service.ts @@ -0,0 +1,19 @@ +import {Injectable, EventEmitter} from "ng-forward"; + +@Injectable() +export class PostCommentEventService { + + private eventEmitter: EventEmitter; + + constructor() { + this.eventEmitter = new EventEmitter(); + } + + emit(comment: noosfero.Comment) { + this.eventEmitter.next(comment); + } + + subscribe(fn: (comment: noosfero.Comment) => void) { + this.eventEmitter.subscribe(fn); + } +} diff --git a/src/app/article/comment/post-comment/post-comment.component.spec.ts b/src/app/article/comment/post-comment/post-comment.component.spec.ts index 498a42b..93da538 100644 --- a/src/app/article/comment/post-comment/post-comment.component.spec.ts +++ b/src/app/article/comment/post-comment/post-comment.component.spec.ts @@ -11,11 +11,13 @@ describe("Components", () => { beforeEach(angular.mock.module("templates")); let commentService = jasmine.createSpyObj("commentService", ["createInArticle"]); + let postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["emit"]); let user = {}; let providers = [ new Provider('CommentService', { useValue: commentService }), new Provider('NotificationService', { useValue: helpers.mocks.notificationService }), - new Provider('SessionService', { useValue: helpers.mocks.sessionWithCurrentUser(user) }) + new Provider('SessionService', { useValue: helpers.mocks.sessionWithCurrentUser(user) }), + new Provider('PostCommentEventService', { useValue: postCommentEventService }) ].concat(helpers.provideFilters("translateFilter")); @Component({ selector: 'test-container-component', directives: [PostCommentComponent], template: htmlTemplate, providers: providers }) @@ -35,9 +37,8 @@ describe("Components", () => { helpers.createComponentFromClass(ContainerComponent).then(fixture => { let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); - component["$scope"].$emit = jasmine.createSpy("$emit"); component.save(); - expect(component["$scope"].$emit).toHaveBeenCalledWith(PostCommentComponent.EVENT_COMMENT_RECEIVED, jasmine.any(Object)); + expect(postCommentEventService.emit).toHaveBeenCalled(); done(); }); }); diff --git a/src/app/article/comment/post-comment/post-comment.component.ts b/src/app/article/comment/post-comment/post-comment.component.ts index 64a70ef..a9e23fd 100644 --- a/src/app/article/comment/post-comment/post-comment.component.ts +++ b/src/app/article/comment/post-comment/post-comment.component.ts @@ -2,12 +2,13 @@ import { Inject, Input, Component } from 'ng-forward'; import { CommentService } from "../../../../lib/ng-noosfero-api/http/comment.service"; import { NotificationService } from "../../../shared/services/notification.service"; import { SessionService } from "../../../login"; +import { PostCommentEventService } from "./post-comment-event.service"; @Component({ selector: 'noosfero-post-comment', templateUrl: 'app/article/comment/post-comment/post-comment.html' }) -@Inject(CommentService, NotificationService, "$scope", SessionService) +@Inject(CommentService, NotificationService, SessionService, PostCommentEventService) export class PostCommentComponent { public static EVENT_COMMENT_RECEIVED = "comment.received"; @@ -18,7 +19,10 @@ export class PostCommentComponent { comment = {}; private currentUser: noosfero.User; - constructor(private commentService: CommentService, private notificationService: NotificationService, private $scope: ng.IScope, private session: SessionService) { + constructor(private commentService: CommentService, + private notificationService: NotificationService, + private session: SessionService, + private postCommentEventService: PostCommentEventService) { this.currentUser = this.session.currentUser(); } @@ -27,7 +31,7 @@ export class PostCommentComponent { this.comment.reply_of_id = this.parent.id; } this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult) => { - this.$scope.$emit(PostCommentComponent.EVENT_COMMENT_RECEIVED, result.data); + this.postCommentEventService.emit(result.data); this.comment.body = ""; this.notificationService.success({ title: "comment.post.success.title", message: "comment.post.success.message" }); }); -- libgit2 0.21.2