-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add settings.js and SettingsService #40
- Loading branch information
Showing
7 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,6 @@ testem.log | |
# System Files | ||
.DS_Store | ||
Thumbs.db | ||
|
||
# Project-specific | ||
/src/settings.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { TestBed, fakeAsync, tick } from '@angular/core/testing'; | ||
|
||
import { SettingsService, Settings } from './settings.service'; | ||
|
||
describe('SettingsService', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
}); | ||
|
||
describe('.settings$', () => { | ||
let settingsMock: Settings; | ||
let next: jasmine.Spy; | ||
let error: jasmine.Spy; | ||
let complete: jasmine.Spy; | ||
beforeEach(() => { | ||
settingsMock = { apiUrl: 'https://example.com/api' }; | ||
|
||
next = jasmine.createSpy('next'); | ||
error = jasmine.createSpy('error'); | ||
complete = jasmine.createSpy('complete'); | ||
}); | ||
|
||
afterEach(() => resetSettings()); | ||
|
||
it('emits settings object', () => { | ||
setSettings(settingsMock); | ||
const service: SettingsService = TestBed.get(SettingsService); | ||
|
||
service.settings$.subscribe(next, error, complete); | ||
expect(next).toHaveBeenCalledWith(settingsMock); | ||
expect(error).not.toHaveBeenCalled(); | ||
expect(complete).toHaveBeenCalled(); | ||
}); | ||
|
||
it('emits settings object, when available after 2s', fakeAsync(() => { | ||
const service: SettingsService = TestBed.get(SettingsService); | ||
service.settings$.subscribe(next, error, complete); | ||
expect(next).not.toHaveBeenCalled(); | ||
|
||
tick(1000); | ||
expect(next).not.toHaveBeenCalled(); | ||
expect(error).not.toHaveBeenCalled(); | ||
expect(complete).not.toHaveBeenCalled(); | ||
|
||
tick(1000); | ||
expect(next).not.toHaveBeenCalled(); | ||
expect(error).not.toHaveBeenCalled(); | ||
expect(complete).not.toHaveBeenCalled(); | ||
|
||
setSettings(settingsMock); | ||
tick(1000); | ||
expect(next).toHaveBeenCalledWith(settingsMock); | ||
expect(error).not.toHaveBeenCalled(); | ||
expect(complete).toHaveBeenCalled(); | ||
})); | ||
|
||
it('throws error when settings loading failed or not defined', fakeAsync(() => { | ||
const service: SettingsService = TestBed.get(SettingsService); | ||
service.settings$.subscribe(next, error, complete); | ||
expect(next).not.toHaveBeenCalled(); | ||
|
||
tick(6000); | ||
expect(next).not.toHaveBeenCalled(); | ||
expect(error).toHaveBeenCalled(); | ||
expect(error.calls.mostRecent().args[0].toString()).toEqual( | ||
'Error: Settings not available' | ||
); | ||
expect(complete).not.toHaveBeenCalled(); | ||
})); | ||
}); | ||
|
||
describe('apiUrl$', () => { | ||
it('returns apiUrl', () => { | ||
setSettings({ apiUrl: 'https://example.com/api' }); | ||
const service: SettingsService = TestBed.get(SettingsService); | ||
const callback = jasmine.createSpy('callback'); | ||
service.apiUrl$.subscribe(callback); | ||
expect(callback).toHaveBeenCalledWith('https://example.com/api'); | ||
}); | ||
}); | ||
|
||
function setSettings(settings: Settings): void { | ||
(window as any).absenzenmanagement = { settings }; | ||
} | ||
|
||
function resetSettings(): void { | ||
(window as any).absenzenmanagement = undefined; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { defer, of, Observable } from 'rxjs'; | ||
import { map, retryWhen, delay, pluck, shareReplay } from 'rxjs/operators'; | ||
|
||
export interface Settings { | ||
apiUrl: string; | ||
} | ||
|
||
const SETTINGS_RETRY_COUNT = 10; | ||
const SETTINGS_RETRY_DELAY = 500; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class SettingsService { | ||
settings$ = this.loadSettings(); | ||
apiUrl$ = this.settings$.pipe(pluck<Settings, string>('apiUrl')); | ||
|
||
private loadSettings(): Observable<Settings> { | ||
return defer(() => of(this.settings)).pipe( | ||
map(settings => { | ||
if (settings == null) { | ||
throw new Error('Settings not available'); | ||
} | ||
return settings; | ||
}), | ||
retryWhen(this.retryNotifier$), | ||
shareReplay(1) | ||
); | ||
} | ||
|
||
private retryNotifier$(errors$: Observable<any>): Observable<any> { | ||
let retries = 0; | ||
return errors$.pipe( | ||
delay(SETTINGS_RETRY_DELAY), | ||
map(error => { | ||
if (retries++ === SETTINGS_RETRY_COUNT) { | ||
throw error; | ||
} | ||
return error; | ||
}) | ||
); | ||
} | ||
|
||
private get settings(): Option<Settings> { | ||
return ( | ||
((window as any).absenzenmanagement && | ||
(window as any).absenzenmanagement.settings) || | ||
null | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Rename this file to settings.js and adjust the settings | ||
|
||
window.absenzenmanagement = window.absenzenmanagement || {}; | ||
|
||
window.absenzenmanagement.settings = { | ||
// API base URL without trailing slash | ||
apiUrl: 'https://eventotest.api' | ||
}; |