import axios from "axios"; import { dnetwork } from "../src/dnetwork"; // Mock axios jest.mock("axios"); const mockedAxios = axios as jest.Mocked; describe("dnetwork", () => { beforeEach(() => { jest.clearAllMocks(); }); it("should perform a GET request and return data", async () => { mockedAxios.get.mockResolvedValueOnce({ status: 200, data: { foo: "bar" }, }); const data = await dnetwork.get("http://test.com"); expect(data).toEqual({ foo: "bar" }); expect(mockedAxios.get).toHaveBeenCalledWith("http://test.com", undefined); }); it("should throw if GET request fails", async () => { mockedAxios.get.mockResolvedValueOnce({ status: 404, data: {} }); await expect(dnetwork.get("http://fail.com")).rejects.toThrow( "GET request to URL http://fail.com failed with status 404" ); }); it("should perform a GET request and parse JSON", async () => { mockedAxios.get.mockResolvedValueOnce({ status: 200, data: { hello: "world" }, }); const data = await dnetwork.getJson("http://json.com"); expect(data).toEqual({ hello: "world" }); }); it("should parse string JSON response in getJson", async () => { mockedAxios.get.mockResolvedValueOnce({ status: 200, data: '{"a":1}' }); const data = await dnetwork.getJson("http://jsonstring.com"); expect(data).toEqual({ a: 1 }); }); it("should throw if getJson response is not valid JSON", async () => { mockedAxios.get.mockResolvedValueOnce({ status: 200, data: "notjson" }); await expect(dnetwork.getJson("http://badjson.com")).rejects.toThrow( "Response is not valid JSON" ); }); it("should perform a POST request and return data", async () => { mockedAxios.post.mockResolvedValueOnce({ status: 201, data: { ok: true } }); const data = await dnetwork.post("http://post.com", { x: 1 }); expect(data).toEqual({ ok: true }); expect(mockedAxios.post).toHaveBeenCalledWith( "http://post.com", { x: 1 }, undefined ); }); it("should throw if POST request fails", async () => { mockedAxios.post.mockResolvedValueOnce({ status: 500, data: {} }); await expect(dnetwork.post("http://failpost.com", {})).rejects.toThrow( "POST request to URL http://failpost.com failed with status 500" ); }); it("should perform a POST request and parse JSON", async () => { mockedAxios.post.mockResolvedValueOnce({ status: 200, data: { ok: 1 } }); const data = await dnetwork.postJson("http://postjson.com", { y: 2 }); expect(data).toEqual({ ok: 1 }); }); it("should parse string JSON response in postJson", async () => { mockedAxios.post.mockResolvedValueOnce({ status: 200, data: '{"b":2}' }); const data = await dnetwork.postJson("http://postjsonstring.com", { y: 2 }); expect(data).toEqual({ b: 2 }); }); it("should throw if postJson response is not valid JSON", async () => { mockedAxios.post.mockResolvedValueOnce({ status: 200, data: "notjson" }); await expect( dnetwork.postJson("http://badpostjson.com", {}) ).rejects.toThrow("Response is not valid JSON"); }); it("should get SimpleStore success", async () => { mockedAxios.get.mockResolvedValueOnce({ status: 200, data: { status: "success", value: 42 }, }); const data = await dnetwork.getSimpleStore("http://store.com"); expect(data).toEqual({ status: "success", value: 42 }); }); it("should throw on SimpleStore GET failure", async () => { mockedAxios.get.mockResolvedValueOnce({ status: 200, data: { status: "fail", msg: "bad" }, }); await expect( dnetwork.getSimpleStore("http://failstore.com") ).rejects.toThrow("bad"); }); it("should post SimpleStore success", async () => { mockedAxios.post.mockResolvedValueOnce({ status: 200, data: { status: "success", value: 99 }, }); const data = await dnetwork.postSimpleStore("http://store.com", { z: 3 }); expect(data).toEqual({ status: "success", value: 99 }); }); it("should throw on SimpleStore POST failure", async () => { mockedAxios.post.mockResolvedValueOnce({ status: 200, data: { status: "error", msg: "failmsg" }, }); await expect( dnetwork.postSimpleStore("http://failstore.com", {}) ).rejects.toThrow("failmsg"); }); it("should perform a GET as fake browser", async () => { mockedAxios.get.mockResolvedValueOnce({ status: 200, data: "browser" }); const data = await dnetwork.getAsFakeBrowser("http://browser.com"); expect(data).toBe("browser"); expect(mockedAxios.get).toHaveBeenCalledWith( "http://browser.com", expect.objectContaining({ headers: expect.objectContaining({ "user-agent": expect.any(String), }), }) ); }); });