AxiosStrategy-Proxy.spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import axios from "axios"
  2. import axiosStrategy from "../AxiosStrategy"
  3. jest.mock("../../utils/b64", () => ({
  4. __esModule: true,
  5. decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
  6. }))
  7. jest.mock("~/newstore/settings", () => {
  8. return {
  9. __esModule: true,
  10. settingsStore: {
  11. value: {
  12. PROXY_ENABLED: true,
  13. PROXY_URL: "test",
  14. },
  15. },
  16. }
  17. })
  18. describe("axiosStrategy", () => {
  19. describe("Proxy Requests", () => {
  20. test("sends POST request to proxy if proxy is enabled", async () => {
  21. let passedURL
  22. jest.spyOn(axios, "post").mockImplementation((url) => {
  23. passedURL = url
  24. return Promise.resolve({ data: { success: true, isBinary: false } })
  25. })
  26. await axiosStrategy({})()
  27. expect(passedURL).toEqual("test")
  28. })
  29. test("passes request fields to axios properly", async () => {
  30. const reqFields = {
  31. testA: "testA",
  32. testB: "testB",
  33. testC: "testC",
  34. }
  35. let passedFields
  36. jest.spyOn(axios, "post").mockImplementation((_url, req) => {
  37. passedFields = req
  38. return Promise.resolve({ data: { success: true, isBinary: false } })
  39. })
  40. await axiosStrategy(reqFields)()
  41. expect(passedFields).toMatchObject(reqFields)
  42. })
  43. test("passes wantsBinary field", async () => {
  44. let passedFields
  45. jest.spyOn(axios, "post").mockImplementation((_url, req) => {
  46. passedFields = req
  47. return Promise.resolve({ data: { success: true, isBinary: false } })
  48. })
  49. await axiosStrategy({})()
  50. expect(passedFields).toHaveProperty("wantsBinary")
  51. })
  52. test("checks for proxy response success field and throws error message for non-success", async () => {
  53. jest.spyOn(axios, "post").mockResolvedValue({
  54. data: {
  55. success: false,
  56. data: {
  57. message: "test message",
  58. },
  59. },
  60. })
  61. expect(await axiosStrategy({})()).toEqualLeft("test message")
  62. })
  63. test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
  64. jest.spyOn(axios, "post").mockResolvedValue({
  65. data: {
  66. success: false,
  67. data: {},
  68. },
  69. })
  70. expect(await axiosStrategy({})()).toBeLeft("Proxy Error")
  71. })
  72. test("checks for proxy response success and doesn't left for success", async () => {
  73. jest.spyOn(axios, "post").mockResolvedValue({
  74. data: {
  75. success: true,
  76. data: {},
  77. },
  78. })
  79. expect(await axiosStrategy({})()).toBeRight()
  80. })
  81. test("checks isBinary response field and right with the converted value if so", async () => {
  82. jest.spyOn(axios, "post").mockResolvedValue({
  83. data: {
  84. success: true,
  85. isBinary: true,
  86. data: "testdata",
  87. },
  88. })
  89. expect(await axiosStrategy({})()).toSubsetEqualRight({
  90. data: "testdata-converted",
  91. })
  92. })
  93. test("checks isBinary response field and right with the actual value if not so", async () => {
  94. jest.spyOn(axios, "post").mockResolvedValue({
  95. data: {
  96. success: true,
  97. isBinary: false,
  98. data: "testdata",
  99. },
  100. })
  101. expect(await axiosStrategy({})()).toSubsetEqualRight({
  102. data: "testdata",
  103. })
  104. })
  105. test("cancel errors are returned a left with the string 'cancellation'", async () => {
  106. jest.spyOn(axios, "post").mockRejectedValue("errr")
  107. jest.spyOn(axios, "isCancel").mockReturnValueOnce(true)
  108. expect(await axiosStrategy({})()).toEqualLeft("cancellation")
  109. })
  110. test("non-cancellation errors return a left", async () => {
  111. jest.spyOn(axios, "post").mockRejectedValue("errr")
  112. jest.spyOn(axios, "isCancel").mockReturnValueOnce(false)
  113. expect(await axiosStrategy({})()).toEqualLeft("errr")
  114. })
  115. })
  116. })