123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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")
- })
- })
- })
|