Commit 618b2ae00d56b614c90bd9bff7a71aa2d192d3e2
1 parent
56a68d9b
Exists in
master
and in
1 other branch
Add tests to post comment component
Showing
3 changed files
with
71 additions
and
5 deletions
Show diff stats
... | ... | @@ -0,0 +1,66 @@ |
1 | +import {Provider, provide, Component} from 'ng-forward'; | |
2 | +import * as helpers from "../../../spec/helpers"; | |
3 | + | |
4 | +import {PostCommentComponent} from './post-comment.component'; | |
5 | + | |
6 | +const htmlTemplate: string = '<noosfero-post-comment [article]="ctrl.article" [reply-of]="ctrl.comment"></noosfero-post-comment>'; | |
7 | + | |
8 | +describe("Components", () => { | |
9 | + describe("Post Comment Component", () => { | |
10 | + | |
11 | + beforeEach(angular.mock.module("templates")); | |
12 | + | |
13 | + let commentService = jasmine.createSpyObj("commentService", ["createInArticle"]); | |
14 | + let providers = [ | |
15 | + new Provider('CommentService', { useValue: commentService }), | |
16 | + new Provider('NotificationService', { useValue: helpers.mocks.notificationService }) | |
17 | + ].concat(helpers.provideFilters("translateFilter")); | |
18 | + | |
19 | + @Component({ selector: 'test-container-component', directives: [PostCommentComponent], template: htmlTemplate, providers: providers }) | |
20 | + class ContainerComponent { | |
21 | + article = { id: 1 }; | |
22 | + comment = { id: 2 }; | |
23 | + } | |
24 | + | |
25 | + it("render the post comment form", done => { | |
26 | + helpers.createComponentFromClass(ContainerComponent).then(fixture => { | |
27 | + expect(fixture.debugElement.queryAll("form").length).toEqual(1); | |
28 | + done(); | |
29 | + }); | |
30 | + }); | |
31 | + | |
32 | + it("emit an event when create comment", done => { | |
33 | + helpers.createComponentFromClass(ContainerComponent).then(fixture => { | |
34 | + let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | |
35 | + commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); | |
36 | + component["$rootScope"].$emit = jasmine.createSpy("$emit"); | |
37 | + component.save(); | |
38 | + expect(component["$rootScope"].$emit).toHaveBeenCalledWith(PostCommentComponent.EVENT_COMMENT_RECEIVED, jasmine.any(Object)); | |
39 | + done(); | |
40 | + }); | |
41 | + }); | |
42 | + | |
43 | + it("notify success when create comment", done => { | |
44 | + helpers.createComponentFromClass(ContainerComponent).then(fixture => { | |
45 | + let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | |
46 | + commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); | |
47 | + component["notificationService"].success = jasmine.createSpy("success"); | |
48 | + component.save(); | |
49 | + expect(component["notificationService"].success).toHaveBeenCalled(); | |
50 | + done(); | |
51 | + }); | |
52 | + }); | |
53 | + | |
54 | + it("set the reply id when reply to a comment", done => { | |
55 | + helpers.createComponentFromClass(ContainerComponent).then(fixture => { | |
56 | + let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | |
57 | + component.comment = <any>{ reply_of_id: null }; | |
58 | + component.replyOf = <any>{ id: 10 }; | |
59 | + component.save(); | |
60 | + expect(component.comment.reply_of_id).toEqual(component.replyOf.id); | |
61 | + done(); | |
62 | + }); | |
63 | + }); | |
64 | + | |
65 | + }); | |
66 | +}); | ... | ... |
src/app/article/comment/post-comment.component.ts
... | ... | @@ -12,14 +12,14 @@ export class PostCommentComponent { |
12 | 12 | public static EVENT_COMMENT_RECEIVED = "comment.received"; |
13 | 13 | |
14 | 14 | @Input() article: noosfero.Article; |
15 | - comment: noosfero.Comment; | |
16 | - | |
17 | 15 | @Input() replyOf: noosfero.Comment; |
18 | 16 | |
17 | + comment: noosfero.Comment; | |
18 | + | |
19 | 19 | constructor(private commentService: CommentService, private notificationService: NotificationService, private $rootScope: ng.IScope) { } |
20 | 20 | |
21 | 21 | save() { |
22 | - if (this.replyOf) { | |
22 | + if (this.replyOf && this.comment) { | |
23 | 23 | this.comment.reply_of_id = this.replyOf.id; |
24 | 24 | } |
25 | 25 | this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult<noosfero.Comment>) => { | ... | ... |
src/spec/mocks.ts
... | ... | @@ -75,7 +75,7 @@ export var mocks = { |
75 | 75 | }, |
76 | 76 | commentService: { |
77 | 77 | getByArticle: (article: noosfero.Article) => { |
78 | - return Promise.resolve({data: {}}); | |
78 | + return Promise.resolve({ data: {} }); | |
79 | 79 | } |
80 | 80 | }, |
81 | 81 | sessionWithCurrentUser: (user: any) => { |
... | ... | @@ -118,6 +118,6 @@ export var mocks = { |
118 | 118 | translate: (text: string) => { return text; } |
119 | 119 | }, |
120 | 120 | notificationService: { |
121 | - | |
121 | + success: () => { } | |
122 | 122 | } |
123 | 123 | }; | ... | ... |