Properly sets defaults for environment variables

This commit is contained in:
Aaron Louie 2020-05-18 19:55:45 -04:00
parent 7f9dce9b00
commit e3214aa5dd
3 changed files with 43 additions and 9 deletions

View File

@ -2,4 +2,5 @@ var ENV = {
production: 'false', production: 'false',
api: 'apiRoot', api: 'apiRoot',
irbUrl: 'irbUrl', irbUrl: 'irbUrl',
homeRoute: 'home',
}; };

View File

@ -2,14 +2,11 @@ import {AppEnvironment} from 'sartography-workflow-lib';
declare var ENV; declare var ENV;
const envHas = (key, temp): boolean => (ENV && (ENV[key] !== null) && (ENV[key] !== undefined) && (ENV[key] !== temp)); export const _has = (env, key, temp): boolean => env && ![null, undefined, temp, ''].includes(env[key]);
export const environment: AppEnvironment = { export const environment: AppEnvironment = {
homeRoute: envHas('homeRoute', '$HOME_ROUTE') ? ENV.homeRoute : 'home', homeRoute: _has(ENV, 'homeRoute', '$HOME_ROUTE') ? ENV.homeRoute : 'home',
production: envHas('production', '$PRODUCTION') ? (ENV.production === 'true') : false, production: _has(ENV, 'production', '$PRODUCTION') ? (ENV.production === 'true') : false,
api: envHas('api', '$API_URL') ? ENV.api : 'http://localhost:5000/v1.0', api: _has(ENV, 'api', '$API_URL') ? ENV.api : 'http://localhost:5000/v1.0',
irbUrl: envHas('irbUrl', '$IRB_URL') ? ENV.irbUrl : 'http://localhost:5001', irbUrl: _has(ENV, 'irbUrl', '$IRB_URL') ? ENV.irbUrl : 'http://localhost:5001',
}; };

View File

@ -1,5 +1,6 @@
import {environment} from './environment.runtime'; import {_has, environment} from './environment.runtime';
declare var ENV;
describe('Environments', () => { describe('Environments', () => {
it('should have settings for all the environments', () => { it('should have settings for all the environments', () => {
@ -7,5 +8,40 @@ describe('Environments', () => {
expect(environment.production).toEqual(false); expect(environment.production).toEqual(false);
expect(environment.api).toEqual('apiRoot'); expect(environment.api).toEqual('apiRoot');
expect(environment.irbUrl).toEqual('irbUrl'); expect(environment.irbUrl).toEqual('irbUrl');
expect(environment.homeRoute).toEqual('home');
});
it('should check if environment variables are defined', () => {
const env = {
homeRoute: '$HOME_ROUTE',
production: '$PRODUCTION',
api: '$API_URL',
irbUrl: '$IRB_URL',
};
expect(_has(env, 'homeRoute', '$HOME_ROUTE')).toBeFalse();
expect(_has(env, 'production', '$PRODUCTION')).toBeFalse();
expect(_has(env, 'api', '$API_URL')).toBeFalse();
expect(_has(env, 'irbUrl', '$IRB_URL')).toBeFalse();
env.homeRoute = undefined;
env.production = undefined;
env.api = undefined;
env.irbUrl = undefined;
expect(_has(env, 'homeRoute', '$HOME_ROUTE')).toBeFalse();
expect(_has(env, 'production', '$PRODUCTION')).toBeFalse();
expect(_has(env, 'api', '$API_URL')).toBeFalse();
expect(_has(env, 'irbUrl', '$IRB_URL')).toBeFalse();
env.homeRoute = 'something';
env.production = 'something';
env.api = 'something';
env.irbUrl = 'something';
expect(_has(env, 'homeRoute', '$HOME_ROUTE')).toBeTrue();
expect(_has(env, 'production', '$PRODUCTION')).toBeTrue();
expect(_has(env, 'api', '$API_URL')).toBeTrue();
expect(_has(env, 'irbUrl', '$IRB_URL')).toBeTrue();
}); });
}); });