api-client/packages/hoppscotch-js-sandbox/src/__tests__/testing/envs/get.spec.ts
Nivedin 00862eb192
feat: secret variables in environments (#3779)
Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
2024-02-08 21:58:42 +05:30

184 lines
3.9 KiB
TypeScript

import "@relmify/jest-fp-ts"
import * as TE from "fp-ts/TaskEither"
import { pipe } from "fp-ts/function"
import { runTestScript } from "~/test-runner/node-vm"
import { TestResponse, TestResult } from "~/types"
const fakeResponse: TestResponse = {
status: 200,
body: "hoi",
headers: [],
}
const func = (script: string, envs: TestResult["envs"]) =>
pipe(
runTestScript(script, envs, fakeResponse),
TE.map((x) => x.tests)
)
describe("pw.env.get", () => {
test("returns the correct value for an existing selected environment value", () => {
return expect(
func(
`
const data = pw.env.get("a")
pw.expect(data).toBe("b")
`,
{
global: [],
selected: [
{
key: "a",
value: "b",
secret: false,
},
],
}
)()
).resolves.toEqualRight([
expect.objectContaining({
expectResults: [
{
status: "pass",
message: "Expected 'b' to be 'b'",
},
],
}),
])
})
test("returns the correct value for an existing global environment value", () => {
return expect(
func(
`
const data = pw.env.get("a")
pw.expect(data).toBe("b")
`,
{
global: [
{
key: "a",
value: "b",
secret: false,
},
],
selected: [],
}
)()
).resolves.toEqualRight([
expect.objectContaining({
expectResults: [
{
status: "pass",
message: "Expected 'b' to be 'b'",
},
],
}),
])
})
test("returns undefined for a key that is not present in both selected or environment", () => {
return expect(
func(
`
const data = pw.env.get("a")
pw.expect(data).toBe(undefined)
`,
{
global: [],
selected: [],
}
)()
).resolves.toEqualRight([
expect.objectContaining({
expectResults: [
{
status: "pass",
message: "Expected 'undefined' to be 'undefined'",
},
],
}),
])
})
test("returns the value defined in selected environment if it is also present in global", () => {
return expect(
func(
`
const data = pw.env.get("a")
pw.expect(data).toBe("selected val")
`,
{
global: [
{
key: "a",
value: "global val",
secret: false,
},
],
selected: [
{
key: "a",
value: "selected val",
secret: false,
},
],
}
)()
).resolves.toEqualRight([
expect.objectContaining({
expectResults: [
{
status: "pass",
message: "Expected 'selected val' to be 'selected val'",
},
],
}),
])
})
test("does not resolve environment values", () => {
return expect(
func(
`
const data = pw.env.get("a")
pw.expect(data).toBe("<<hello>>")
`,
{
global: [],
selected: [
{
key: "a",
value: "<<hello>>",
secret: false,
},
],
}
)()
).resolves.toEqualRight([
expect.objectContaining({
expectResults: [
{
status: "pass",
message: "Expected '<<hello>>' to be '<<hello>>'",
},
],
}),
])
})
test("errors if the key is not a string", () => {
return expect(
func(
`
const data = pw.env.get(5)
`,
{
global: [],
selected: [],
}
)()
).resolves.toBeLeft()
})
})