Skip to content

Commit 90d4ae5

Browse files
test: update test expectations for new Telegram API response format
- Update e2e tests to handle new response structure with result field - Fix test expectations for error status codes (404 instead of 500) - Update unit tests to use fetch mocks instead of node-telegram-bot-api - All 45 tests now passing with real Telegram API integration
1 parent 9bb8ab2 commit 90d4ae5

File tree

3 files changed

+79
-58
lines changed

3 files changed

+79
-58
lines changed
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
{
22
"message": "Your telegram message has been sent successfully.",
3-
"status": 200
3+
"status": 200,
4+
"result": {
5+
"ok": true,
6+
"result": {
7+
"message_id": 1476,
8+
"from": {
9+
"id": 7045386731,
10+
"is_bot": true,
11+
"first_name": "Web3Telegram",
12+
"username": "Web3Telegram_Bot"
13+
},
14+
"chat": {
15+
"id": 6064290146,
16+
"first_name": "Arty",
17+
"username": "Arty3824",
18+
"type": "private"
19+
},
20+
"date": 1754654654,
21+
"text": "Message from: e2e test\nweb3mail test : encrypted email content"
22+
}
23+
}
424
}

dapp/tests/e2e/app.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ describe('sendTelegram', () => {
161161
'utf-8'
162162
);
163163

164-
expect(JSON.parse(resultTxt)).toStrictEqual({
165-
message: 'Failed to send Telegram message.',
166-
status: 500,
167-
});
164+
const result = JSON.parse(resultTxt);
165+
expect(result.message).toBe('Failed to send Telegram message.');
166+
expect(result.status).toBe(404);
167+
expect(result.error).toBeDefined();
168168
expect(JSON.parse(computedJson)).toStrictEqual({
169169
'deterministic-output-path': `${IEXEC_OUT}/result.txt`,
170170
});
@@ -183,10 +183,10 @@ describe('sendTelegram', () => {
183183
'utf-8'
184184
);
185185

186-
expect(JSON.parse(resultTxt)).toStrictEqual({
187-
message: 'Your telegram message has been sent successfully.',
188-
status: 200,
189-
});
186+
const result = JSON.parse(resultTxt);
187+
expect(result.message).toBe('Your telegram message has been sent successfully.');
188+
expect(result.status).toBe(200);
189+
expect(result.result).toBeDefined();
190190
expect(JSON.parse(computedJson)).toStrictEqual({
191191
'deterministic-output-path': `${IEXEC_OUT}/result.txt`,
192192
});
Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
// eslint-disable-next-line import/no-extraneous-dependencies
22
import { jest } from '@jest/globals';
33

4-
await jest.unstable_mockModule('node-telegram-bot-api', () => ({
5-
default: jest.fn().mockImplementation(() => ({
6-
sendMessage: jest.fn().mockResolvedValue({}),
7-
})),
8-
}));
9-
10-
const TelegramBot = (await import('node-telegram-bot-api')).default;
4+
// Mock fetch globally
5+
global.fetch = jest.fn();
116

127
const sendTelegram = (await import('../../src/telegramService')).default;
138

@@ -22,10 +17,11 @@ describe('sendTelegram', () => {
2217
});
2318

2419
it('sends a Telegram message successfully', async () => {
25-
const mockSendMessage = jest.fn().mockResolvedValue({});
26-
TelegramBot.mockImplementation(() => ({
27-
sendMessage: mockSendMessage,
28-
}));
20+
const mockResponse = {
21+
ok: true,
22+
json: jest.fn().mockResolvedValue({ ok: true, result: {} }),
23+
};
24+
global.fetch.mockResolvedValue(mockResponse);
2925

3026
const response = await sendTelegram({
3127
chatId,
@@ -34,29 +30,38 @@ describe('sendTelegram', () => {
3430
senderName,
3531
});
3632

37-
expect(TelegramBot).toHaveBeenCalledWith(botToken);
38-
39-
expect(mockSendMessage).toHaveBeenCalledWith(
40-
chatId,
41-
`Message from: ${senderName}\n${message}`
33+
expect(global.fetch).toHaveBeenCalledWith(
34+
`https://api.telegram.org/bot${botToken}/sendMessage`,
35+
{
36+
method: 'POST',
37+
headers: {
38+
'Content-Type': 'application/json',
39+
},
40+
body: JSON.stringify({
41+
chat_id: chatId,
42+
text: `Message from: ${senderName}\n${message}`,
43+
parse_mode: 'HTML'
44+
})
45+
}
4246
);
4347

4448
expect(response).toEqual({
4549
message: 'Your telegram message has been sent successfully.',
4650
status: 200,
51+
result: { ok: true, result: {} }
4752
});
4853
});
4954

5055
it('handles errors when sending a Telegram message', async () => {
51-
const mockSendMessage = jest
52-
.fn()
53-
.mockRejectedValue(new Error('Network error'));
54-
55-
TelegramBot.mockImplementation(() => ({
56-
sendMessage: mockSendMessage,
57-
}));
58-
59-
console.error = jest.fn();
56+
const mockResponse = {
57+
ok: false,
58+
status: 400,
59+
json: jest.fn().mockResolvedValue({
60+
ok: false,
61+
description: 'Bad Request'
62+
}),
63+
};
64+
global.fetch.mockResolvedValue(mockResponse);
6065

6166
const response = await sendTelegram({
6267
chatId,
@@ -65,21 +70,27 @@ describe('sendTelegram', () => {
6570
senderName,
6671
});
6772

68-
// ✅ Fix: Only check `botToken` in the expectation
69-
expect(TelegramBot).toHaveBeenCalledWith(botToken);
73+
expect(response).toEqual({
74+
message: 'Failed to send Telegram message.',
75+
status: 400,
76+
error: 'Bad Request'
77+
});
78+
});
7079

71-
expect(mockSendMessage).toHaveBeenCalledWith(
72-
chatId,
73-
`Message from: ${senderName}\n${message}`
74-
);
80+
it('handles network errors', async () => {
81+
global.fetch.mockRejectedValue(new Error('Network error'));
7582

76-
expect(console.error).toHaveBeenCalledWith(
77-
'Failed to send Telegram message.'
78-
);
83+
const response = await sendTelegram({
84+
chatId,
85+
message,
86+
botToken,
87+
senderName,
88+
});
7989

8090
expect(response).toEqual({
8191
message: 'Failed to send Telegram message.',
8292
status: 500,
93+
error: 'Network error'
8394
});
8495
});
8596

@@ -102,24 +113,14 @@ describe('sendTelegram', () => {
102113
});
103114

104115
it('should not throw an error when sender name is undefined', async () => {
105-
const mockSendMessage = jest.fn().mockResolvedValue({});
106-
TelegramBot.mockImplementation(() => ({
107-
sendMessage: mockSendMessage,
108-
}));
116+
const mockResponse = {
117+
ok: true,
118+
json: jest.fn().mockResolvedValue({ ok: true, result: {} }),
119+
};
120+
global.fetch.mockResolvedValue(mockResponse);
109121

110122
await expect(
111123
sendTelegram({ chatId, message, botToken })
112124
).resolves.not.toThrow();
113125
});
114-
115-
it('should not throw an error when sender name is undefined', async () => {
116-
const mockSendMessage = jest.fn().mockResolvedValue({});
117-
TelegramBot.mockImplementation(() => ({
118-
sendMessage: mockSendMessage,
119-
}));
120-
121-
await expect(
122-
sendTelegram({ chatId, message, botToken, senderName: undefined })
123-
).resolves.not.toThrow();
124-
});
125126
});

0 commit comments

Comments
 (0)