AxiosStrategy-NoProxy.spec.js 1.9 KB

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