fix like test

This commit is contained in:
Eugenio Romano 2018-05-21 15:15:02 +01:00
parent 1c7f267c63
commit ff5d02b5b7

View File

@ -19,22 +19,23 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LikeComponent } from './like.component'; import { LikeComponent } from './like.component';
import { setupTestBed } from '../../core/testing'; import { setupTestBed } from '../../core/testing';
import { ContentTestingModule } from '../testing/content.testing.module'; import { ContentTestingModule } from '../testing/content.testing.module';
import { Observable } from 'rxjs/Observable';
declare let jasmine: any; import { RatingService } from './services/rating.service';
describe('Like component', () => { describe('Like component', () => {
let component: any; let component: any;
let fixture: ComponentFixture<LikeComponent>; let fixture: ComponentFixture<LikeComponent>;
let element: HTMLElement; let element: HTMLElement;
let service: RatingService;
setupTestBed({ setupTestBed({
imports: [ContentTestingModule] imports: [ContentTestingModule]
}); });
beforeEach(() => { beforeEach(() => {
jasmine.Ajax.install();
fixture = TestBed.createComponent(LikeComponent); fixture = TestBed.createComponent(LikeComponent);
service = TestBed.get(RatingService);
element = fixture.nativeElement; element = fixture.nativeElement;
component = fixture.componentInstance; component = fixture.componentInstance;
@ -42,25 +43,20 @@ describe('Like component', () => {
fixture.detectChanges(); fixture.detectChanges();
}); });
afterEach(() => {
fixture.destroy();
jasmine.Ajax.uninstall();
});
function simulateResponseWithLikes(numberOfRatings: number) { function simulateResponseWithLikes(numberOfRatings: number) {
jasmine.Ajax.requests.mostRecent().respondWith({ spyOn(service, 'getRating').and.returnValue(Observable.of({
status: 200, contentType: 'json', entry: {
responseText: { id: 'likes',
entry: { id: 'likes', aggregate: { numberOfRatings } } aggregate: { numberOfRatings }
} }
}); }));
} }
it('should load the likes by default on onChanges', async(() => { it('should load the likes by default on onChanges', async(() => {
component.ngOnChanges();
simulateResponseWithLikes(2); simulateResponseWithLikes(2);
component.ngOnChanges();
fixture.whenStable().then(() => { fixture.whenStable().then(() => {
fixture.detectChanges(); fixture.detectChanges();
expect(element.querySelector('#adf-like-counter').innerHTML).toBe('2'); expect(element.querySelector('#adf-like-counter').innerHTML).toBe('2');
@ -68,6 +64,8 @@ describe('Like component', () => {
})); }));
it('should increase the number of likes when clicked', async(() => { it('should increase the number of likes when clicked', async(() => {
simulateResponseWithLikes(3);
component.likesCounter = 2; component.likesCounter = 2;
let likeButton: any = element.querySelector('#adf-like-test-id'); let likeButton: any = element.querySelector('#adf-like-test-id');
@ -78,10 +76,11 @@ describe('Like component', () => {
expect(element.querySelector('#adf-like-counter').innerHTML).toBe('3'); expect(element.querySelector('#adf-like-counter').innerHTML).toBe('3');
}); });
simulateResponseWithLikes(3);
})); }));
it('should decrease the number of likes when clicked and is already liked', async(() => { it('should decrease the number of likes when clicked and is already liked', async(() => {
simulateResponseWithLikes(1);
component.likesCounter = 2; component.likesCounter = 2;
component.isLike = true; component.isLike = true;
@ -93,6 +92,5 @@ describe('Like component', () => {
expect(element.querySelector('#adf-like-counter').innerHTML).toBe('1'); expect(element.querySelector('#adf-like-counter').innerHTML).toBe('1');
}); });
simulateResponseWithLikes(1);
})); }));
}); });