mirror of
https://github.com/BrenBroZAYT/uptime-kuma-discord-bot.git
synced 2026-06-18 00:30:03 +00:00
feat: add Jest testing framework and implement unit tests for configuration and service logic
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { UptimeKumaService } from '../src/services/uptime-kuma.service';
|
||||
|
||||
// Mock socket.io-client
|
||||
jest.mock('socket.io-client', () => ({
|
||||
io: jest.fn().mockImplementation(() => ({
|
||||
connected: false,
|
||||
connect: jest.fn(),
|
||||
disconnect: jest.fn(),
|
||||
on: jest.fn(),
|
||||
emit: jest.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
// Mock other dependencies
|
||||
jest.mock('../src/config/config');
|
||||
jest.mock('../src/utils/logger');
|
||||
|
||||
describe('UptimeKumaService', () => {
|
||||
let uptimeKumaService: UptimeKumaService;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
uptimeKumaService = new UptimeKumaService();
|
||||
});
|
||||
|
||||
test('should create service instance', () => {
|
||||
expect(uptimeKumaService).toBeDefined();
|
||||
});
|
||||
|
||||
test('should return connection status', () => {
|
||||
// Initially should be disconnected
|
||||
expect(uptimeKumaService.isConnected()).toBe(false);
|
||||
});
|
||||
|
||||
test('should handle connection state changes', () => {
|
||||
// Mock socket connection
|
||||
const mockSocket = {
|
||||
connected: true,
|
||||
connect: jest.fn(),
|
||||
disconnect: jest.fn(),
|
||||
on: jest.fn(),
|
||||
emit: jest.fn(),
|
||||
};
|
||||
|
||||
// Simulate connection by mocking the socket property and authentication
|
||||
(uptimeKumaService as any).socket = mockSocket;
|
||||
(uptimeKumaService as any).isAuthenticated = true;
|
||||
|
||||
expect(uptimeKumaService.isConnected()).toBe(true);
|
||||
|
||||
// Simulate disconnection
|
||||
mockSocket.connected = false;
|
||||
expect(uptimeKumaService.isConnected()).toBe(false);
|
||||
});
|
||||
|
||||
test('should handle force reconnect', async () => {
|
||||
// Mock the connect method to avoid socket.once issues
|
||||
const mockConnect = jest.fn().mockResolvedValue(undefined);
|
||||
(uptimeKumaService as any).connect = mockConnect;
|
||||
|
||||
// Should not throw error even when not connected
|
||||
await expect(uptimeKumaService.forceReconnect()).resolves.not.toThrow();
|
||||
expect(mockConnect).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('should handle disconnect', () => {
|
||||
// Should not throw error
|
||||
expect(() => uptimeKumaService.disconnect()).not.toThrow();
|
||||
});
|
||||
|
||||
test('should get monitor stats', () => {
|
||||
const stats = uptimeKumaService.getMonitorStats();
|
||||
expect(stats).toBeDefined();
|
||||
expect(Array.isArray(stats)).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user