AxiosStrategy-NoProxy.spec.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import axios from "axios"
  2. import axiosStrategy from "../AxiosStrategy"
  3. import { JsonFormattedError } from "~/helpers/utils/JsonFormattedError"
  4. jest.mock("axios")
  5. jest.mock("~/newstore/settings", () => {
  6. return {
  7. __esModule: true,
  8. settingsStore: {
  9. value: {
  10. PROXY_ENABLED: false,
  11. },
  12. },
  13. }
  14. })
  15. axios.CancelToken.source.mockReturnValue({ token: "test" })
  16. axios.mockResolvedValue({})
  17. describe("axiosStrategy", () => {
  18. describe("No-Proxy Requests", () => {
  19. test("sends request to the actual sender if proxy disabled", async () => {
  20. await axiosStrategy({ url: "test" })
  21. expect(axios).toBeCalledWith(
  22. expect.objectContaining({
  23. url: "test",
  24. })
  25. )
  26. })
  27. test("asks axios to return data as arraybuffer", async () => {
  28. await axiosStrategy({ url: "test" })
  29. expect(axios).toBeCalledWith(
  30. expect.objectContaining({
  31. responseType: "arraybuffer",
  32. })
  33. )
  34. })
  35. test("resolves successful requests", async () => {
  36. await expect(axiosStrategy({})).resolves.toBeDefined()
  37. })
  38. test("rejects cancel errors with text 'cancellation'", async () => {
  39. axios.isCancel.mockReturnValueOnce(true)
  40. axios.mockRejectedValue("err")
  41. await expect(axiosStrategy({})).rejects.toBe("cancellation")
  42. })
  43. test("rejects non-cancellation errors as-is", async () => {
  44. axios.isCancel.mockReturnValueOnce(false)
  45. axios.mockRejectedValue("err")
  46. await expect(axiosStrategy({})).rejects.toBe("err")
  47. })
  48. test("non-cancellation errors that have response data are thrown", async () => {
  49. const errorResponse = { error: "errr" }
  50. axios.isCancel.mockReturnValueOnce(false)
  51. axios.mockRejectedValue({
  52. response: {
  53. data: Buffer.from(JSON.stringify(errorResponse), "utf8").toString(
  54. "base64"
  55. ),
  56. },
  57. })
  58. await expect(axiosStrategy({})).rejects.toMatchObject(
  59. new JsonFormattedError(errorResponse)
  60. )
  61. })
  62. })
  63. })