Skip to content

Commit 7121791

Browse files
committed
test: dedupe matrix client mock calls
1 parent 188fa93 commit 7121791

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

extensions/matrix/src/matrix/client.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,27 @@ function expectAuthFields(auth: unknown, fields: Record<string, unknown>) {
6363
expectRecordFields(requireRecord(auth, "Matrix auth"), fields);
6464
}
6565

66+
function mockCall(mock: ReturnType<typeof vi.fn>, index = 0): unknown[] {
67+
const call = mock.mock.calls.at(index);
68+
if (!call) {
69+
throw new Error(`missing mock call ${index}`);
70+
}
71+
return call;
72+
}
73+
6674
function expectSavedCredentials(
6775
mock: ReturnType<typeof vi.fn>,
6876
fields: Record<string, unknown>,
6977
accountId: string,
7078
) {
71-
const call = mock.mock.calls[0] as unknown[] | undefined;
72-
if (!call) {
73-
throw new Error("missing save credentials call");
74-
}
79+
const call = mockCall(mock);
7580
expectRecordFields(requireRecord(call[0], "Matrix credentials"), fields);
7681
requireRecord(call[1], "Matrix credential save options");
7782
expect(call[2]).toBe(accountId);
7883
}
7984

8085
function expectMatrixLoginCall(fields: Record<string, unknown>) {
81-
const call = matrixDoRequestMock.mock.calls[0] as unknown[] | undefined;
82-
if (!call) {
83-
throw new Error("missing Matrix login call");
84-
}
86+
const call = mockCall(matrixDoRequestMock);
8587
expect(call[0]).toBe("POST");
8688
expect(call[1]).toBe("/_matrix/client/v3/login");
8789
expect(call[2]).toBeUndefined();
@@ -575,7 +577,7 @@ describe("resolveMatrixAuth", () => {
575577
"default",
576578
);
577579
const repairMeta = requireRecord(
578-
repairCurrentTokenStorageMetaDeviceIdMock.mock.calls[0]?.[0],
580+
mockCall(repairCurrentTokenStorageMetaDeviceIdMock).at(0),
579581
"repair metadata",
580582
);
581583
expectRecordFields(repairMeta, {
@@ -725,7 +727,7 @@ describe("resolveMatrixAuth", () => {
725727
});
726728

727729
expectRecordFields(
728-
requireRecord(resolveConfiguredSecretInputStringMock.mock.calls[0]?.[0], "secret request"),
730+
requireRecord(mockCall(resolveConfiguredSecretInputStringMock).at(0), "secret request"),
729731
{
730732
config: cfg,
731733
value: { source: "file", provider: "matrix-file", id: "value" },

0 commit comments

Comments
 (0)