fix: revamp broken network tests
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
import { cancelRunningRequest, sendNetworkRequest } from "../network"
|
||||
|
||||
import AxiosStrategy, {
|
||||
cancelRunningAxiosRequest,
|
||||
} from "../strategies/AxiosStrategy"
|
||||
import ExtensionStrategy, {
|
||||
cancelRunningExtensionRequest,
|
||||
hasExtensionInstalled,
|
||||
} from "../strategies/ExtensionStrategy"
|
||||
|
||||
jest.mock("../strategies/AxiosStrategy", () => ({
|
||||
__esModule: true,
|
||||
default: jest.fn(() => Promise.resolve()),
|
||||
cancelRunningAxiosRequest: jest.fn(() => Promise.resolve()),
|
||||
}))
|
||||
|
||||
jest.mock("../strategies/ExtensionStrategy", () => ({
|
||||
__esModule: true,
|
||||
default: jest.fn(() => Promise.resolve()),
|
||||
cancelRunningExtensionRequest: jest.fn(() => Promise.resolve()),
|
||||
hasExtensionInstalled: jest.fn(),
|
||||
}))
|
||||
|
||||
jest.mock("~/newstore/settings", () => {
|
||||
return {
|
||||
settingsStore: {
|
||||
value: {
|
||||
EXTENSIONS_ENABLED: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
global.$nuxt = {
|
||||
$loading: {
|
||||
finish: jest.fn(() => Promise.resolve()),
|
||||
},
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks() // Reset the call count for the mock functions
|
||||
})
|
||||
|
||||
describe("cancelRunningRequest", () => {
|
||||
test("cancels only axios request if extension not allowed in settings and extension is installed", () => {
|
||||
hasExtensionInstalled.mockReturnValue(true)
|
||||
|
||||
cancelRunningRequest()
|
||||
|
||||
expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
|
||||
expect(cancelRunningAxiosRequest).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test("cancels only axios request if extension is not allowed and not installed", () => {
|
||||
hasExtensionInstalled.mockReturnValue(false)
|
||||
|
||||
cancelRunningRequest()
|
||||
|
||||
expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
|
||||
expect(cancelRunningAxiosRequest).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("sendNetworkRequest", () => {
|
||||
test("runs only axios request if extension not allowed in settings and extension is installed and clears the progress bar", async () => {
|
||||
hasExtensionInstalled.mockReturnValue(true)
|
||||
|
||||
await sendNetworkRequest({})
|
||||
|
||||
expect(ExtensionStrategy).not.toHaveBeenCalled()
|
||||
expect(AxiosStrategy).toHaveBeenCalled()
|
||||
expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test("runs only axios request if extension is not allowed and not installed and clears the progress bar", async () => {
|
||||
hasExtensionInstalled.mockReturnValue(false)
|
||||
|
||||
await sendNetworkRequest({})
|
||||
|
||||
expect(ExtensionStrategy).not.toHaveBeenCalled()
|
||||
expect(AxiosStrategy).toHaveBeenCalled()
|
||||
expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -1,83 +0,0 @@
|
||||
import { cancelRunningRequest, sendNetworkRequest } from "../network"
|
||||
|
||||
import AxiosStrategy, {
|
||||
cancelRunningAxiosRequest,
|
||||
} from "../strategies/AxiosStrategy"
|
||||
import ExtensionStrategy, {
|
||||
cancelRunningExtensionRequest,
|
||||
hasExtensionInstalled,
|
||||
} from "../strategies/ExtensionStrategy"
|
||||
|
||||
jest.mock("../strategies/AxiosStrategy", () => ({
|
||||
__esModule: true,
|
||||
default: jest.fn(() => Promise.resolve()),
|
||||
cancelRunningAxiosRequest: jest.fn(() => Promise.resolve()),
|
||||
}))
|
||||
|
||||
jest.mock("../strategies/ExtensionStrategy", () => ({
|
||||
__esModule: true,
|
||||
default: jest.fn(() => Promise.resolve()),
|
||||
cancelRunningExtensionRequest: jest.fn(() => Promise.resolve()),
|
||||
hasExtensionInstalled: jest.fn(),
|
||||
}))
|
||||
|
||||
jest.mock("~/newstore/settings", () => {
|
||||
return {
|
||||
settingsStore: {
|
||||
value: {
|
||||
EXTENSIONS_ENABLED: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
global.$nuxt = {
|
||||
$loading: {
|
||||
finish: jest.fn(() => Promise.resolve()),
|
||||
},
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks() // Reset the call count for the mock functions
|
||||
})
|
||||
|
||||
describe("cancelRunningRequest", () => {
|
||||
test("cancels only extension request if extension allowed in settings and is installed", () => {
|
||||
hasExtensionInstalled.mockReturnValue(true)
|
||||
|
||||
cancelRunningRequest()
|
||||
|
||||
expect(cancelRunningAxiosRequest).not.toHaveBeenCalled()
|
||||
expect(cancelRunningExtensionRequest).toHaveBeenCalled()
|
||||
})
|
||||
test("cancels only axios request if extension is allowed but not installed", () => {
|
||||
hasExtensionInstalled.mockReturnValue(false)
|
||||
|
||||
cancelRunningRequest()
|
||||
|
||||
expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
|
||||
expect(cancelRunningAxiosRequest).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("sendNetworkRequest", () => {
|
||||
test("runs only extension request if extension allowed in settings and is installed and clears the progress bar", async () => {
|
||||
hasExtensionInstalled.mockReturnValue(true)
|
||||
|
||||
await sendNetworkRequest({})
|
||||
|
||||
expect(AxiosStrategy).not.toHaveBeenCalled()
|
||||
expect(ExtensionStrategy).toHaveBeenCalled()
|
||||
expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test("runs only axios request if extension is allowed but not installed and clears the progress bar", async () => {
|
||||
hasExtensionInstalled.mockReturnValue(false)
|
||||
|
||||
await sendNetworkRequest({})
|
||||
|
||||
expect(ExtensionStrategy).not.toHaveBeenCalled()
|
||||
expect(AxiosStrategy).toHaveBeenCalled()
|
||||
expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,5 @@
|
||||
import axios from "axios"
|
||||
import axiosStrategy from "../AxiosStrategy"
|
||||
import { JsonFormattedError } from "~/helpers/utils/JsonFormattedError"
|
||||
|
||||
jest.mock("axios")
|
||||
jest.mock("~/newstore/settings", () => {
|
||||
@@ -20,7 +19,7 @@ axios.mockResolvedValue({})
|
||||
describe("axiosStrategy", () => {
|
||||
describe("No-Proxy Requests", () => {
|
||||
test("sends request to the actual sender if proxy disabled", async () => {
|
||||
await axiosStrategy({ url: "test" })
|
||||
await axiosStrategy({ url: "test" })()
|
||||
|
||||
expect(axios).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
@@ -30,7 +29,7 @@ describe("axiosStrategy", () => {
|
||||
})
|
||||
|
||||
test("asks axios to return data as arraybuffer", async () => {
|
||||
await axiosStrategy({ url: "test" })
|
||||
await axiosStrategy({ url: "test" })()
|
||||
|
||||
expect(axios).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
@@ -40,24 +39,24 @@ describe("axiosStrategy", () => {
|
||||
})
|
||||
|
||||
test("resolves successful requests", async () => {
|
||||
await expect(axiosStrategy({})).resolves.toBeDefined()
|
||||
expect(await axiosStrategy({})()).toBeRight()
|
||||
})
|
||||
|
||||
test("rejects cancel errors with text 'cancellation'", async () => {
|
||||
axios.isCancel.mockReturnValueOnce(true)
|
||||
axios.mockRejectedValue("err")
|
||||
|
||||
await expect(axiosStrategy({})).rejects.toBe("cancellation")
|
||||
expect(await axiosStrategy({})()).toEqualLeft("cancellation")
|
||||
})
|
||||
|
||||
test("rejects non-cancellation errors as-is", async () => {
|
||||
axios.isCancel.mockReturnValueOnce(false)
|
||||
axios.mockRejectedValue("err")
|
||||
|
||||
await expect(axiosStrategy({})).rejects.toBe("err")
|
||||
expect(await axiosStrategy({})()).toEqualLeft("err")
|
||||
})
|
||||
|
||||
test("non-cancellation errors that have response data are thrown", async () => {
|
||||
test("non-cancellation errors that have response data are right", async () => {
|
||||
const errorResponse = { error: "errr" }
|
||||
axios.isCancel.mockReturnValueOnce(false)
|
||||
axios.mockRejectedValue({
|
||||
@@ -68,9 +67,9 @@ describe("axiosStrategy", () => {
|
||||
},
|
||||
})
|
||||
|
||||
await expect(axiosStrategy({})).rejects.toMatchObject(
|
||||
new JsonFormattedError(errorResponse)
|
||||
)
|
||||
expect(await axiosStrategy({})()).toSubsetEqualRight({
|
||||
data: "eyJlcnJvciI6ImVycnIifQ==",
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import axios from "axios"
|
||||
import axiosStrategy, {
|
||||
testables,
|
||||
cancelRunningAxiosRequest,
|
||||
} from "../AxiosStrategy"
|
||||
import axiosStrategy from "../AxiosStrategy"
|
||||
|
||||
jest.mock("../../utils/b64", () => ({
|
||||
__esModule: true,
|
||||
@@ -20,15 +17,6 @@ jest.mock("~/newstore/settings", () => {
|
||||
}
|
||||
})
|
||||
|
||||
describe("cancelRunningAxiosRequest", () => {
|
||||
test("cancels axios request and does that only 1 time", () => {
|
||||
const cancelFunc = jest.spyOn(testables.cancelSource, "cancel")
|
||||
|
||||
cancelRunningAxiosRequest()
|
||||
expect(cancelFunc).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe("axiosStrategy", () => {
|
||||
describe("Proxy Requests", () => {
|
||||
test("sends POST request to proxy if proxy is enabled", async () => {
|
||||
@@ -39,7 +27,7 @@ describe("axiosStrategy", () => {
|
||||
return Promise.resolve({ data: { success: true, isBinary: false } })
|
||||
})
|
||||
|
||||
await axiosStrategy({})
|
||||
await axiosStrategy({})()
|
||||
|
||||
expect(passedURL).toEqual("test")
|
||||
})
|
||||
@@ -58,7 +46,7 @@ describe("axiosStrategy", () => {
|
||||
return Promise.resolve({ data: { success: true, isBinary: false } })
|
||||
})
|
||||
|
||||
await axiosStrategy(reqFields)
|
||||
await axiosStrategy(reqFields)()
|
||||
|
||||
expect(passedFields).toMatchObject(reqFields)
|
||||
})
|
||||
@@ -71,7 +59,7 @@ describe("axiosStrategy", () => {
|
||||
return Promise.resolve({ data: { success: true, isBinary: false } })
|
||||
})
|
||||
|
||||
await axiosStrategy({})
|
||||
await axiosStrategy({})()
|
||||
|
||||
expect(passedFields).toHaveProperty("wantsBinary")
|
||||
})
|
||||
@@ -86,7 +74,7 @@ describe("axiosStrategy", () => {
|
||||
},
|
||||
})
|
||||
|
||||
await expect(axiosStrategy({})).rejects.toThrow("test message")
|
||||
expect(await axiosStrategy({})()).toEqualLeft("test message")
|
||||
})
|
||||
|
||||
test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
|
||||
@@ -97,10 +85,10 @@ describe("axiosStrategy", () => {
|
||||
},
|
||||
})
|
||||
|
||||
await expect(axiosStrategy({})).rejects.toThrow("Proxy Error")
|
||||
expect(await axiosStrategy({})()).toBeLeft("Proxy Error")
|
||||
})
|
||||
|
||||
test("checks for proxy response success and doesn't throw for success", async () => {
|
||||
test("checks for proxy response success and doesn't left for success", async () => {
|
||||
jest.spyOn(axios, "post").mockResolvedValue({
|
||||
data: {
|
||||
success: true,
|
||||
@@ -108,10 +96,10 @@ describe("axiosStrategy", () => {
|
||||
},
|
||||
})
|
||||
|
||||
await expect(axiosStrategy({})).resolves.toBeDefined()
|
||||
expect(await axiosStrategy({})()).toBeRight()
|
||||
})
|
||||
|
||||
test("checks isBinary response field and resolve with the converted value if so", async () => {
|
||||
test("checks isBinary response field and right with the converted value if so", async () => {
|
||||
jest.spyOn(axios, "post").mockResolvedValue({
|
||||
data: {
|
||||
success: true,
|
||||
@@ -120,12 +108,12 @@ describe("axiosStrategy", () => {
|
||||
},
|
||||
})
|
||||
|
||||
await expect(axiosStrategy({})).resolves.toMatchObject({
|
||||
expect(await axiosStrategy({})()).toSubsetEqualRight({
|
||||
data: "testdata-converted",
|
||||
})
|
||||
})
|
||||
|
||||
test("checks isBinary response field and resolve with the actual value if not so", async () => {
|
||||
test("checks isBinary response field and right with the actual value if not so", async () => {
|
||||
jest.spyOn(axios, "post").mockResolvedValue({
|
||||
data: {
|
||||
success: true,
|
||||
@@ -134,23 +122,23 @@ describe("axiosStrategy", () => {
|
||||
},
|
||||
})
|
||||
|
||||
await expect(axiosStrategy({})).resolves.toMatchObject({
|
||||
expect(await axiosStrategy({})()).toSubsetEqualRight({
|
||||
data: "testdata",
|
||||
})
|
||||
})
|
||||
|
||||
test("cancel errors are thrown with the string 'cancellation'", async () => {
|
||||
test("cancel errors are returned a left with the string 'cancellation'", async () => {
|
||||
jest.spyOn(axios, "post").mockRejectedValue("errr")
|
||||
jest.spyOn(axios, "isCancel").mockReturnValueOnce(true)
|
||||
|
||||
await expect(axiosStrategy({})).rejects.toBe("cancellation")
|
||||
expect(await axiosStrategy({})()).toEqualLeft("cancellation")
|
||||
})
|
||||
|
||||
test("non-cancellation errors are thrown", async () => {
|
||||
test("non-cancellation errors return a left", async () => {
|
||||
jest.spyOn(axios, "post").mockRejectedValue("errr")
|
||||
jest.spyOn(axios, "isCancel").mockReturnValueOnce(false)
|
||||
|
||||
await expect(axiosStrategy({})).rejects.toBe("errr")
|
||||
expect(await axiosStrategy({})()).toEqualLeft("errr")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -148,7 +148,7 @@ describe("extensionStrategy", () => {
|
||||
data: '{"success":true,"data":""}',
|
||||
})
|
||||
|
||||
await extensionStrategy({})
|
||||
await extensionStrategy({})()
|
||||
|
||||
expect(sendReqFunc).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
@@ -168,7 +168,7 @@ describe("extensionStrategy", () => {
|
||||
})
|
||||
})
|
||||
|
||||
await extensionStrategy({ url: "test" })
|
||||
await extensionStrategy({ url: "test" })()
|
||||
|
||||
expect(passedUrl).toEqual("test")
|
||||
})
|
||||
@@ -188,12 +188,12 @@ describe("extensionStrategy", () => {
|
||||
})
|
||||
})
|
||||
|
||||
await extensionStrategy({})
|
||||
await extensionStrategy({})()
|
||||
|
||||
expect(passedFields).toHaveProperty("wantsBinary")
|
||||
})
|
||||
|
||||
test("resolves successful requests", async () => {
|
||||
test("rights successful requests", async () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
@@ -202,7 +202,7 @@ describe("extensionStrategy", () => {
|
||||
data: '{"success":true,"data":""}',
|
||||
})
|
||||
|
||||
await expect(extensionStrategy({})).resolves.toBeDefined()
|
||||
expect(await extensionStrategy({})()).toBeRight()
|
||||
})
|
||||
|
||||
test("rejects errors as-is", async () => {
|
||||
@@ -212,7 +212,7 @@ describe("extensionStrategy", () => {
|
||||
|
||||
sendReqFunc.mockRejectedValue("err")
|
||||
|
||||
await expect(extensionStrategy({})).rejects.toBe("err")
|
||||
expect(await extensionStrategy({})()).toEqualLeft("err")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,300 +0,0 @@
|
||||
import extensionStrategy, {
|
||||
hasExtensionInstalled,
|
||||
hasChromeExtensionInstalled,
|
||||
hasFirefoxExtensionInstalled,
|
||||
cancelRunningExtensionRequest,
|
||||
} from "../ExtensionStrategy"
|
||||
|
||||
jest.mock("../../utils/b64", () => ({
|
||||
__esModule: true,
|
||||
decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
|
||||
}))
|
||||
|
||||
jest.mock("~/newstore/settings", () => {
|
||||
return {
|
||||
__esModule: true,
|
||||
settingsStore: {
|
||||
value: {
|
||||
EXTENSIONS_ENABLED: true,
|
||||
PROXY_ENABLED: true,
|
||||
PROXY_URL: "test",
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
describe("hasExtensionInstalled", () => {
|
||||
test("returns true if extension is present and hooked", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
|
||||
|
||||
expect(hasExtensionInstalled()).toEqual(true)
|
||||
})
|
||||
|
||||
test("returns false if extension not present or not hooked", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
|
||||
|
||||
expect(hasExtensionInstalled()).toEqual(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("hasChromeExtensionInstalled", () => {
|
||||
test("returns true if extension is hooked and browser is chrome", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
|
||||
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
|
||||
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
|
||||
|
||||
expect(hasChromeExtensionInstalled()).toEqual(true)
|
||||
})
|
||||
|
||||
test("returns false if extension is hooked and browser is not chrome", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
|
||||
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
|
||||
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
|
||||
|
||||
expect(hasChromeExtensionInstalled()).toEqual(false)
|
||||
})
|
||||
|
||||
test("returns false if extension not installed and browser is chrome", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
|
||||
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
|
||||
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
|
||||
|
||||
expect(hasChromeExtensionInstalled()).toEqual(false)
|
||||
})
|
||||
|
||||
test("returns false if extension not installed and browser is not chrome", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
|
||||
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
|
||||
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
|
||||
|
||||
expect(hasChromeExtensionInstalled()).toEqual(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("hasFirefoxExtensionInstalled", () => {
|
||||
test("returns true if extension is hooked and browser is firefox", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
|
||||
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
|
||||
|
||||
expect(hasFirefoxExtensionInstalled()).toEqual(true)
|
||||
})
|
||||
|
||||
test("returns false if extension is hooked and browser is not firefox", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
|
||||
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
|
||||
|
||||
expect(hasFirefoxExtensionInstalled()).toEqual(false)
|
||||
})
|
||||
|
||||
test("returns false if extension not installed and browser is firefox", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
|
||||
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
|
||||
|
||||
expect(hasFirefoxExtensionInstalled()).toEqual(false)
|
||||
})
|
||||
|
||||
test("returns false if extension not installed and browser is not firefox", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
|
||||
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
|
||||
|
||||
expect(hasFirefoxExtensionInstalled()).toEqual(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("cancelRunningExtensionRequest", () => {
|
||||
const cancelFunc = jest.fn()
|
||||
|
||||
beforeEach(() => {
|
||||
cancelFunc.mockClear()
|
||||
})
|
||||
|
||||
test("cancels request if extension installed and function present in hook", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
cancelRunningRequest: cancelFunc,
|
||||
}
|
||||
|
||||
cancelRunningExtensionRequest()
|
||||
expect(cancelFunc).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test("does not cancel request if extension not installed", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
|
||||
|
||||
cancelRunningExtensionRequest()
|
||||
expect(cancelFunc).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test("does not cancel request if extension installed but function not present", () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
|
||||
|
||||
cancelRunningExtensionRequest()
|
||||
expect(cancelFunc).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("extensionStrategy", () => {
|
||||
const sendReqFunc = jest.fn()
|
||||
|
||||
beforeEach(() => {
|
||||
sendReqFunc.mockClear()
|
||||
})
|
||||
|
||||
describe("Proxy Requests", () => {
|
||||
test("asks extension to send request", async () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
|
||||
sendReqFunc.mockResolvedValue({
|
||||
data: '{"success":true,"data":""}',
|
||||
})
|
||||
|
||||
await extensionStrategy({})
|
||||
|
||||
expect(sendReqFunc).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test("sends POST request to proxy if proxy is enabled", async () => {
|
||||
let passedUrl
|
||||
let passedMethod
|
||||
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
|
||||
sendReqFunc.mockImplementation(({ method, url }) => {
|
||||
passedUrl = url
|
||||
passedMethod = method
|
||||
|
||||
return Promise.resolve({
|
||||
data: '{"success":true,"data":""}',
|
||||
})
|
||||
})
|
||||
|
||||
await extensionStrategy({})
|
||||
|
||||
expect(passedUrl).toEqual("test")
|
||||
expect(passedMethod).toEqual("post")
|
||||
})
|
||||
|
||||
test("passes request fields properly", async () => {
|
||||
const reqFields = {
|
||||
testA: "testA",
|
||||
testB: "testB",
|
||||
testC: "testC",
|
||||
}
|
||||
|
||||
let passedFields
|
||||
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
|
||||
sendReqFunc.mockImplementation(({ data }) => {
|
||||
passedFields = data
|
||||
|
||||
return Promise.resolve({
|
||||
data: '{"success":true,"data":""}',
|
||||
})
|
||||
})
|
||||
|
||||
await extensionStrategy(reqFields)
|
||||
|
||||
expect(passedFields).toMatchObject(reqFields)
|
||||
})
|
||||
|
||||
test("passes wantsBinary field", async () => {
|
||||
let passedFields
|
||||
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
|
||||
sendReqFunc.mockImplementation(({ data }) => {
|
||||
passedFields = data
|
||||
|
||||
return Promise.resolve({
|
||||
data: '{"success":true,"data":""}',
|
||||
})
|
||||
})
|
||||
|
||||
await extensionStrategy({})
|
||||
|
||||
expect(passedFields).toHaveProperty("wantsBinary")
|
||||
})
|
||||
|
||||
test("checks for proxy response success field and throws error message for non-success", async () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
|
||||
sendReqFunc.mockResolvedValue({
|
||||
data: '{"success":false,"data": { "message": "testerr" } }',
|
||||
})
|
||||
|
||||
await expect(extensionStrategy({})).rejects.toThrow("testerr")
|
||||
})
|
||||
|
||||
test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
|
||||
sendReqFunc.mockResolvedValue({
|
||||
data: '{"success":false,"data": {} }',
|
||||
})
|
||||
|
||||
await expect(extensionStrategy({})).rejects.toThrow("Proxy Error")
|
||||
})
|
||||
|
||||
test("checks for proxy response success and doesn't throw for success", async () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
|
||||
sendReqFunc.mockResolvedValue({
|
||||
data: '{"success":true,"data": {} }',
|
||||
})
|
||||
|
||||
await expect(extensionStrategy({})).resolves.toBeDefined()
|
||||
})
|
||||
|
||||
test("checks isBinary response field and resolve with the converted value if so", async () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
|
||||
sendReqFunc.mockResolvedValue({
|
||||
data: '{"success": true, "isBinary": true, "data": "testdata" }',
|
||||
})
|
||||
|
||||
await expect(extensionStrategy({})).resolves.toMatchObject({
|
||||
data: "testdata-converted",
|
||||
})
|
||||
})
|
||||
|
||||
test("checks isBinary response field and resolve with the actual value if not so", async () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
|
||||
sendReqFunc.mockResolvedValue({
|
||||
data: '{"success": true, "isBinary": false, "data": "testdata" }',
|
||||
})
|
||||
|
||||
await expect(extensionStrategy({})).resolves.toMatchObject({
|
||||
data: "testdata",
|
||||
})
|
||||
})
|
||||
|
||||
test("failed request errors are thrown as-is", async () => {
|
||||
global.__POSTWOMAN_EXTENSION_HOOK__ = {
|
||||
sendRequest: sendReqFunc,
|
||||
}
|
||||
|
||||
sendReqFunc.mockRejectedValue("err")
|
||||
|
||||
await expect(extensionStrategy({})).rejects.toBe("err")
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user