Files
hoppscotch/helpers/strategies/__tests__/AxiosStrategy-Proxy.spec.js
Andrew Bastin 5fce1118f6 Revamp of the Settings State System along with TypeScript support (#1560)
* Add vue-rx, rxjs and lodash as dependencies

* Added vue-rx plugin integration to nuxt config

* Initial settings store implementation

* Add babel plugin for private class properties to for Jest

* Add DispatchingStore test spec

* Initial settings code

* Reactive Streams for fb current user and id token

* Fix typo

* Migrate index and graphql pages to the new store

* Migrate network strategy to the new store

* Fixed Section.vue errors

* Fix getSettingSubject issue

* Migrate fb settings reference in components to the new state system

* Add typings for lodash as dev dependency

* Load setting

* Load initial sync setting values

* Update proxy url

* Add typescript support

* Rewrite Settings store to TypeScript

* Port Settings page to TypeScript as reference

* Move all store migrations to a separate file

* Delete test file for fb.js

* Add ts-jest as dev dependency

* Remove firebase-mock as dependency

* Remove FRAME_COLORS_ENABLED settings value
2021-03-23 11:18:14 -04:00

155 lines
4.1 KiB
JavaScript

import axios from "axios"
import axiosStrategy, { testables, cancelRunningAxiosRequest } from "../AxiosStrategy"
jest.mock("../../utils/b64", () => ({
__esModule: true,
decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
}))
jest.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
value: {
PROXY_ENABLED: true,
PROXY_URL: "test"
}
}
}
})
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 () => {
let passedURL
jest.spyOn(axios, "post").mockImplementation((url) => {
passedURL = url
return Promise.resolve({ data: { success: true, isBinary: false } })
})
await axiosStrategy({})
expect(passedURL).toEqual("test")
})
test("passes request fields to axios properly", async () => {
const reqFields = {
testA: "testA",
testB: "testB",
testC: "testC",
}
let passedFields
jest.spyOn(axios, "post").mockImplementation((_url, req) => {
passedFields = req
return Promise.resolve({ data: { success: true, isBinary: false } })
})
await axiosStrategy(reqFields)
expect(passedFields).toMatchObject(reqFields)
})
test("passes wantsBinary field", async () => {
let passedFields
jest.spyOn(axios, "post").mockImplementation((_url, req) => {
passedFields = req
return Promise.resolve({ data: { success: true, isBinary: false } })
})
await axiosStrategy({})
expect(passedFields).toHaveProperty("wantsBinary")
})
test("checks for proxy response success field and throws error message for non-success", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: {
success: false,
data: {
message: "test message",
},
},
})
await expect(axiosStrategy({})).rejects.toThrow("test message")
})
test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: {
success: false,
data: {},
},
})
await expect(axiosStrategy({})).rejects.toThrow("Proxy Error")
})
test("checks for proxy response success and doesn't throw for success", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: {
success: true,
data: {},
},
})
await expect(axiosStrategy({})).resolves.toBeDefined()
})
test("checks isBinary response field and resolve with the converted value if so", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: {
success: true,
isBinary: true,
data: "testdata",
},
})
await expect(axiosStrategy({})).resolves.toMatchObject({
data: "testdata-converted",
})
})
test("checks isBinary response field and resolve with the actual value if not so", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: {
success: true,
isBinary: false,
data: "testdata",
},
})
await expect(axiosStrategy({})).resolves.toMatchObject({
data: "testdata",
})
})
test("cancel errors are thrown with the string 'cancellation'", async () => {
jest.spyOn(axios, "post").mockRejectedValue("errr")
jest.spyOn(axios, "isCancel").mockReturnValueOnce(true)
await expect(axiosStrategy({})).rejects.toBe("cancellation")
})
test("non-cancellation errors are thrown", async () => {
jest.spyOn(axios, "post").mockRejectedValue("errr")
jest.spyOn(axios, "isCancel").mockReturnValueOnce(false)
await expect(axiosStrategy({})).rejects.toBe("errr")
})
})
})