import { webMethods } from '../src/methods/web';
import { AuthService } from '../src/utils/auth';
import { ValidationError, RpcBaseError } from '../src/utils/errors';
// Mock the clients
jest.mock('../src/clients/veeqoClient');
jest.mock('../src/clients/easypostClient');
// Mock the auth service
jest.mock('../src/utils/auth');
describe('Web Methods', () => {
describe('login', () => {
it('should authenticate user successfully', async () => {
const mockUser = {
id: '1',
username: 'admin',
role: 'admin',
createdAt: '2023-01-01T00:00:00Z'
};
const mockToken = 'mock-jwt-token';
(AuthService.authenticate as jest.Mock).mockResolvedValue(mockUser);
(AuthService.generateToken as jest.Mock).mockReturnValue(mockToken);
const params = { username: 'admin', password: 'password123' };
const result = await webMethods.login(params);
expect(result).toEqual({
token: mockToken,
user: {
id: '1',
username: 'admin',
role: 'admin'
}
});
});
it('should throw validation error for missing credentials', async () => {
await expect(webMethods.login({} as any))
.rejects
.toThrow(ValidationError);
});
it('should throw authentication error for invalid credentials', async () => {
(AuthService.authenticate as jest.Mock).mockResolvedValue(null);
const params = { username: 'invalid', password: 'wrongpassword' };
await expect(webMethods.login(params))
.rejects
.toThrow(RpcBaseError);
});
});
describe('changePassword', () => {
it('should change password successfully', async () => {
(AuthService.changePassword as jest.Mock).mockResolvedValue(true);
const params = {
oldPassword: 'oldpassword',
newPassword: 'newpassword',
userId: '1'
};
const result = await webMethods.changePassword(params, '1');
expect(result).toEqual({ success: true });
});
it('should throw error for failed password change', async () => {
(AuthService.changePassword as jest.Mock).mockResolvedValue(false);
const params = {
oldPassword: 'oldpassword',
newPassword: 'newpassword',
userId: '1'
};
await expect(webMethods.changePassword(params, '1'))
.rejects
.toThrow(RpcBaseError);
});
});
describe('getDashboardStats', () => {
it('should return dashboard statistics', async () => {
const mockOrdersResponse = {
orders: [
{ id: 'ord_1', status: 'pending', number: 'V123', created_at: '2023-01-01' },
{ id: 'ord_2', status: 'shipped', number: 'V124', created_at: '2023-01-02' }
],
total_count: 2,
page: 1,
per_page: 10,
total_pages: 1
};
const mockInventoryItems = [
{ id: 'item_1', product_id: 'prod_1', available: 10, allocated: 2, quantity: 12 },
{ id: 'item_2', product_id: 'prod_2', available: 3, allocated: 1, quantity: 4 }
];
// Mock the VeeqoClient methods
const mockVeeqoClient: any = {
listOrders: jest.fn().mockResolvedValue(mockOrdersResponse),
syncInventory: jest.fn().mockResolvedValue(mockInventoryItems)
};
// We need to mock the actual client instantiation
jest.mock('../src/clients/veeqoClient', () => {
return {
VeeqoClient: jest.fn().mockImplementation(() => mockVeeqoClient)
};
});
// Re-import to get the mocked version
const { webMethods: webMethodsReloaded } = require('../src/methods/web');
const result = await webMethodsReloaded.getDashboardStats();
expect(result).toHaveProperty('totalOrders');
expect(result).toHaveProperty('pendingShipments');
expect(result).toHaveProperty('lowStockItems');
expect(result).toHaveProperty('revenue');
expect(result).toHaveProperty('recentOrders');
});
});
describe('searchOrders', () => {
it('should search orders with filters', async () => {
const mockOrdersResponse = {
orders: [
{ id: 'ord_1', status: 'pending', number: 'V123', created_at: '2023-01-01' }
],
total_count: 1,
page: 1,
per_page: 10,
total_pages: 1
};
// Mock the VeeqoClient methods
const mockVeeqoClient: any = {
listOrders: jest.fn().mockResolvedValue(mockOrdersResponse)
};
// We need to mock the actual client instantiation
jest.mock('../src/clients/veeqoClient', () => {
return {
VeeqoClient: jest.fn().mockImplementation(() => mockVeeqoClient)
};
});
// Re-import to get the mocked version
const { webMethods: webMethodsReloaded } = require('../src/methods/web');
const params = { query: 'V123', status: 'pending', page: 1, limit: 10 };
const result = await webMethodsReloaded.searchOrders(params);
expect(result).toHaveProperty('orders');
expect(result).toHaveProperty('pagination');
});
});
describe('getLowStockItems', () => {
it('should return low stock items', async () => {
const mockInventoryItems = [
{ id: 'item_1', product_id: 'prod_1', available: 3, allocated: 2, quantity: 5, sellable_id: 'sell_1' },
{ id: 'item_2', product_id: 'prod_2', available: 10, allocated: 0, quantity: 10, sellable_id: 'sell_2' }
];
// Mock the VeeqoClient methods
const mockVeeqoClient: any = {
syncInventory: jest.fn().mockResolvedValue(mockInventoryItems)
};
// We need to mock the actual client instantiation
jest.mock('../src/clients/veeqoClient', () => {
return {
VeeqoClient: jest.fn().mockImplementation(() => mockVeeqoClient)
};
});
// Re-import to get the mocked version
const { webMethods: webMethodsReloaded } = require('../src/methods/web');
const result = await webMethodsReloaded.getLowStockItems();
expect(result).toHaveProperty('items');
expect(result.items).toHaveLength(1);
expect(result.items[0].available).toBeLessThan(5);
});
});
});