Skip to content

Commit 9f37680

Browse files
SeddikBellamineabbesBenayache
authored andcommitted
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 9f37680

File tree

3 files changed

+71
-76
lines changed

3 files changed

+71
-76
lines changed

dapp/src/telegramService.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
async function sendTelegram({
32
chatId,
43
message,
@@ -14,41 +13,39 @@ async function sendTelegram({
1413
const messageToSend = `Message from: ${senderName}\n${message}`;
1514

1615
try {
17-
const response = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
18-
method: 'POST',
19-
headers: {
20-
'Content-Type': 'application/json',
21-
},
22-
body: JSON.stringify({
23-
chat_id: chatId,
24-
text: messageToSend,
25-
parse_mode: 'HTML'
26-
})
27-
});
28-
29-
const result = await response.json();
16+
const response = await fetch(
17+
`https://api.telegram.org/bot${botToken}/sendMessage`,
18+
{
19+
method: 'POST',
20+
headers: {
21+
'Content-Type': 'application/json',
22+
},
23+
body: JSON.stringify({
24+
chat_id: chatId,
25+
text: messageToSend,
26+
parse_mode: 'HTML',
27+
}),
28+
}
29+
);
3030

3131
if (!response.ok) {
32-
console.error('Telegram API Error:', result);
32+
console.error('Telegram API Error');
3333
return {
3434
message: 'Failed to send Telegram message.',
3535
status: response.status,
36-
error: result.description
3736
};
3837
}
3938

4039
console.log('Message successfully sent by Telegram bot.');
4140
return {
4241
message: 'Your telegram message has been sent successfully.',
4342
status: 200,
44-
result
4543
};
4644
} catch (error) {
47-
console.error('Failed to send Telegram message:', error);
45+
console.error('Failed to send Telegram message');
4846
return {
4947
message: 'Failed to send Telegram message.',
5048
status: 500,
51-
error: error.message
5249
};
5350
}
5451
}

dapp/tests/e2e/app.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,9 @@ 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);
168167
expect(JSON.parse(computedJson)).toStrictEqual({
169168
'deterministic-output-path': `${IEXEC_OUT}/result.txt`,
170169
});
@@ -183,10 +182,11 @@ describe('sendTelegram', () => {
183182
'utf-8'
184183
);
185184

186-
expect(JSON.parse(resultTxt)).toStrictEqual({
187-
message: 'Your telegram message has been sent successfully.',
188-
status: 200,
189-
});
185+
const result = JSON.parse(resultTxt);
186+
expect(result.message).toBe(
187+
'Your telegram message has been sent successfully.'
188+
);
189+
expect(result.status).toBe(200);
190190
expect(JSON.parse(computedJson)).toStrictEqual({
191191
'deterministic-output-path': `${IEXEC_OUT}/result.txt`,
192192
});
Lines changed: 47 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,11 +30,19 @@ 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({
@@ -48,15 +52,15 @@ describe('sendTelegram', () => {
4852
});
4953

5054
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();
55+
const mockResponse = {
56+
ok: false,
57+
status: 400,
58+
json: jest.fn().mockResolvedValue({
59+
ok: false,
60+
description: 'Bad Request',
61+
}),
62+
};
63+
global.fetch.mockResolvedValue(mockResponse);
6064

6165
const response = await sendTelegram({
6266
chatId,
@@ -65,17 +69,21 @@ describe('sendTelegram', () => {
6569
senderName,
6670
});
6771

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

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

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

8088
expect(response).toEqual({
8189
message: 'Failed to send Telegram message.',
@@ -102,24 +110,14 @@ describe('sendTelegram', () => {
102110
});
103111

104112
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-
}));
113+
const mockResponse = {
114+
ok: true,
115+
json: jest.fn().mockResolvedValue({ ok: true, result: {} }),
116+
};
117+
global.fetch.mockResolvedValue(mockResponse);
109118

110119
await expect(
111120
sendTelegram({ chatId, message, botToken })
112121
).resolves.not.toThrow();
113122
});
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-
});
125123
});

0 commit comments

Comments
 (0)