import {TestBed} from '@angular/core/testing'; import {HintService} from './hint.service'; import {PostMessageBridgeFactory} from 'ngx-post-message'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {GameActionsService} from './game-actions.service'; import {AnswerService} from './answer.service'; import {EventEmitter} from '@angular/core'; describe('HintService', () => { let hintService: HintService; let gameActions: GameActionsService; const answerServiceSpy = jasmine.createSpyObj>('AnswerService', ['onTryAnswer']); answerServiceSpy.tryAnswer = new EventEmitter(); beforeEach(() => { TestBed.configureTestingModule({ providers: [PostMessageBridgeFactory, GameActionsService, {provide: AnswerService, useValue: answerServiceSpy}], imports: [HttpClientTestingModule] }); hintService = TestBed.get(HintService); gameActions = TestBed.get(GameActionsService); }); it('should be created', () => { expect(hintService).toBeTruthy(); }); it('Hint should disabled after try', () => { answerServiceSpy.tryAnswer.emit(); availableHintStatusShouldBe(false); }); it('Hint should disabled after checkanswer true', () => { gameActions.checkedAnswer.emit(true); availableHintStatusShouldBe(false); }); it('Hint should disabled after microLessonCompleted', () => { gameActions.microLessonCompleted.emit(); availableHintStatusShouldBe(false); }); it('Hint should disabled after surrender', () => { gameActions.surrender.emit(); availableHintStatusShouldBe(false); }); it('Hint current uses is 0 after show next challenge, and check if available', () => { hintService['_currentUses'] = 3; spyOn(hintService, 'checkHintAvailable'); gameActions.showNextChallenge.emit(); expect(hintService['_currentUses']).toBe(0); expect(hintService.checkHintAvailable).toHaveBeenCalled(); }); it('Hint should check available after wrong answer', () => { spyOn(hintService, 'checkHintAvailable'); gameActions.checkedAnswer.emit(false); expect(hintService.checkHintAvailable).toHaveBeenCalled(); }); it('Hint should check available after action to answer', () => { spyOn(hintService, 'checkHintAvailable'); gameActions.actionToAnswer.emit(); expect(hintService.checkHintAvailable).toHaveBeenCalled(); }); it('Hint should raise uses count after show hint', () => { gameActions.showNextChallenge.emit(); // set the current uses to zero spyOn(hintService, 'checkHintAvailable'); gameActions.showHint.emit(); expect(hintService['_currentUses']).toBe(1); }); it('Hint Is not Available if current uses >= uses per challenge', () => { hintService.usesPerChallenge = 3; hintService['_currentUses'] = 3; expect(hintService['hintIsAvailable']()).toBeFalsy(); hintService['_currentUses'] = 5; expect(hintService['hintIsAvailable']()).toBeFalsy(); }); it('Hint Is Available if current uses < uses per challenge', () => { hintService.usesPerChallenge = 3; hintService['_currentUses'] = 2; expect(hintService['hintIsAvailable']()).toBeTruthy(); }); function availableHintStatusShouldBe(correctStatus: boolean) { expect(hintService.hintAvailable.value).toBe(correctStatus); } });