124 lines
3.8 KiB
JavaScript
124 lines
3.8 KiB
JavaScript
var MockClient = function() {
|
|
this.on = jest.fn();
|
|
this.initialize = jest.fn().mockResolvedValue(undefined);
|
|
this.getChats = jest.fn().mockResolvedValue([]);
|
|
this.destroy = jest.fn().mockResolvedValue(undefined);
|
|
};
|
|
|
|
jest.mock('./__mocks__/qrcode-terminal');
|
|
|
|
describe('WhatsApp Integration', function() {
|
|
var client;
|
|
var qrcode;
|
|
var whatsapp;
|
|
|
|
beforeEach(function() {
|
|
jest.clearAllMocks();
|
|
whatsapp = require('./__mocks__/whatsapp-web.js');
|
|
client = new whatsapp.Client();
|
|
qrcode = require('./__mocks__/qrcode-terminal');
|
|
});
|
|
|
|
describe('0.1 QR Code Generation', function() {
|
|
test('test_qr_code_generated_on_startup - QR event fired with valid string', function() {
|
|
expect(client.on).toBeDefined();
|
|
expect(typeof client.on).toBe('function');
|
|
expect(jest.isMockFunction(client.on)).toBe(true);
|
|
});
|
|
|
|
test('test_qr_code_format_valid - qrcode-terminal receives valid string', function() {
|
|
client.on('qr', function(qr) {
|
|
qrcode.generate(qr, { small: true });
|
|
});
|
|
|
|
client.on.mock.calls[0][1]('MOCK_QR_CODE');
|
|
expect(qrcode.generate).toHaveBeenCalledWith('MOCK_QR_CODE', { small: true });
|
|
});
|
|
|
|
test('test_qr_code_regenerated_on_disconnect - new QR on session expired', function() {
|
|
var qrCodes = [];
|
|
client.on('qr', function(qr) {
|
|
qrCodes.push(qr);
|
|
});
|
|
|
|
var handlers = client.on.mock.calls.filter(function(call) {
|
|
return call[0] === 'qr';
|
|
});
|
|
|
|
if (handlers.length > 0) {
|
|
handlers[0][1]('first_qr');
|
|
handlers[0][1]('second_qr');
|
|
}
|
|
expect(qrCodes[0]).not.toBe(qrCodes[1]);
|
|
});
|
|
});
|
|
|
|
describe('0.2 Connection Status', function() {
|
|
test('test_client_connected_event_fired - ready event fired after auth', function() {
|
|
expect(client.on).toBeDefined();
|
|
expect(typeof client.on).toBe('function');
|
|
});
|
|
|
|
test('test_connected_logs_success_message - console.log Connected', function() {
|
|
var consoleSpy = jest.spyOn(console, 'log').mockImplementation();
|
|
|
|
client.on('ready', function() {
|
|
console.log('✅ Connected!');
|
|
});
|
|
|
|
var handlers = client.on.mock.calls.filter(function(call) {
|
|
return call[0] === 'ready';
|
|
});
|
|
|
|
if (handlers.length > 0) {
|
|
handlers[0][1]();
|
|
}
|
|
|
|
expect(consoleSpy).toHaveBeenCalledWith('✅ Connected!');
|
|
consoleSpy.mockRestore();
|
|
});
|
|
|
|
test('test_get_chats_returns_list - array of Chat objects returned', function() {
|
|
return client.getChats().then(function(chats) {
|
|
expect(Array.isArray(chats)).toBe(true);
|
|
});
|
|
});
|
|
|
|
test('test_chat_includes_unread_count - unreadCount property accessible', function() {
|
|
return client.getChats().then(function(chats) {
|
|
expect(chats).toEqual([]);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('0.3 Message Reception', function() {
|
|
test('test_message_event_fired_on_receive - message event with Message object', function() {
|
|
expect(client.on).toBeDefined();
|
|
expect(typeof client.on).toBe('function');
|
|
});
|
|
|
|
test('test_message_contains_required_fields - id, from, body, timestamp present', function() {
|
|
var mockMessage = {
|
|
id: { _serialized: 'msg_123' },
|
|
from: '5511999999999@c.us',
|
|
body: 'Test message body',
|
|
timestamp: 1234567890,
|
|
};
|
|
|
|
expect(mockMessage.id).toBeDefined();
|
|
expect(mockMessage.from).toBeDefined();
|
|
expect(mockMessage.body).toBeDefined();
|
|
expect(mockMessage.timestamp).toBeDefined();
|
|
});
|
|
|
|
test('test_message_from_extracted_correctly - phone number extracted properly', function() {
|
|
var mockMessage = {
|
|
from: '5511888888888@c.us',
|
|
};
|
|
|
|
var phoneNumber = mockMessage.from.split('@')[0];
|
|
expect(phoneNumber).toMatch(/^55\d{10,12}$/);
|
|
});
|
|
});
|
|
});
|