import {TestBed} from '@angular/core/testing'; import {ProgressService} from './progress.service'; import {PostMessageBridgeFactory} from 'ngx-post-message'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {GameActionsService} from './game-actions.service'; import {answerServiceSpy, appInfoSpy, feedBackServiceSpy, mockMicroLessonConfiguration} from '../test-files/test-declarations.spec'; import {AppInfoOxService} from './app-info-ox.service'; import {LevelService} from './level.service'; import {AnswerService} from './answer.service'; import {FeedbackOxService} from './feedback-ox.service'; describe('ProgressService', () => { let progressService: ProgressService; let gameActions: GameActionsService; beforeEach(() => { TestBed.configureTestingModule({ providers: [ {provide: AppInfoOxService, useValue: appInfoSpy}, {provide: AnswerService, useValue: answerServiceSpy}, {provide: FeedbackOxService, useValue: feedBackServiceSpy}, PostMessageBridgeFactory, LevelService ], imports: [HttpClientTestingModule] }); progressService = TestBed.get(ProgressService); gameActions = TestBed.get(GameActionsService); TestBed.get(LevelService).currentLevel.next(1); }); it('should be created', () => { expect(progressService).toBeTruthy(); }); it('Values should be clear after start game', () => { validateValuesAsStart(gameActions.startGame); }); it('Values should be clear after restart game', () => { validateValuesAsStart(gameActions.restartGame); }); it('After correct answer should add 1 and send changeInProgress event', () => { validateAddOne(gameActions.checkedAnswer, true); }); it('After surrender should add 1 and send changeInProgress event', () => { validateAddOne(TestBed.get(FeedbackOxService).surrenderEnd); }); function validateAddOne(event, param?) { spyOn(progressService.changeInProgress, 'emit'); gameActions.startGame.emit(); event.emit(param); expect(progressService['currentCompleted']).toBe(1); expect(progressService.changeInProgress.emit).toHaveBeenCalledWith(Math.round(100 / 3)); } function validateValuesAsStart(event) { spyOn(progressService.startProgress, 'emit'); event.emit(); expect(progressService.startProgress.emit).toHaveBeenCalledTimes(1); expect(progressService['currentCompleted']).toBe(0); expect(progressService['totalToComplete']).toBe(mockMicroLessonConfiguration.types.find( x => x.mode === 'challenges').value); } });