diff --git a/src/lib/debug-state.service.spec.ts b/src/lib/debug-state.service.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..2aacaf58160ef9d5f3e72b2626cac934842c75bc --- /dev/null +++ b/src/lib/debug-state.service.spec.ts @@ -0,0 +1,52 @@ +import { marbles } from 'rxjs-marbles'; + +import { DebugStateService } from './debug-state.service'; + +describe('DebugStateService', () => { + let debugStateService: DebugStateService; + + beforeEach(() => { + debugStateService = new DebugStateService(); + }); + + it('should construct', () => { + expect(debugStateService).toBeDefined(); + }); + + it('should ne enabled by default', () => { + expect(debugStateService.enabled).toBeTruthy(); + }); + + it('should be disabled by toggle', () => { + debugStateService.toggle(); + expect(debugStateService.enabled).toBeFalsy(); + }); + + it('should be enabled again', () => { + debugStateService.toggle(); + debugStateService.toggle(); + expect(debugStateService.enabled).toBeTruthy(); + }); + + it( + 'should emit state changed', + marbles(m => { + m.scheduler.schedule( + () => debugStateService.toggle(), + m.scheduler.createTime('-|') + ); + + m.scheduler.schedule( + () => debugStateService.toggle(), + m.scheduler.createTime('---|') + ); + + m.expect(debugStateService.enabled$).toBeObservable('tf-t', { + f: false, + t: true, + }); + + expect(debugStateService.enabled).toBeTruthy(); + }) + ); +}); diff --git a/src/lib/debug-state.service.ts b/src/lib/debug-state.service.ts index a0b9b4453bcb0813c983e980b5c9af2a9a65ec2a..1a5c072f4796ae9d11294e2337d7fb42ea929e75 100644 --- a/src/lib/debug-state.service.ts +++ b/src/lib/debug-state.service.ts @@ -1,31 +1,18 @@ import { Injectable } from '@angular/core'; -import { BehaviorSubject } from 'rxjs'; +import { BehaviorSubject, Observable } from 'rxjs'; import { distinctUntilChanged, map } from 'rxjs/operators'; -// TODO injecter -export const environment = { - production: false, - apiURL: 'http://baliste-api.dvp', - api: 'http://127.0.0.101:8040', - geonames_api: 'http://api.geonames.org/searchJSON', -}; - export const enum DebugState { UNAVAILABLE = -1, DISABLED = 0, ENABLED = 1, } -@Injectable({ providedIn: 'root' }) +@Injectable() export class DebugStateService { - private readonly state$ = new BehaviorSubject<DebugState>( - environment.production ? DebugState.UNAVAILABLE : DebugState.ENABLED - ); + private readonly state$: BehaviorSubject<DebugState>; - public readonly enabled$ = this.state$.pipe( - map(state => state === DebugState.ENABLED), - distinctUntilChanged() - ); + public readonly enabled$: Observable<boolean>; public get state(): DebugState { return this.state$.getValue(); @@ -39,6 +26,17 @@ export class DebugStateService { return this.state === DebugState.ENABLED; } + public constructor(production: boolean = false) { + this.state$ = new BehaviorSubject<DebugState>( + production ? DebugState.UNAVAILABLE : DebugState.ENABLED + ); + + this.enabled$ = this.state$.pipe( + map(state => state === DebugState.ENABLED), + distinctUntilChanged() + ); + } + public toggle(): void { switch (this.state) { case DebugState.ENABLED: