import {fakeAsync, TestBed, tick} from '@angular/core/testing'; import {CollectiblesService} from './collectibles.service'; import {Collectible} from 'ox-core'; import {GameActionsService} from './game-actions.service'; import {Observable, timer} from 'rxjs'; describe('CollectiblesService', () => { let collectiblesService: CollectiblesService; let gameActions: GameActionsService; beforeEach(() => { TestBed.configureTestingModule({providers: [GameActionsService]}); collectiblesService = TestBed.get(CollectiblesService); gameActions = TestBed.get(GameActionsService); }); it('should be created', () => { expect(collectiblesService).toBeTruthy(); }); it('should clear collectibles after startGame', () => { gameActions.startGame.emit(); expect(collectiblesService.currentCollectibles.length).toBe(0); }); it('should clear collectibles after restart', () => { gameActions.startGame.emit(); collectiblesService.addCollectible(new Collectible('a', 1)); gameActions.restartGame.emit(); expect(collectiblesService.currentCollectibles.length).toBe(0); }); it('add collectible add the collectible to the list', () => { gameActions.startGame.emit(); const collectible = new Collectible('a', 1); collectiblesService.addCollectible(collectible); expect(collectiblesService.currentCollectibles[0]).toEqual(collectible); }); it('add temp collectible only add the collectible if event add is trigger, ' + 'and clear if the negative event trigger', fakeAsync(() => { gameActions.startGame.emit(); const collectible = new Collectible('a', 1); collectiblesService.addTempCollectible(collectible, timer(1000), timer(1500)); tick(1500); // should be added expect(collectiblesService.currentCollectibles[0]).toEqual(collectible); collectiblesService.addTempCollectible(collectible, timer(2000), timer(1500)); // shouldn't be added tick(2500); expect(collectiblesService.currentCollectibles.length).toEqual(1); })); });