Skip to content

Commit 8596cae

Browse files
nadr0jacebrowning
andauthored
chore: support IS_STAGING_OR_DEBUG in web based runtimes (#7981)
* nadro/adhoc/staging-debug-for-web * chore: updating IS_STAGING_OR_DEBUG to support web browser runtimes, added unit test * fix: typo * fix: renaming from main/development to dev * Update src/routes/utils.test.ts Co-authored-by: Jace Browning <[email protected]> * Update src/routes/utils.test.ts Co-authored-by: Jace Browning <[email protected]> --------- Co-authored-by: Jace Browning <[email protected]>
1 parent 3840377 commit 8596cae

File tree

2 files changed

+114
-9
lines changed

2 files changed

+114
-9
lines changed

src/routes/utils.test.ts

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
import { getRefFromVersion } from '@src/routes/utils'
1+
import { getAppVersion, getRefFromVersion } from '@src/routes/utils'
2+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3+
4+
beforeEach(() => {
5+
const mockElectron = {
6+
packageJson: {
7+
version: 'mocked-version',
8+
},
9+
}
10+
vi.stubGlobal('window', { electron: mockElectron })
11+
})
12+
13+
afterEach(() => {
14+
vi.unstubAllGlobals()
15+
})
216

317
describe('Routes utility functions', () => {
418
describe('getRefFromVersion', () => {
@@ -12,4 +26,70 @@ describe('Routes utility functions', () => {
1226
expect(getRefFromVersion('main')).toBeUndefined()
1327
})
1428
})
29+
30+
describe('getAppVersion', () => {
31+
it('should return 0.0.0 for web testing', () => {
32+
const expected = '0.0.0'
33+
const actual = getAppVersion({
34+
isTestEnvironment: true,
35+
NODE_ENV: 'development',
36+
isDesktop: false,
37+
})
38+
expect(actual).toBe(expected)
39+
})
40+
it('should return 0.0.0 for desktop testing', () => {
41+
const expected = '0.0.0'
42+
const actual = getAppVersion({
43+
isTestEnvironment: true,
44+
NODE_ENV: 'development',
45+
isDesktop: true,
46+
})
47+
expect(actual).toBe(expected)
48+
})
49+
it('should return another mocked packageJson version', () => {
50+
const expected = 'mocked-version'
51+
const actual = getAppVersion({
52+
isTestEnvironment: false,
53+
NODE_ENV: 'development',
54+
isDesktop: true,
55+
})
56+
expect(actual).toBe(expected)
57+
})
58+
it('should return another mocked packageJson version', () => {
59+
const expected = 'mocked-version'
60+
const actual = getAppVersion({
61+
isTestEnvironment: true,
62+
NODE_ENV: 'not-development',
63+
isDesktop: true,
64+
})
65+
expect(actual).toBe(expected)
66+
})
67+
it('should return dev', () => {
68+
const expected = 'dev'
69+
const actual = getAppVersion({
70+
isTestEnvironment: false,
71+
NODE_ENV: 'development',
72+
isDesktop: false,
73+
})
74+
expect(actual).toBe(expected)
75+
})
76+
it('should return main', () => {
77+
const expected = 'main'
78+
const actual = getAppVersion({
79+
isTestEnvironment: false,
80+
NODE_ENV: 'not-development',
81+
isDesktop: false,
82+
})
83+
expect(actual).toBe(expected)
84+
})
85+
it('should return main because NODE_ENV is production', () => {
86+
const expected = 'main'
87+
const actual = getAppVersion({
88+
isTestEnvironment: false,
89+
NODE_ENV: 'production',
90+
isDesktop: false,
91+
})
92+
expect(actual).toBe(expected)
93+
})
94+
})
1595
})

src/routes/utils.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,46 @@ import { withSiteBaseURL } from '@src/lib/withBaseURL'
99

1010
const isTestEnv = window?.localStorage.getItem(IS_PLAYWRIGHT_KEY) === 'true'
1111

12-
export const APP_VERSION =
13-
isTestEnv && env().NODE_ENV === 'development'
14-
? '0.0.0'
15-
: isDesktop()
16-
? // @ts-ignore
17-
window.electron.packageJson.version
18-
: 'main'
12+
export function getAppVersion({
13+
isTestEnvironment,
14+
NODE_ENV,
15+
isDesktop,
16+
}: {
17+
isTestEnvironment: boolean
18+
NODE_ENV: string | undefined
19+
isDesktop: boolean
20+
}) {
21+
if (isTestEnvironment && NODE_ENV === 'development') {
22+
return '0.0.0'
23+
}
24+
25+
if (isDesktop) {
26+
// @ts-ignore
27+
return window.electron.packageJson.version
28+
}
29+
30+
// Web based runtimes
31+
if (NODE_ENV === 'development') {
32+
return 'dev'
33+
}
34+
35+
return 'main'
36+
}
37+
38+
export const APP_VERSION = getAppVersion({
39+
isTestEnvironment: isTestEnv,
40+
NODE_ENV: env().NODE_ENV,
41+
isDesktop: isDesktop(),
42+
})
1943

2044
export const PACKAGE_NAME = isDesktop()
2145
? window.electron.packageJson.name
2246
: 'zoo-modeling-app'
2347

2448
export const IS_STAGING = PACKAGE_NAME.indexOf('-staging') > -1
2549

26-
export const IS_STAGING_OR_DEBUG = IS_STAGING || APP_VERSION === '0.0.0'
50+
export const IS_STAGING_OR_DEBUG =
51+
IS_STAGING || APP_VERSION === '0.0.0' || APP_VERSION === 'dev'
2752

2853
export function getRefFromVersion(version: string) {
2954
const hash = version.split('.').pop()

0 commit comments

Comments
 (0)