import {discardPeriodicTasks, fakeAsync, flush, TestBed, tick} from '@angular/core/testing'; import {EndGameService} from './end-game.service'; import {PostMessageBridgeFactory} from 'ngx-post-message'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {AppInfoOxService} from './app-info-ox.service'; import { MicroLessonInfoOx} from 'ox-core'; import {GameActionsService} from './game-actions.service'; import {FeedbackOxService} from './feedback-ox.service'; import {Test} from 'tslint'; import {AnswerService} from './answer.service'; import {EventEmitter} from '@angular/core'; import {BehaviorSubject} from 'rxjs'; import {LevelService} from './level.service'; import {MicroLessonLevelConfiguration} from '../../../../ox-core/src/lib/models/micro-lesson-info-ox'; describe('EndGameService', () => { const appInfoSpy = jasmine.createSpyObj('AppInfoService', ['getMetricsTable']); const configuration = new MicroLessonLevelConfiguration(); configuration.types = [{ mode: 'challenges', value: 3}]; const info = new MicroLessonInfoOx([configuration], 1, 2, '', [], undefined); appInfoSpy.microLessonInfo = info; let endGameService: EndGameService; let gameActionsService: GameActionsService; const answerServiceSpy: AnswerService = jasmine.createSpyObj>('AnswerService', ['onTryAnswer']); const feedBackServiceSpy = jasmine.createSpyObj('FeedBackService', ['doesntMatter']); feedBackServiceSpy.endFeedback = new EventEmitter(); feedBackServiceSpy.surrenderEnd = new EventEmitter(); beforeEach(() => { TestBed.configureTestingModule({ providers: [ PostMessageBridgeFactory, LevelService, GameActionsService, {provide: FeedbackOxService, useValue: feedBackServiceSpy}, {provide: AnswerService, useValue: answerServiceSpy}, {provide: AppInfoOxService, useValue: appInfoSpy}, ], imports: [HttpClientTestingModule] }); endGameService = TestBed.get(EndGameService); gameActionsService = TestBed.get(GameActionsService); }); it('should be created', () => { expect(endGameService).toBeTruthy(); }); it('challenges complete is 0 after start game', () => { mockStartGame(); expect(endGameService.getChallengeCompleteCount()).toBe(0); }); it('challenge count raise after correct answer checked', () => { mockStartGame(); const originalVal = endGameService.getChallengeCompleteCount(); gameActionsService.checkedAnswer.emit(true); expect(endGameService.getChallengeCompleteCount()).toBe(originalVal + 1); }); it('challenge count is 0 after restartGame', () => { mockStartGame(); const originalVal = endGameService.getChallengeCompleteCount(); gameActionsService.checkedAnswer.emit(true); expect(endGameService.getChallengeCompleteCount()).toBe(originalVal + 1); gameActionsService.restartGame.emit(); expect(endGameService.getChallengeCompleteCount()).toBe(0); }); it('challenge count raise after surrender', () => { mockStartGame(); const originalVal = endGameService.getChallengeCompleteCount(); gameActionsService.surrender.emit(); expect(endGameService.getChallengeCompleteCount()).toBe(originalVal + 1); }); it('challenge count does not raise after incorrect answer checked', () => { mockStartGame(); const originalVal = endGameService.getChallengeCompleteCount(); gameActionsService.checkedAnswer.emit(false); expect(endGameService.getChallengeCompleteCount()).toBe(originalVal); }); it('Game is ended when total challenges =< doneChalleges', () => { mockStartGame(); endGameService['challengesCompleted'] = 2; expect(endGameService['gameIsEnded']()).toBeFalsy(); endGameService['challengesCompleted'] = 3; expect(endGameService['gameIsEnded']()).toBeTruthy(); endGameService['challengesCompleted'] = 5; expect(endGameService['gameIsEnded']()).toBeTruthy(); }); it('Game is just ended after end feed back or surrender event are fired, not after checked answer or surrender', () => { mockStartGame(); gameActionsService.checkedAnswer.emit(false); let mustSendComplete: boolean; gameActionsService.microLessonCompleted.subscribe(z => { expect(mustSendComplete).toBeTruthy(); }); for (let i = 0; i < configuration.types.find( x => x.mode === 'challenges').value; i++) { mustSendComplete = false; gameActionsService.checkedAnswer.emit(true); mustSendComplete = i + 1 === configuration.types.find( x => x.mode === 'challenges').value; feedBackServiceSpy.endFeedback.emit(); } }); function mockStartGame() { gameActionsService.startGame.emit(); TestBed.get(LevelService).currentLevel.next(1); } });