import {TestBed} from '@angular/core/testing'; import {MicroLessonCommunicationService} from './micro-lesson-communication.service'; import {PostMessageBridgeFactory} from 'ngx-post-message'; import {GameActionsService} from '../game-actions.service'; import {MicroLessonMetricsService} from '../metrics/micro-lesson-metrics.service'; import { Collectible, CollectiblesOxBridge, CommunicationOxService, GameCompleteOxBridge, MetricsOxBridge, MicroLessonMetrics, SettingsInfoOxBridge, SettingsOx, SettingsService, UserInfoOx } from 'ox-core'; import {AppInfoOxService} from '../app-info-ox.service'; import {EventEmitter} from '@angular/core'; import {CollectiblesService} from '../collectibles.service'; describe('MicroLessonCommunicationService', () => { const wumboxCommunicationServiceSpy = jasmine.createSpyObj('WumboxCommunication', ['sendMessage']); const gameActionsServiceSpy = jasmine.createSpyObj>('GameActionsService', ['goToResults']); const metricsServiceSpy = jasmine.createSpyObj>('MicroLessonMetricsService', ['gameStart']); const settingsServiceSpy = jasmine.createSpyObj('SettingsService', ['setMusic']); const appInfoServiceSpy = jasmine.createSpyObj('AppInfoOxService', ['getInitialLevel']); const collectibleServiceSpy = jasmine.createSpyObj('AppInfoOxService', ['addCollectible']); let microLessonComunicationService: MicroLessonCommunicationService; // iPostMessageBridgeSpy.makeBridge.and.returnValue(iPostMessageBridgeSpy); beforeEach(() => { gameActionsServiceSpy.goToResults = new EventEmitter(); gameActionsServiceSpy.microLessonCompleted = new EventEmitter(); metricsServiceSpy.currentMetrics = new MicroLessonMetrics(); metricsServiceSpy.updateMetrics = new EventEmitter>(); appInfoServiceSpy.userInfo = new UserInfoOx('test', false, 'test'); settingsServiceSpy.currentSettings = new SettingsOx(); collectibleServiceSpy.currentCollectibles = [new Collectible('test', 2)]; // gameActionsServiceSpy.microLessonCompleted = new EventEmitter(); TestBed.configureTestingModule({ providers: [ PostMessageBridgeFactory, {provide: CommunicationOxService, useValue: wumboxCommunicationServiceSpy}, {provide: GameActionsService, useValue: gameActionsServiceSpy}, {provide: MicroLessonMetricsService, useValue: metricsServiceSpy}, {provide: SettingsService, useValue: settingsServiceSpy}, {provide: AppInfoOxService, useValue: appInfoServiceSpy}, {provide: CollectiblesService, useValue: collectibleServiceSpy}, MicroLessonCommunicationService ] }); microLessonComunicationService = TestBed.get(MicroLessonCommunicationService); }); it('should be created', () => { expect(microLessonComunicationService).toBeTruthy(); }); it('should send messages to wumbox communication service with appinfo and settings when go to results', () => { gameActionsServiceSpy.goToResults.emit(); expect(wumboxCommunicationServiceSpy.sendMessage).toHaveBeenCalledWith(GameCompleteOxBridge, appInfoServiceSpy.userInfo); expect(wumboxCommunicationServiceSpy.sendMessage).toHaveBeenCalledWith(SettingsInfoOxBridge, settingsServiceSpy.currentSettings); }); it('should send metrics and collectibles to wumbox when game is completed', () => { gameActionsServiceSpy.microLessonCompleted.emit(); expect(wumboxCommunicationServiceSpy.sendMessage).toHaveBeenCalledWith(MetricsOxBridge, JSON.stringify(metricsServiceSpy.currentMetrics)); expect(wumboxCommunicationServiceSpy.sendMessage).toHaveBeenCalledWith(CollectiblesOxBridge, JSON.stringify(collectibleServiceSpy.currentCollectibles)); }); it('should send metrics to wumbox after each update in metrics', () => { metricsServiceSpy.updateMetrics.emit(); expect(wumboxCommunicationServiceSpy.sendMessage).toHaveBeenCalledWith(MetricsOxBridge, JSON.stringify(metricsServiceSpy.currentMetrics)); }); });