import {TestBed} from '@angular/core/testing'; import {ScreenOxService} from './screen-ox.service'; import {PostMessageBridgeFactory} from 'ngx-post-message'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {GameActionsService} from './game-actions.service'; import {PreloaderOxService, ScreenTypeOx} from 'ox-core'; import {VideoService} from './video.service'; import {Component, Injector, NgModule} from '@angular/core'; describe('ScreenOxService', () => { let screeenService: ScreenOxService; let gameActions: GameActionsService; let videoService: VideoService; beforeEach(() => { TestBed.configureTestingModule({ declarations: [MockComponent], providers: [ Injector, PostMessageBridgeFactory, PreloaderOxService, GameActionsService, VideoService], imports: [HttpClientTestingModule, MockModule] }); screeenService = TestBed.get(ScreenOxService); gameActions = TestBed.get(GameActionsService); videoService = TestBed.get(VideoService); screeenService.parentScreen = TestBed.createComponent(MockComponent).elementRef.nativeElement; }); it('should be created', () => { expect(screeenService).toBeTruthy(); }); it('After start game show game is called', () => { afterEventMethodIsCalled(gameActions.startGame, 'showGame'); }); it('After restart game show game is called', () => { afterEventMethodIsCalled(gameActions.restartGame, 'showGame'); }); it('After resume game resume game is called', () => { afterEventMethodIsCalled(gameActions.resumeGame, 'resumeGame'); }); it('After goToLevelMenu showLevelMenu is called', () => { afterEventMethodIsCalled(gameActions.goToLevelMenu, 'showLevelMenu'); }); it('After goToGamePreview showGamePreview is called', () => { afterEventMethodIsCalled(gameActions.goToGamePreview, 'showGamePreview'); }); it('After goToResults showGamePreview is called', () => { afterEventMethodIsCalled(gameActions.goToResults, 'showGameComplete'); }); it('After goToInGameMenu showInGameMenu is called', () => { afterEventMethodIsCalled(gameActions.goToInGameMenu, 'showInGameMenu'); }); it('After playVideo showInGameMenu is called', () => { afterEventMethodIsCalled(videoService.playVideo, 'showVideo'); }); function afterEventMethodIsCalled(event, method) { spyOn(screeenService as any, method); event.emit(); expect(screeenService[method]).toHaveBeenCalled(); } it('Show game call correctly change main screen', () => { spyOn(screeenService as any, 'changeMainScreen'); screeenService['showGame'](); expect(screeenService['changeMainScreen']).toHaveBeenCalledWith('micro-lesson', ScreenTypeOx.Game); }); it('is showing screen returns true if the screen is actually at the front (it means, ' + 'if there is a pop up it will return false if you ask for the main screen)', () => { screeenService['mainScreen'] = {element: undefined, type: ScreenTypeOx.Game}; screeenService['popUpScreen'] = {element: undefined, type: ScreenTypeOx.InGameMenu}; expect(screeenService.isShowingScreen(ScreenTypeOx.Game)).toBeFalsy(); expect(screeenService.isShowingScreen(ScreenTypeOx.InGameMenu)).toBeTruthy(); screeenService['popUpScreen'] = undefined; expect(screeenService.isShowingScreen(ScreenTypeOx.Game)).toBeTruthy(); }); it('is showing main screen returns true without checking about the pop up', () => { screeenService['mainScreen'] = {element: undefined, type: ScreenTypeOx.Game}; screeenService['popUpScreen'] = {element: undefined, type: ScreenTypeOx.InGameMenu}; expect(screeenService.isShowingMainScreen(ScreenTypeOx.Game)).toBeTruthy(); }); it('After screen ready event the screen is correctly set as true in the map', () => { expect(screeenService.screensReady.get(ScreenTypeOx.Game)).toBeFalsy(); TestBed.get(PreloaderOxService).screenReady.emit(ScreenTypeOx.Game); expect(screeenService.screensReady.get(ScreenTypeOx.Game)).toBeTruthy(); }); it('Show loading calls correctly changeMainScreen', () => { callChangeChangeMainScreenWith('loading-assets', ScreenTypeOx.Loading, () => screeenService.showLoading()); }); it('Show showNotInWumboxScreen calls correctly changeMainScreen', () => { callChangeChangeMainScreenWith('not-in-wumbox', ScreenTypeOx.NotInWumbox, () => screeenService.showNotInWumboxScreen()); }); it('Show showNotInWumboxScreen calls correctly changeMainScreen', () => { callChangeChangeMainScreenWith('not-in-wumbox', ScreenTypeOx.NotInWumbox, () => screeenService.showNotInWumboxScreen()); }); function callChangeChangeMainScreenWith(tag, screen, after = () => { }) { spyOn(screeenService as any, 'changeMainScreen'); after(); expect(screeenService['changeMainScreen']).toHaveBeenCalledWith(tag, screen); } // Todo find a way to make this task actually call is define it('MOCK --- isDefine --- MOCK is true if the element is defined and false otherwise WIP', () => { const mock = TestBed.createComponent(MockComponent); expect(mock.elementRef.nativeElement.constructor !== HTMLElement).toBeTruthy(); mock.destroy(); }); it('Change main screen correctly set new screen', () => { const screenToSet = ScreenTypeOx.Game; screeenService['changeMainScreen']('video-ox', ScreenTypeOx.Video); expect(screeenService.isShowingMainScreen(screenToSet)).toBeFalsy(); screeenService['changeMainScreen']('game-ox', screenToSet); expect(screeenService.isShowingMainScreen(screenToSet)).toBeTruthy(); }); function setAndCheckGameAndInGameMenuScreens() { spyOn(screeenService.screenChanges, 'emit'); const screenToSet = ScreenTypeOx.InGameMenu; screeenService['changeMainScreen']('game-ox', ScreenTypeOx.Game); screeenService['addPopUp']('in-game-menu-ox', screenToSet); expect(screeenService.isShowingScreen(screenToSet)).toBeTruthy(); expect(screeenService.isShowingScreen(ScreenTypeOx.Game)).toBeFalsy(); expect(screeenService['mainScreen'].element.style.display).toBe('none'); expect(screeenService.screenChanges.emit).toHaveBeenCalledTimes(2); } it('addPopUp correctly set new pop up and set main screen display to none and emit screenChanges', () => { setAndCheckGameAndInGameMenuScreens(); }); it('remove pop up correctly removes pop up and set main screen display to empty and emit screenChanges', () => { setAndCheckGameAndInGameMenuScreens(); screeenService['removePopUp'](screeenService['popUpScreen'].element, true); expect(screeenService['mainScreen'].element.style.display).toBe(''); expect(screeenService.screenChanges.emit).toHaveBeenCalledTimes(3); }); it('if there is not parent screen in ngOnInit it throws error', () => { expect(function () { screeenService.parentScreen = undefined; screeenService.ngOnInit(); }).toThrowError(); }); }); @Component({ selector: 'ml-mock-component', template: '

here a valid vlaue

' }) class MockComponent { constructor() { } } @NgModule({ imports: [], exports: [] }) export class MockModule { }