Skip to content

Commit fe1f30b

Browse files
committed
test: guard zalouser payload mock calls
1 parent 7d7751e commit fe1f30b

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

extensions/zalouser/src/channel.sendpayload.test.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,17 @@ function requireRecord(value: unknown, label: string): Record<string, unknown> {
8080
function requireSendOptions(
8181
mockedSend: ReturnType<typeof vi.mocked<(typeof import("./send.js"))["sendMessageZalouser"]>>,
8282
): Record<string, unknown> {
83-
return requireRecord(mockedSend.mock.calls[0]?.[2], "Zalouser send options");
83+
return requireRecord(requireSendCall(mockedSend)[2], "Zalouser send options");
84+
}
85+
86+
function requireSendCall(
87+
mockedSend: ReturnType<typeof vi.mocked<(typeof import("./send.js"))["sendMessageZalouser"]>>,
88+
): unknown[] {
89+
const [call] = mockedSend.mock.calls as unknown[][];
90+
if (!call) {
91+
throw new Error("expected Zalouser send call");
92+
}
93+
return call;
8494
}
8595

8696
describe("zalouserPlugin outbound sendPayload", () => {
@@ -109,8 +119,9 @@ describe("zalouserPlugin outbound sendPayload", () => {
109119
});
110120

111121
expect(mockedSend).toHaveBeenCalledOnce();
112-
expect(mockedSend.mock.calls[0]?.[0]).toBe("1471383327500481391");
113-
expect(mockedSend.mock.calls[0]?.[1]).toBe("hello group");
122+
const sendCall = requireSendCall(mockedSend);
123+
expect(sendCall[0]).toBe("1471383327500481391");
124+
expect(sendCall[1]).toBe("hello group");
114125
const options = requireSendOptions(mockedSend);
115126
expect(options.isGroup).toBe(true);
116127
expect(options.textMode).toBe("markdown");
@@ -128,8 +139,9 @@ describe("zalouserPlugin outbound sendPayload", () => {
128139
});
129140

130141
expect(mockedSend).toHaveBeenCalledOnce();
131-
expect(mockedSend.mock.calls[0]?.[0]).toBe("987654321");
132-
expect(mockedSend.mock.calls[0]?.[1]).toBe("hello");
142+
const sendCall = requireSendCall(mockedSend);
143+
expect(sendCall[0]).toBe("987654321");
144+
expect(sendCall[1]).toBe("hello");
133145
const options = requireSendOptions(mockedSend);
134146
expect(options.isGroup).toBe(false);
135147
expect(options.textMode).toBe("markdown");
@@ -147,8 +159,9 @@ describe("zalouserPlugin outbound sendPayload", () => {
147159
});
148160

149161
expect(mockedSend).toHaveBeenCalledOnce();
150-
expect(mockedSend.mock.calls[0]?.[0]).toBe("g-1471383327500481391");
151-
expect(mockedSend.mock.calls[0]?.[1]).toBe("hello native group");
162+
const sendCall = requireSendCall(mockedSend);
163+
expect(sendCall[0]).toBe("g-1471383327500481391");
164+
expect(sendCall[1]).toBe("hello native group");
152165
const options = requireSendOptions(mockedSend);
153166
expect(options.isGroup).toBe(true);
154167
expect(options.textMode).toBe("markdown");
@@ -167,8 +180,9 @@ describe("zalouserPlugin outbound sendPayload", () => {
167180
});
168181

169182
expect(mockedSend).toHaveBeenCalledTimes(1);
170-
expect(mockedSend.mock.calls[0]?.[0]).toBe("987654321");
171-
expect(mockedSend.mock.calls[0]?.[1]).toBe(text);
183+
const sendCall = requireSendCall(mockedSend);
184+
expect(sendCall[0]).toBe("987654321");
185+
expect(sendCall[1]).toBe(text);
172186
const options = requireSendOptions(mockedSend);
173187
expect(options.isGroup).toBe(false);
174188
expect(options.textMode).toBe("markdown");

0 commit comments

Comments
 (0)