AxiosStrategy-NoProxy.spec.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. await expect(axiosStrategy({})).resolves.toBeDefined()
  36. })
  37. test("rejects cancel errors with text 'cancellation'", () => {
  38. axios.isCancel.mockReturnValueOnce(true)
  39. axios.mockRejectedValue("err")
  40. expect(axiosStrategy({})).rejects.toBe("cancellation")
  41. })
  42. test("rejects non-cancellation errors as-is", () => {
  43. axios.isCancel.mockReturnValueOnce(false)
  44. axios.mockRejectedValue("err")
  45. expect(axiosStrategy({})).rejects.toBe("err")
  46. })
  47. })
  48. })