From ff5d02b5b7109b9df7eb5a0f194980c42a117342 Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Mon, 21 May 2018 15:15:02 +0100 Subject: [PATCH] fix like test --- .../social/like.component.spec.ts | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/content-services/social/like.component.spec.ts b/lib/content-services/social/like.component.spec.ts index 4371edba22..65c32fb314 100644 --- a/lib/content-services/social/like.component.spec.ts +++ b/lib/content-services/social/like.component.spec.ts @@ -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; 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); })); });