cosmos-explorer/src/Utils/arm/request.test.ts
Laurent Nguyen 90c1439d34
Update prettier to latest. Remove tslint (#1641)
* Rev up prettier

* Reformat

* Remove deprecated tslint

* Remove call to tslint and update package-lock.json
2023-10-03 17:13:24 +02:00

79 lines
2.3 KiB
TypeScript

import { armRequest } from "./request";
import fetch from "node-fetch";
import { updateUserContext } from "../../UserContext";
import { AuthType } from "../../AuthType";
interface Global {
Headers: unknown;
}
(global as unknown as Global).Headers = (fetch as unknown as Global).Headers;
describe("ARM request", () => {
updateUserContext({
authType: AuthType.AAD,
authorizationToken: "some-token",
});
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);
});
it("should throw token error", async () => {
updateUserContext({
authType: AuthType.AAD,
authorizationToken: undefined,
});
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("No authority token provided");
});
});