Skip to content

Commit 96b0c66

Browse files
chore(components): fix environment vars
1 parent 4f6d8c9 commit 96b0c66

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

packages/components/src/utils/environment.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,38 @@
1515
* That's why comparing the origin is aboslutely mandatory.
1616
* - Since we test the hydrate app in the test context, the origin is getting
1717
* overwritten by the test setup, leading to a false positive in the
18-
* IS_HYDRATEAPP detection.
19-
* To circumvent this, we use a global flag (`IS_HYDRATEAPP_TEST`),
18+
* `isHydrateApp` detection.
19+
* To circumvent this, we use a global flag (`isHydrateAppTestEnv`),
2020
* that we set/reset during the server test, so we can correctly identify
2121
* the hydrate app in the test context.
2222
* As a drawback, we can't properly test if the hydrate app is detected as
2323
* expected, in a real world scenario (elsewhere than in our tests).
2424
*/
2525

26-
const HYDRATEAPP_USERAGENT = 'MockNavigator';
27-
const HYDRATEAPP_ORIGIN = 'http://mockdoc.stenciljs.com';
28-
const IS_HYDRATEAPP =
29-
global?.IS_HYDRATEAPP_TEST ||
30-
(window?.navigator.userAgent === HYDRATEAPP_USERAGENT &&
31-
window?.location.origin === HYDRATEAPP_ORIGIN);
26+
const MOCKED_USERAGENT = 'MockNavigator';
27+
const HYDRATEAPP_MOCKED_ORIGIN = 'http://mockdoc.stenciljs.com';
3228

33-
export const IS_BROWSER: boolean = typeof window !== 'undefined' && !IS_HYDRATEAPP;
29+
const isMockedUserAgent = window?.navigator.userAgent === MOCKED_USERAGENT;
30+
const isMockedOrigin = window?.location.origin === HYDRATEAPP_MOCKED_ORIGIN;
31+
const isNodeEnv = typeof process !== 'undefined';
32+
33+
/**
34+
* This is the natural hydrate app environment
35+
* Is `true` if the hydrate app runs on server
36+
*/
37+
const isHydrateAppEnv = isNodeEnv && isMockedUserAgent && isMockedOrigin;
38+
39+
/**
40+
* This is the test app server test env
41+
* Is `true` if the server unit test runs (`false` if the browser unit test runs)
42+
*/
43+
const isTestAppServerTestEnv = isNodeEnv && global.IS_HYDRATEAPP_SERVERTEST;
44+
45+
/**
46+
* This is the final hydrate app flag
47+
* if it`s `true`, IS_BROWSER equals `false` and IS_SERVER equals `true
48+
*/
49+
const isHydrateApp = isHydrateAppEnv || isTestAppServerTestEnv;
50+
51+
export const IS_BROWSER: boolean = typeof window !== 'undefined' && !isHydrateApp;
3452
export const IS_SERVER: boolean = !IS_BROWSER;

packages/components/src/utils/tests/environment/hydrate-app-helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export async function getAppEnv() {
2-
global.IS_HYDRATEAPP_TEST = true;
2+
global.IS_HYDRATEAPP_SERVERTEST = true;
33

44
// keep this, to have runtime-only import (otherwise the build fails)
55
const HYDRATE_ID = '@swisspost/design-system-components/hydrate';
@@ -17,5 +17,5 @@ export async function getAppEnv() {
1717
}
1818

1919
export function afterTestCleanup() {
20-
delete global.IS_HYDRATEAPP_TEST;
20+
delete global.IS_HYDRATEAPP_SERVERTEST;
2121
}

0 commit comments

Comments
 (0)