let service: SettingsService;
let httpTesting: HttpTestingController;
+ const exampleUrl = 'api/settings/something';
+ const exampleValue = 'http://localhost:3000';
+
configureTestBed(
{
providers: [SettingsService],
});
describe('isSettingConfigured', () => {
- const exampleUrl = 'api/settings/something';
- const exampleValue = 'http://localhost:3000';
let increment: number;
const testConfig = (url, value) => {
- service.ifSettingConfigured(url, (setValue) => {
- expect(setValue).toBe(value);
- increment++;
- });
+ service.ifSettingConfigured(
+ url,
+ (setValue) => {
+ expect(setValue).toBe(value);
+ increment++;
+ },
+ () => {
+ increment--;
+ }
+ );
};
const expectSettingsApiCall = (url: string, value: object, isSet: string) => {
expect(req.request.method).toBe('GET');
req.flush(value);
tick();
- expect(increment).toBe(isSet !== '' ? 1 : 0);
+ expect(increment).toBe(isSet !== '' ? 1 : -1);
expect(service['settings'][url]).toBe(isSet);
};
expect(increment).toBe(2);
}));
});
+
+ it('should disable a set setting', () => {
+ service['settings'] = { [exampleUrl]: exampleValue };
+ service.disableSetting(exampleUrl);
+ expect(service['settings']).toEqual({ [exampleUrl]: '' });
+ });
});
private settings: { [url: string]: string } = {};
- ifSettingConfigured(url: string, fn: (value?: string) => void): void {
+ ifSettingConfigured(url: string, fn: (value?: string) => void, elseFn?: () => void): void {
const setting = this.settings[url];
if (setting === undefined) {
- this.http.get(url).subscribe((data: any) => {
- this.settings[url] = this.getSettingsValue(data);
- this.ifSettingConfigured(url, fn);
- });
+ this.http.get(url).subscribe(
+ (data: any) => {
+ this.settings[url] = this.getSettingsValue(data);
+ this.ifSettingConfigured(url, fn, elseFn);
+ },
+ (resp) => {
+ if (resp.status !== 401) {
+ this.settings[url] = '';
+ }
+ }
+ );
} else if (setting !== '') {
fn(setting);
+ } else {
+ if (elseFn) {
+ elseFn();
+ }
}
}
+ // Easiest way to stop reloading external content that can't be reached
+ disableSetting(url) {
+ this.settings[url] = '';
+ }
+
private getSettingsValue(data: any): string {
return data.value || data.instance || '';
}