From 9aaaec779eda77a5bf580c867285b698bc378319 Mon Sep 17 00:00:00 2001 From: Matthew Russell Date: Wed, 21 Jun 2023 16:27:21 -0700 Subject: [PATCH] test: add test to check that you can set the env from window object --- .../src/hooks/use-environment.spec.ts | 19 +++++++++++++++++++ libs/environment/src/hooks/use-environment.ts | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/libs/environment/src/hooks/use-environment.spec.ts b/libs/environment/src/hooks/use-environment.spec.ts index 38777de8f..10be7ecbf 100644 --- a/libs/environment/src/hooks/use-environment.spec.ts +++ b/libs/environment/src/hooks/use-environment.spec.ts @@ -301,6 +301,25 @@ describe('useEnvironment', () => { expect(fetch).toHaveBeenCalledWith(configUrl); }); + it('uses env vars from window._env_ if set', async () => { + const url = 'http://foo.bar.com'; + // @ts-ignore _env_ is declared in app + window._env_ = { + VEGA_URL: url, + }; + + const { result } = setup(); + + await act(async () => { + result.current.initialize(); + }); + + expect(result.current.VEGA_URL).toBe(url); + + // @ts-ignore delete _env_ + delete window['_env_']; + }); + it('sets error if environment is invalid', async () => { const error = console.error; console.error = noop; diff --git a/libs/environment/src/hooks/use-environment.ts b/libs/environment/src/hooks/use-environment.ts index 56401c98c..7c399907f 100644 --- a/libs/environment/src/hooks/use-environment.ts +++ b/libs/environment/src/hooks/use-environment.ts @@ -412,5 +412,5 @@ export function windowOrDefault(key: string, defaultValue?: string) { return window._env_[key]; } } - return defaultValue || ''; + return defaultValue || undefined; }