|
15 | 15 | * That's why comparing the origin is aboslutely mandatory. |
16 | 16 | * - Since we test the hydrate app in the test context, the origin is getting |
17 | 17 | * 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`), |
20 | 20 | * that we set/reset during the server test, so we can correctly identify |
21 | 21 | * the hydrate app in the test context. |
22 | 22 | * As a drawback, we can't properly test if the hydrate app is detected as |
23 | 23 | * expected, in a real world scenario (elsewhere than in our tests). |
24 | 24 | */ |
25 | 25 |
|
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'; |
32 | 28 |
|
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; |
34 | 52 | export const IS_SERVER: boolean = !IS_BROWSER; |
0 commit comments