test: tighten msteams graph assertions

This commit is contained in:
Peter Steinberger
2026-05-11 07:00:08 +01:00
parent ebe0f12b82
commit bbd7b3ada5
+22 -33
View File
@@ -105,6 +105,16 @@ function fetchCallUrl(index: number) {
return requestUrl(input);
}
function fetchCallInit(index: number) {
return vi.mocked(globalThis.fetch).mock.calls[index]?.[1];
}
function fetchCallHeader(index: number, name: string) {
const headers = fetchCallInit(index)?.headers;
expect(headers).toBeDefined();
return (headers as Record<string, string> | undefined)?.[name];
}
function expectFetchPathContains(index: number, expectedPath: string) {
expect(fetchCallUrl(index)).toContain(expectedPath);
}
@@ -168,15 +178,9 @@ describe("msteams graph helpers", () => {
}),
).resolves.toEqual(graphCollection(groupOne));
expect(globalThis.fetch).toHaveBeenCalledWith(
"https://graph.microsoft.com/v1.0/groups?$select=id",
{
headers: expect.objectContaining({
Authorization: `Bearer ${graphToken}`,
ConsistencyLevel: "eventual",
}),
},
);
expect(fetchCallUrl(0)).toBe("https://graph.microsoft.com/v1.0/groups?$select=id");
expect(fetchCallHeader(0, "Authorization")).toBe(`Bearer ${graphToken}`);
expect(fetchCallHeader(0, "ConsistencyLevel")).toBe("eventual");
mockTextFetchResponse("forbidden", { status: 403 });
@@ -213,26 +217,16 @@ describe("msteams graph helpers", () => {
}),
).resolves.toBeUndefined();
expect(globalThis.fetch).toHaveBeenNthCalledWith(
1,
"https://graph.microsoft.com/v1.0/chats/chat-1/pinnedMessages",
expect.objectContaining({
method: "POST",
body: JSON.stringify({ messageId: "msg-1" }),
headers: expect.objectContaining({
Authorization: `Bearer ${graphToken}`,
"Content-Type": "application/json",
}),
}),
);
expect(globalThis.fetch).toHaveBeenNthCalledWith(
2,
expect(fetchCallUrl(0)).toBe("https://graph.microsoft.com/v1.0/chats/chat-1/pinnedMessages");
expect(fetchCallInit(0)?.method).toBe("POST");
expect(fetchCallInit(0)?.body).toBe(JSON.stringify({ messageId: "msg-1" }));
expect(fetchCallHeader(0, "Authorization")).toBe(`Bearer ${graphToken}`);
expect(fetchCallHeader(0, "Content-Type")).toBe("application/json");
expect(fetchCallUrl(1)).toBe(
"https://graph.microsoft.com/beta/chats/chat-1/messages/msg-1/setReaction",
expect.objectContaining({
method: "POST",
body: JSON.stringify({ reactionType: "like" }),
}),
);
expect(fetchCallInit(1)?.method).toBe("POST");
expect(fetchCallInit(1)?.body).toBe(JSON.stringify({ reactionType: "like" }));
});
it("surfaces POST and DELETE graph failures with method-specific labels", async () => {
@@ -335,16 +329,11 @@ describe("msteams graph helpers", () => {
});
await expectSearchGraphUsers("carol", [], { token: "token-4" });
const calls = vi.mocked(globalThis.fetch).mock.calls;
expectFetchPathContains(
0,
"/users?$search=%22displayName%3Abob%22&$select=id,displayName,mail,userPrincipalName&$top=25",
);
expect(calls[0]?.[1]).toEqual(
expect.objectContaining({
headers: expect.objectContaining({ ConsistencyLevel: "eventual" }),
}),
);
expect(fetchCallHeader(0, "ConsistencyLevel")).toBe("eventual");
expectFetchPathContains(
1,
"/users?$search=%22displayName%3Acarol%22&$select=id,displayName,mail,userPrincipalName&$top=10",