Commit 1e8427b5 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Added a test on DebugStateService.

No related merge requests found
Showing with 67 additions and 17 deletions
+67 -17
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();
})
);
});
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:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment