cosmos-explorer/src/Utils/arm/request.test.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

import { armRequest } from "./request";
import fetch from "node-fetch";
interface Global {
Headers: unknown;
}
((global as unknown) as Global).Headers = ((fetch as unknown) as Global).Headers;
describe("ARM request", () => {
it("should call window.fetch", async () => {
window.fetch = jest.fn().mockResolvedValue({
ok: true,
json: async () => {
return {};
}
});
await armRequest({ apiVersion: "2001-01-01", host: "https://foo.com", path: "foo", method: "GET" });
expect(window.fetch).toHaveBeenCalled();
});
it("should poll for async operations", async () => {
const headers = new Headers();
headers.set("location", "https://foo.com/operationStatus");
window.fetch = jest.fn().mockResolvedValue({
ok: true,
headers,
status: 200,
json: async () => ({})
});
await armRequest({ apiVersion: "2001-01-01", host: "https://foo.com", path: "foo", method: "GET" });
expect(window.fetch).toHaveBeenCalledTimes(2);
});
it("should throw for failed async operations", async () => {
const headers = new Headers();
headers.set("location", "https://foo.com/operationStatus");
window.fetch = jest.fn().mockResolvedValue({
ok: true,
headers,
status: 200,
json: async () => {
return { status: "Failed" };
}
});
await expect(() =>
armRequest({ apiVersion: "2001-01-01", host: "https://foo.com", path: "foo", method: "GET" })
).rejects.toThrow();
expect(window.fetch).toHaveBeenCalledTimes(2);
});
});