Skip to content

Commit

Permalink
Add comment component test
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien committed Sep 21, 2017
1 parent 819e5a2 commit b48ccb2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
42 changes: 42 additions & 0 deletions src/components/comments.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { DatePipe } from '@angular/common';

import { Comment } from './comments';

describe('Comment component', () => {
let comp: Comment;
let fixture: ComponentFixture<Comment>;
let commentContent, commentAuthor, commentDate: HTMLElement;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [Comment],
});

fixture = TestBed.createComponent(Comment);

comp = fixture.componentInstance;

commentContent = fixture.debugElement.query(By.css('.comment-body')).nativeElement;
commentAuthor = fixture.debugElement.query(By.css('.comment-author')).nativeElement;
commentDate = fixture.debugElement.query(By.css('.comment-date')).nativeElement;
});

it('should display author, date and format content', () => {
let comment = {
creation_date: '01/01/2000',
author_name: 'Dynausor',
text: { 'mime-type': 'text/plain', 'data': 'Roaaaar\nRoooar\nRoooar' }
};

comp.comment = comment;

fixture.detectChanges();

expect(commentContent.innerHTML).toEqual(comment.text.data.replace(/\n/g, '<br>'));
expect(commentAuthor.innerHTML).toEqual(comment.author_name);
expect(commentDate.innerHTML).toEqual(new DatePipe('en').transform(comment.creation_date));
});
});
6 changes: 3 additions & 3 deletions src/components/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Comment {

formatText(text) {
if (text['mime-type'] == 'text/plain') {
return text.data.replace(/\n/g, '<br/>');
return text.data.replace(/\n/g, '<br>');
} else {
return text.data;
}
Expand All @@ -48,11 +48,11 @@ export class CommentAdd {
@Input() path: string;
@Output() onCreate: EventEmitter<boolean> = new EventEmitter<boolean>();
error: string;

constructor(
private services: Services,
) { }

add(form: NgForm) {
this.services.comments.add(this.path, form.value).subscribe(res => {
this.onCreate.next(true);
Expand Down

0 comments on commit b48ccb2

Please sign in to comment.