123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import axios from "axios"
- import axiosStrategy from "../AxiosStrategy"
- jest.mock("axios")
- jest.mock("~/newstore/settings", () => {
- return {
- __esModule: true,
- settingsStore: {
- value: {
- PROXY_ENABLED: false,
- },
- },
- }
- })
- axios.CancelToken.source.mockReturnValue({ token: "test" })
- axios.mockResolvedValue({})
- describe("axiosStrategy", () => {
- describe("No-Proxy Requests", () => {
- test("sends request to the actual sender if proxy disabled", async () => {
- await axiosStrategy({ url: "test" })()
- expect(axios).toBeCalledWith(
- expect.objectContaining({
- url: "test",
- })
- )
- })
- test("asks axios to return data as arraybuffer", async () => {
- await axiosStrategy({ url: "test" })()
- expect(axios).toBeCalledWith(
- expect.objectContaining({
- responseType: "arraybuffer",
- })
- )
- })
- test("resolves successful requests", async () => {
- expect(await axiosStrategy({})()).toBeRight()
- })
- test("rejects cancel errors with text 'cancellation'", async () => {
- axios.isCancel.mockReturnValueOnce(true)
- axios.mockRejectedValue("err")
- expect(await axiosStrategy({})()).toEqualLeft("cancellation")
- })
- test("rejects non-cancellation errors as-is", async () => {
- axios.isCancel.mockReturnValueOnce(false)
- axios.mockRejectedValue("err")
- expect(await axiosStrategy({})()).toEqualLeft("err")
- })
- test("non-cancellation errors that have response data are right", async () => {
- const errorResponse = { error: "errr" }
- axios.isCancel.mockReturnValueOnce(false)
- axios.mockRejectedValue({
- response: {
- data: Buffer.from(JSON.stringify(errorResponse), "utf8").toString(
- "base64"
- ),
- },
- })
- expect(await axiosStrategy({})()).toSubsetEqualRight({
- data: "eyJlcnJvciI6ImVycnIifQ==",
- })
- })
- })
- })
|