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