Repository navigation
Expand file tree
/
Copy pathbeepmate-javascript-example.test.js
More file actions
68 lines (53 loc) · 2.32 KB
/
Copy pathbeepmate-javascript-example.test.js
File metadata and controls
68 lines (53 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { jest } from '@jest/globals';
import { BeepMateClient } from './beepmate-javascript-example.js';
// Mock the global fetch API
global.fetch = jest.fn();
describe('BeepMateClient', () => {
beforeEach(() => {
fetch.mockClear();
});
describe('Initialization', () => {
it('should throw an error if initialized without valid configuration', () => {
expect(() => new BeepMateClient({ apiKey: '', targetId: '123' })).toThrow('valid API key is required');
expect(() => new BeepMateClient({ apiKey: 'key', targetId: ' ' })).toThrow('valid Target ID');
});
});
describe('sendMessage()', () => {
let client;
beforeEach(() => {
client = new BeepMateClient({ apiKey: 'test_key', targetId: '123456789' });
});
it('should throw an error if the message is empty', async () => {
await expect(client.sendMessage('')).rejects.toThrow('Message content cannot be empty');
await expect(client.sendMessage(' ')).rejects.toThrow('Message content cannot be empty');
});
it('should successfully send a message and encode the URL properly', async () => {
fetch.mockResolvedValueOnce({
ok: true,
text: async () => 'Success',
});
const result = await client.sendMessage('Hello World!');
expect(result).toBe(true);
expect(fetch).toHaveBeenCalledTimes(1);
// Verify URL formatting and parameter encoding
const calledUrl = fetch.mock.calls[0][0];
const parsedUrl = new URL(calledUrl);
expect(parsedUrl.origin + parsedUrl.pathname).toBe('https://beepmate.io/send');
expect(parsedUrl.searchParams.get('key')).toBe('test_key');
expect(parsedUrl.searchParams.get('id')).toBe('123456789');
expect(parsedUrl.searchParams.get('msg')).toBe('Hello World!');
});
it('should handle API HTTP errors gracefully', async () => {
fetch.mockResolvedValueOnce({
ok: false,
status: 401,
text: async () => 'Unauthorized',
});
await expect(client.sendMessage('Test')).rejects.toThrow('API Error 401: Unauthorized');
});
it('should handle network failures', async () => {
fetch.mockRejectedValueOnce(new Error('Network disconnected'));
await expect(client.sendMessage('Test')).rejects.toThrow('Failed to send message: Network disconnected');
});
});
});