2020-08-03 17:11:07 -05:00
|
|
|
import { armRequest } from "./request";
|
2020-10-01 14:00:46 +02:00
|
|
|
import fetch from "node-fetch";
|
|
|
|
|
|
|
|
interface Global {
|
|
|
|
Headers: unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
((global as unknown) as Global).Headers = ((fetch as unknown) as Global).Headers;
|
2020-08-03 17:11:07 -05:00
|
|
|
|
|
|
|
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();
|
2020-11-02 16:59:08 -08:00
|
|
|
headers.set("location", "https://foo.com/operationStatus");
|
2020-08-03 17:11:07 -05:00
|
|
|
window.fetch = jest.fn().mockResolvedValue({
|
|
|
|
ok: true,
|
|
|
|
headers,
|
2020-11-02 16:59:08 -08:00
|
|
|
status: 200,
|
|
|
|
json: async () => ({})
|
2020-08-03 17:11:07 -05:00
|
|
|
});
|
|
|
|
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();
|
2020-11-02 16:59:08 -08:00
|
|
|
headers.set("location", "https://foo.com/operationStatus");
|
2020-08-03 17:11:07 -05:00
|
|
|
window.fetch = jest.fn().mockResolvedValue({
|
|
|
|
ok: true,
|
|
|
|
headers,
|
2020-11-02 16:59:08 -08:00
|
|
|
status: 200,
|
2020-08-03 17:11:07 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|