Skip to content

Commit fc17cb3

Browse files
add unit tests and fix lint issues
Co-authored-by: Saikrishna321 <[email protected]>
1 parent 9b2688f commit fc17cb3

File tree

10 files changed

+130
-46
lines changed

10 files changed

+130
-46
lines changed

app/common/renderer/actions/Inspector.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import _ from 'lodash';
22

3-
import {SAVED_FRAMEWORK, SET_SAVED_GESTURES, ENVIRONMENT_VARIABLES} from '../../shared/setting-defs';
3+
import {
4+
ENVIRONMENT_VARIABLES,
5+
SAVED_FRAMEWORK,
6+
SET_SAVED_GESTURES,
7+
} from '../../shared/setting-defs';
48
import {POINTER_TYPES} from '../constants/gestures';
59
import {APP_MODE, NATIVE_APP} from '../constants/session-inspector';
610
import i18n from '../i18next';
@@ -944,15 +948,15 @@ export function addEnvironmentVariable(key, value) {
944948
export function deleteEnvironmentVariable(key) {
945949
return async (dispatch, getState) => {
946950
const currentEnvVars = getState().inspector.environmentVariables || [];
947-
const newEnvVars = currentEnvVars.filter(v => v.key !== key);
951+
const newEnvVars = currentEnvVars.filter((v) => v.key !== key);
948952
await setSetting(ENVIRONMENT_VARIABLES, newEnvVars);
949953
dispatch({type: DELETE_ENVIRONMENT_VARIABLE, key});
950954
};
951955
}
952956

953957
export function loadEnvironmentVariables() {
954958
return async (dispatch) => {
955-
const envVars = await getSetting(ENVIRONMENT_VARIABLES) || [];
959+
const envVars = (await getSetting(ENVIRONMENT_VARIABLES)) || [];
956960
dispatch({type: SET_ENVIRONMENT_VARIABLES, envVars});
957961
};
958962
}

app/common/renderer/actions/Session.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {notification} from 'antd';
22
import {includes, isPlainObject, isUndefined, toPairs, union, values, without} from 'lodash';
3-
import {interpolateEnvironmentVariables} from '../utils/env-utils';
43
import moment from 'moment';
54
import {Web2Driver} from 'web2driver';
65

@@ -16,6 +15,7 @@ import {APP_MODE} from '../constants/session-inspector';
1615
import i18n from '../i18next';
1716
import {getSetting, ipcRenderer, setSetting} from '../polyfills';
1817
import {fetchSessionInformation, formatSeleniumGridSessions} from '../utils/attaching-to-session';
18+
import {interpolateEnvironmentVariables} from '../utils/env-utils';
1919
import {downloadFile, parseSessionFileContents} from '../utils/file-handling';
2020
import {log} from '../utils/logger';
2121
import {addVendorPrefixes} from '../utils/other';
@@ -232,10 +232,13 @@ export function newSession(caps, attachSessId = null) {
232232

233233
// Get environment variables from state
234234
const environmentVariables = getState().inspector.environmentVariables || [];
235-
235+
236236
// Get capabilities and interpolate environment variables
237237
let desiredCapabilities = caps ? getCapsObject(caps) : {};
238-
desiredCapabilities = interpolateEnvironmentVariables(desiredCapabilities, environmentVariables);
238+
desiredCapabilities = interpolateEnvironmentVariables(
239+
desiredCapabilities,
240+
environmentVariables,
241+
);
239242
let host, port, username, accessKey, https, path, token;
240243
desiredCapabilities = addCustomCaps(desiredCapabilities);
241244

app/common/renderer/components/Inspector/EnvironmentVariables.jsx

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import {PlusOutlined, DeleteOutlined} from '@ant-design/icons';
2-
import {Button, Input, Space, Table, Tooltip, Popconfirm} from 'antd';
1+
import {DeleteOutlined, PlusOutlined} from '@ant-design/icons';
2+
import {Button, Input, Popconfirm, Space, Table, Tooltip} from 'antd';
33
import {useState} from 'react';
44
import {connect} from 'react-redux';
5-
import {setEnvironmentVariables, addEnvironmentVariable, deleteEnvironmentVariable} from '../../actions/Inspector';
5+
6+
import {
7+
addEnvironmentVariable,
8+
deleteEnvironmentVariable,
9+
setEnvironmentVariables,
10+
} from '../../actions/Inspector';
611
import styles from './Inspector.module.css';
712

813
const EnvironmentVariables = ({t, envVars, addVariable, deleteVariable}) => {
@@ -36,20 +41,18 @@ const EnvironmentVariables = ({t, envVars, addVariable, deleteVariable}) => {
3641
cancelText={t('Cancel')}
3742
onConfirm={() => deleteVariable(record.key)}
3843
>
39-
<Button
40-
type="text"
41-
danger
42-
icon={<DeleteOutlined />}
43-
/>
44+
<Button type="text" danger icon={<DeleteOutlined />} />
4445
</Popconfirm>
4546
</Tooltip>
4647
),
4748
},
4849
];
4950

5051
const handleAddVariable = () => {
51-
if (!newVar.key || !newVar.value) return;
52-
52+
if (!newVar.key || !newVar.value) {
53+
return;
54+
}
55+
5356
// Check for duplicate keys
5457
if (envVars.some((v) => v.key === newVar.key)) {
5558
return;
@@ -83,24 +86,18 @@ const EnvironmentVariables = ({t, envVars, addVariable, deleteVariable}) => {
8386
</Button>
8487
</Space.Compact>
8588
</div>
86-
<Table
87-
columns={columns}
88-
dataSource={envVars}
89-
pagination={false}
90-
rowKey="key"
91-
size="small"
92-
/>
89+
<Table columns={columns} dataSource={envVars} pagination={false} rowKey="key" size="small" />
9390
</div>
9491
);
9592
};
9693

9794
const mapStateToProps = (state) => ({
98-
envVars: state.inspector.environmentVariables || []
95+
envVars: state.inspector.environmentVariables || [],
9996
});
10097

10198
const mapDispatchToProps = (dispatch) => ({
10299
addVariable: (key, value) => dispatch(addEnvironmentVariable(key, value)),
103-
deleteVariable: (key) => dispatch(deleteEnvironmentVariable(key))
100+
deleteVariable: (key) => dispatch(deleteEnvironmentVariable(key)),
104101
});
105102

106103
export default connect(mapStateToProps, mapDispatchToProps)(EnvironmentVariables);

app/common/renderer/components/Inspector/Inspector.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
import {Button, Card, Modal, Space, Spin, Switch, Tabs, Tooltip} from 'antd';
1616
import {debounce} from 'lodash';
1717
import {useEffect, useRef, useState} from 'react';
18-
import styles from './Inspector.module.css';
1918
import {useNavigate} from 'react-router';
2019

2120
import {BUTTON} from '../../constants/antd-types';
@@ -31,6 +30,7 @@ import {downloadFile} from '../../utils/file-handling';
3130
import Commands from './Commands.jsx';
3231
import GestureEditor from './GestureEditor.jsx';
3332
import HeaderButtons from './HeaderButtons.jsx';
33+
import styles from './Inspector.module.css';
3434
import Recorder from './Recorder.jsx';
3535
import SavedGestures from './SavedGestures.jsx';
3636
import Screenshot from './Screenshot.jsx';
@@ -263,7 +263,7 @@ const Inspector = (props) => {
263263
);
264264

265265
const main = (
266-
<div className={styles['inspector-main']}>
266+
<div className={styles['inspector-main']}>
267267
<div
268268
id="screenshotContainer"
269269
className={styles['screenshot-container']}

app/common/renderer/components/Inspector/Inspector.module.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,4 +741,4 @@
741741

742742
.addForm {
743743
padding: 8px 0;
744-
}
744+
}

app/common/renderer/components/Session/Session.jsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import {LinkOutlined} from '@ant-design/icons';
2-
import {Badge, Button, Spin, Tabs, Card} from 'antd';
3-
import { SettingOutlined } from '@ant-design/icons';
1+
import {LinkOutlined, SettingOutlined} from '@ant-design/icons';
2+
import {Badge, Button, Card, Spin, Tabs} from 'antd';
43
import _ from 'lodash';
54
import {useEffect} from 'react';
65
import {useNavigate} from 'react-router';
@@ -14,6 +13,7 @@ import {
1413
} from '../../constants/session-builder';
1514
import {ipcRenderer, openLink} from '../../polyfills';
1615
import {log} from '../../utils/logger';
16+
import EnvironmentVariables from '../Inspector/EnvironmentVariables.jsx';
1717
import AdvancedServerParams from './AdvancedServerParams.jsx';
1818
import AttachToSession from './AttachToSession.jsx';
1919
import CapabilityEditor from './CapabilityEditor.jsx';
@@ -22,7 +22,6 @@ import CloudProviderSelector from './CloudProviderSelector.jsx';
2222
import SavedSessions from './SavedSessions.jsx';
2323
import ServerTabCustom from './ServerTabCustom.jsx';
2424
import SessionStyles from './Session.module.css';
25-
import EnvironmentVariables from '../Inspector/EnvironmentVariables.jsx';
2625

2726
const Session = (props) => {
2827
const {

app/common/renderer/constants/session-inspector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ export const INSPECTOR_TABS = {
4141
COMMANDS: 'commands',
4242
GESTURES: 'gestures',
4343
RECORDER: 'recorder',
44-
SESSION_INFO: 'sessionInfo',
44+
SESSION_INFO: 'sessionInfo',
4545
};

app/common/renderer/reducers/Inspector.jsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import {omit} from 'lodash';
22

3+
import {ENVIRONMENT_VARIABLES} from '../../shared/setting-defs';
34
import {
45
ADD_ASSIGNED_VAR_CACHE,
6+
ADD_ENVIRONMENT_VARIABLE,
57
CANCEL_PENDING_COMMAND,
68
CLEAR_ASSIGNED_VAR_CACHE,
79
CLEAR_COORD_ACTION,
810
CLEAR_RECORDING,
911
CLEAR_SEARCH_RESULTS,
1012
CLEAR_SEARCHED_FOR_ELEMENT_BOUNDS,
1113
CLEAR_TAP_COORDINATES,
14+
DELETE_ENVIRONMENT_VARIABLE,
1215
DELETE_SAVED_GESTURES_DONE,
1316
DELETE_SAVED_GESTURES_REQUESTED,
1417
ENTERING_COMMAND_ARGS,
@@ -49,8 +52,6 @@ import {
4952
SET_COORD_END,
5053
SET_COORD_START,
5154
SET_ENVIRONMENT_VARIABLES,
52-
ADD_ENVIRONMENT_VARIABLE,
53-
DELETE_ENVIRONMENT_VARIABLE,
5455
SET_EXPANDED_PATHS,
5556
SET_GESTURE_TAP_COORDS_MODE,
5657
SET_GESTURE_UPLOAD_ERROR,
@@ -89,14 +90,13 @@ import {
8990
UNSELECT_TICK_ELEMENT,
9091
} from '../actions/Inspector';
9192
import {SCREENSHOT_INTERACTION_MODE} from '../constants/screenshot';
92-
import {ENVIRONMENT_VARIABLES} from '../../shared/setting-defs';
9393
import {APP_MODE, INSPECTOR_TABS, NATIVE_APP} from '../constants/session-inspector';
9494
import {getSetting} from '../polyfills';
9595

9696
const DEFAULT_FRAMEWORK = 'java';
9797

9898
// Load initial environment variables from persistent settings
99-
const envVars = await getSetting(ENVIRONMENT_VARIABLES) || [];
99+
const envVars = (await getSetting(ENVIRONMENT_VARIABLES)) || [];
100100

101101
const INITIAL_STATE = {
102102
savedGestures: [],
@@ -144,9 +144,11 @@ let nextState;
144144

145145
// Helper function to interpolate environment variables in a string
146146
const interpolateEnvVars = (str, envVars) => {
147-
if (typeof str !== 'string') return str;
147+
if (typeof str !== 'string') {
148+
return str;
149+
}
148150
return str.replace(/\${([^}]+)}/g, (match, key) => {
149-
const envVar = envVars.find(v => v.key === key);
151+
const envVar = envVars.find((v) => v.key === key);
150152
return envVar ? envVar.value : match;
151153
});
152154
};
@@ -164,14 +166,14 @@ export default function inspector(state = INITIAL_STATE, action) {
164166
...state,
165167
environmentVariables: [
166168
...state.environmentVariables,
167-
{key: action.key, value: action.value}
169+
{key: action.key, value: action.value},
168170
],
169171
};
170172

171173
case DELETE_ENVIRONMENT_VARIABLE:
172174
return {
173175
...state,
174-
environmentVariables: state.environmentVariables.filter(v => v.key !== action.key),
176+
environmentVariables: state.environmentVariables.filter((v) => v.key !== action.key),
175177
};
176178

177179
case SET_SOURCE_AND_SCREENSHOT:

app/common/renderer/utils/env-utils.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
export function interpolateEnvironmentVariables(obj, envVars) {
88
if (typeof obj === 'string') {
99
return obj.replace(/\${([^}]+)}/g, (match, key) => {
10-
const envVar = envVars.find(v => v.key === key);
10+
const envVar = envVars.find((v) => v.key === key);
1111
return envVar ? envVar.value : match;
1212
});
1313
}
14-
14+
1515
if (Array.isArray(obj)) {
16-
return obj.map(item => interpolateEnvironmentVariables(item, envVars));
16+
return obj.map((item) => interpolateEnvironmentVariables(item, envVars));
1717
}
18-
18+
1919
if (obj !== null && typeof obj === 'object') {
2020
return Object.keys(obj).reduce((acc, key) => {
2121
acc[key] = interpolateEnvironmentVariables(obj[key], envVars);
2222
return acc;
2323
}, {});
2424
}
25-
25+
2626
return obj;
2727
}

test/unit/utils-env.spec.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {describe, expect, it} from 'vitest';
2+
3+
import {interpolateEnvironmentVariables} from '../../app/common/renderer/utils/env-utils';
4+
5+
describe('interpolateEnvironmentVariables', function () {
6+
const envVars = [
7+
{key: 'HOME', value: '/home/user'},
8+
{key: 'PATH', value: '/usr/bin'},
9+
{key: 'APP_ENV', value: 'test'},
10+
];
11+
12+
it('should interpolate environment variables in strings', function () {
13+
const input = 'My home is ${HOME} and path is ${PATH}';
14+
const expected = 'My home is /home/user and path is /usr/bin';
15+
expect(interpolateEnvironmentVariables(input, envVars)).to.equal(expected);
16+
});
17+
18+
it('should leave unmatched variables unchanged', function () {
19+
const input = 'Value: ${UNKNOWN_VAR}';
20+
expect(interpolateEnvironmentVariables(input, envVars)).to.equal('Value: ${UNKNOWN_VAR}');
21+
});
22+
23+
it('should interpolate variables in arrays', function () {
24+
const input = ['${HOME}/files', '${PATH}/python'];
25+
const expected = ['/home/user/files', '/usr/bin/python'];
26+
expect(interpolateEnvironmentVariables(input, envVars)).to.deep.equal(expected);
27+
});
28+
29+
it('should interpolate variables in nested objects', function () {
30+
const input = {
31+
home: '${HOME}',
32+
paths: {
33+
bin: '${PATH}',
34+
config: '${HOME}/.config',
35+
},
36+
env: '${APP_ENV}',
37+
};
38+
const expected = {
39+
home: '/home/user',
40+
paths: {
41+
bin: '/usr/bin',
42+
config: '/home/user/.config',
43+
},
44+
env: 'test',
45+
};
46+
expect(interpolateEnvironmentVariables(input, envVars)).to.deep.equal(expected);
47+
});
48+
49+
it('should handle null values', function () {
50+
expect(interpolateEnvironmentVariables(null, envVars)).to.equal(null);
51+
});
52+
53+
it('should handle undefined values', function () {
54+
expect(interpolateEnvironmentVariables(undefined, envVars)).to.equal(undefined);
55+
});
56+
57+
it('should handle number values', function () {
58+
expect(interpolateEnvironmentVariables(42, envVars)).to.equal(42);
59+
});
60+
61+
it('should handle boolean values', function () {
62+
expect(interpolateEnvironmentVariables(true, envVars)).to.equal(true);
63+
expect(interpolateEnvironmentVariables(false, envVars)).to.equal(false);
64+
});
65+
66+
it('should handle empty arrays', function () {
67+
expect(interpolateEnvironmentVariables([], envVars)).to.deep.equal([]);
68+
});
69+
70+
it('should handle empty objects', function () {
71+
expect(interpolateEnvironmentVariables({}, envVars)).to.deep.equal({});
72+
});
73+
74+
it('should handle multiple variables in single string', function () {
75+
const input = '${HOME}:${PATH}:${APP_ENV}';
76+
const expected = '/home/user:/usr/bin:test';
77+
expect(interpolateEnvironmentVariables(input, envVars)).to.equal(expected);
78+
});
79+
});

0 commit comments

Comments
 (0)