feat(js-sandbox): add pw.env.unset method (#3677)
Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
@@ -18,6 +18,63 @@ const getEnv = (envName: string, envs: TestResult["envs"]) => {
|
||||
)
|
||||
}
|
||||
|
||||
const findEnvIndex = (
|
||||
envName: string,
|
||||
envList: SelectedEnvItem[] | GlobalEnvItem[]
|
||||
): number => {
|
||||
return envList.findIndex(
|
||||
(envItem: SelectedEnvItem) => envItem.key === envName
|
||||
)
|
||||
}
|
||||
|
||||
const setEnv = (
|
||||
envName: string,
|
||||
envValue: string,
|
||||
envs: TestResult["envs"]
|
||||
): TestResult["envs"] => {
|
||||
const { global, selected } = envs
|
||||
|
||||
const indexInSelected = findEnvIndex(envName, selected)
|
||||
const indexInGlobal = findEnvIndex(envName, global)
|
||||
|
||||
if (indexInSelected >= 0) {
|
||||
selected[indexInSelected].value = envValue
|
||||
} else if (indexInGlobal >= 0) {
|
||||
global[indexInGlobal].value = envValue
|
||||
} else {
|
||||
selected.push({
|
||||
key: envName,
|
||||
value: envValue,
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
global,
|
||||
selected,
|
||||
}
|
||||
}
|
||||
|
||||
const unsetEnv = (
|
||||
envName: string,
|
||||
envs: TestResult["envs"]
|
||||
): TestResult["envs"] => {
|
||||
const { global, selected } = envs
|
||||
|
||||
const indexInSelected = findEnvIndex(envName, selected)
|
||||
const indexInGlobal = findEnvIndex(envName, global)
|
||||
|
||||
if (indexInSelected >= 0) {
|
||||
selected.splice(indexInSelected, 1)
|
||||
} else if (indexInGlobal >= 0) {
|
||||
global.splice(indexInGlobal, 1)
|
||||
}
|
||||
|
||||
return {
|
||||
global,
|
||||
selected,
|
||||
}
|
||||
}
|
||||
|
||||
// Compiles shared scripting API methods for use in both pre and post request scripts
|
||||
const getSharedMethods = (envs: TestResult["envs"]) => {
|
||||
let updatedEnvs = envs
|
||||
@@ -79,6 +136,16 @@ const getSharedMethods = (envs: TestResult["envs"]) => {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const envUnsetFn = (key: any) => {
|
||||
if (typeof key !== "string") {
|
||||
throw new Error("Expected key to be a string")
|
||||
}
|
||||
|
||||
updatedEnvs = unsetEnv(key, updatedEnvs)
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
const envResolveFn = (value: any) => {
|
||||
if (typeof value !== "string") {
|
||||
throw new Error("Expected value to be a string")
|
||||
@@ -101,6 +168,7 @@ const getSharedMethods = (envs: TestResult["envs"]) => {
|
||||
get: envGetFn,
|
||||
getResolve: envGetResolveFn,
|
||||
set: envSetFn,
|
||||
unset: envUnsetFn,
|
||||
resolve: envResolveFn,
|
||||
},
|
||||
},
|
||||
@@ -108,51 +176,6 @@ const getSharedMethods = (envs: TestResult["envs"]) => {
|
||||
}
|
||||
}
|
||||
|
||||
const setEnv = (
|
||||
envName: string,
|
||||
envValue: string,
|
||||
envs: TestResult["envs"]
|
||||
): TestResult["envs"] => {
|
||||
const { global, selected } = envs
|
||||
|
||||
const indexInSelected = selected.findIndex(
|
||||
(x: SelectedEnvItem) => x.key === envName
|
||||
)
|
||||
|
||||
// Found the match in selected
|
||||
if (indexInSelected >= 0) {
|
||||
selected[indexInSelected].value = envValue
|
||||
|
||||
return {
|
||||
global,
|
||||
selected,
|
||||
}
|
||||
}
|
||||
|
||||
const indexInGlobal = global.findIndex((x: GlobalEnvItem) => x.key == envName)
|
||||
|
||||
// Found a match in globals
|
||||
if (indexInGlobal >= 0) {
|
||||
global[indexInGlobal].value = envValue
|
||||
|
||||
return {
|
||||
global,
|
||||
selected,
|
||||
}
|
||||
}
|
||||
|
||||
// Didn't find in both places, create a new variable in selected
|
||||
selected.push({
|
||||
key: envName,
|
||||
value: envValue,
|
||||
})
|
||||
|
||||
return {
|
||||
global,
|
||||
selected,
|
||||
}
|
||||
}
|
||||
|
||||
export function preventCyclicObjects(
|
||||
obj: Record<string, any>
|
||||
): E.Left<string> | E.Right<Record<string, any>> {
|
||||
|
||||
Reference in New Issue
Block a user