update environment service to handle optional urls

pull/7913/head
Arman Ozak 5 years ago
parent d0a52b1b1e
commit 272361159d

@ -4,6 +4,9 @@ import { map } from 'rxjs/operators';
import { Apis, Environment } from '../models/environment';
import { InternalStore } from '../utils/internal-store-utils';
const mapToApiUrl = (key: string) => (apis: Apis) =>
(apis[key] || apis.default).url || apis.default.url;
@Injectable({ providedIn: 'root' })
export class EnvironmentService {
private readonly store = new InternalStore({} as Environment);
@ -21,13 +24,11 @@ export class EnvironmentService {
}
getApiUrl(key?: string) {
return (this.store.state.apis[key || 'default'] || this.store.state.apis.default).url;
return mapToApiUrl(key)(this.store.state.apis);
}
getApiUrl$(key?: string) {
return this.store
.sliceState(state => state.apis)
.pipe(map((apis: Apis) => (apis[key || 'default'] || apis.default).url));
return this.store.sliceState(state => state.apis).pipe(map(mapToApiUrl(key)));
}
setState(environment: Environment) {

@ -1,3 +1,4 @@
import { waitForAsync } from '@angular/core/testing';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
import { Environment } from '../models';
import { EnvironmentService } from '../services';
@ -17,6 +18,7 @@ export const ENVIRONMENT_DATA = ({
other: {
url: 'https://localhost:44306',
},
yetAnother: {},
},
localization: {
defaultResourceName: 'MyProjectName',
@ -39,19 +41,28 @@ describe('ConfigState', () => {
});
describe('#getEnvironment', () => {
it('should return ENVIRONMENT_DATA', () => {
expect(environment.getEnvironment()).toEqual(ENVIRONMENT_DATA);
environment.getEnvironment$().subscribe(data => expect(data).toEqual(ENVIRONMENT_DATA));
});
it(
'should return ENVIRONMENT_DATA',
waitForAsync(() => {
expect(environment.getEnvironment()).toEqual(ENVIRONMENT_DATA);
environment.getEnvironment$().subscribe(data => expect(data).toEqual(ENVIRONMENT_DATA));
}),
);
});
describe('#getApiUrl', () => {
it('should return api url', () => {
expect(environment.getApiUrl()).toEqual(ENVIRONMENT_DATA.apis.default.url);
environment
.getApiUrl$('other')
.subscribe(data => expect(data).toEqual(ENVIRONMENT_DATA.apis.other.url));
});
it(
'should return api url',
waitForAsync(() => {
expect(environment.getApiUrl()).toEqual(ENVIRONMENT_DATA.apis.default.url);
environment
.getApiUrl$('other')
.subscribe(data => expect(data).toEqual(ENVIRONMENT_DATA.apis.other.url));
environment
.getApiUrl$('yetAnother')
.subscribe(data => expect(data).toEqual(ENVIRONMENT_DATA.apis.default.url));
}),
);
});
// TODO: create permission.service.spec.ts

Loading…
Cancel
Save