view data fixes
This commit is contained in:
145
apps/website/lib/page-queries/AdminDashboardPageQuery.test.ts
Normal file
145
apps/website/lib/page-queries/AdminDashboardPageQuery.test.ts
Normal file
@@ -0,0 +1,145 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { AdminDashboardPageQuery } from './AdminDashboardPageQuery';
|
||||
import { AdminService } from '@/lib/services/admin/AdminService';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { AdminDashboardViewDataBuilder } from '@/lib/builders/view-data/AdminDashboardViewDataBuilder';
|
||||
import type { DashboardStats } from '@/lib/types/admin';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('@/lib/services/admin/AdminService', () => ({
|
||||
AdminService: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/builders/view-data/AdminDashboardViewDataBuilder', () => ({
|
||||
AdminDashboardViewDataBuilder: {
|
||||
build: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('AdminDashboardPageQuery', () => {
|
||||
let query: AdminDashboardPageQuery;
|
||||
let mockServiceInstance: any;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
query = new AdminDashboardPageQuery();
|
||||
mockServiceInstance = {
|
||||
getDashboardStats: vi.fn(),
|
||||
};
|
||||
vi.mocked(AdminService).mockImplementation(function() {
|
||||
return mockServiceInstance;
|
||||
});
|
||||
});
|
||||
|
||||
it('should return view data when service succeeds', async () => {
|
||||
const mockStats: DashboardStats = {
|
||||
totalUsers: 1250,
|
||||
activeUsers: 1100,
|
||||
suspendedUsers: 50,
|
||||
deletedUsers: 100,
|
||||
systemAdmins: 5,
|
||||
recentLogins: 450,
|
||||
newUsersToday: 12,
|
||||
userGrowth: [
|
||||
{ label: 'This week', value: 45, color: '#10b981' },
|
||||
{ label: 'Last week', value: 38, color: '#3b82f6' },
|
||||
],
|
||||
roleDistribution: [
|
||||
{ label: 'Users', value: 1200, color: '#6b7280' },
|
||||
{ label: 'Admins', value: 50, color: '#8b5cf6' },
|
||||
],
|
||||
statusDistribution: {
|
||||
active: 1100,
|
||||
suspended: 50,
|
||||
deleted: 100,
|
||||
},
|
||||
activityTimeline: [
|
||||
{ date: '2024-01-01', newUsers: 10, logins: 200 },
|
||||
{ date: '2024-01-02', newUsers: 15, logins: 220 },
|
||||
],
|
||||
};
|
||||
|
||||
const mockViewData = {
|
||||
stats: {
|
||||
totalUsers: 1250,
|
||||
activeUsers: 1100,
|
||||
suspendedUsers: 50,
|
||||
deletedUsers: 100,
|
||||
systemAdmins: 5,
|
||||
recentLogins: 450,
|
||||
newUsersToday: 12,
|
||||
},
|
||||
};
|
||||
|
||||
mockServiceInstance.getDashboardStats.mockResolvedValue(Result.ok(mockStats));
|
||||
(AdminDashboardViewDataBuilder.build as any).mockReturnValue(mockViewData);
|
||||
|
||||
const result = await query.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual(mockViewData);
|
||||
expect(AdminService).toHaveBeenCalled();
|
||||
expect(mockServiceInstance.getDashboardStats).toHaveBeenCalled();
|
||||
expect(AdminDashboardViewDataBuilder.build).toHaveBeenCalledWith(mockStats);
|
||||
});
|
||||
|
||||
it('should return error when service fails', async () => {
|
||||
const serviceError = { type: 'serverError', message: 'Service error' };
|
||||
|
||||
mockServiceInstance.getDashboardStats.mockResolvedValue(Result.err(serviceError));
|
||||
|
||||
const result = await query.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('serverError');
|
||||
});
|
||||
|
||||
it('should provide a static execute method', async () => {
|
||||
const mockStats: DashboardStats = {
|
||||
totalUsers: 1250,
|
||||
activeUsers: 1100,
|
||||
suspendedUsers: 50,
|
||||
deletedUsers: 100,
|
||||
systemAdmins: 5,
|
||||
recentLogins: 450,
|
||||
newUsersToday: 12,
|
||||
userGrowth: [
|
||||
{ label: 'This week', value: 45, color: '#10b981' },
|
||||
{ label: 'Last week', value: 38, color: '#3b82f6' },
|
||||
],
|
||||
roleDistribution: [
|
||||
{ label: 'Users', value: 1200, color: '#6b7280' },
|
||||
{ label: 'Admins', value: 50, color: '#8b5cf6' },
|
||||
],
|
||||
statusDistribution: {
|
||||
active: 1100,
|
||||
suspended: 50,
|
||||
deleted: 100,
|
||||
},
|
||||
activityTimeline: [
|
||||
{ date: '2024-01-01', newUsers: 10, logins: 200 },
|
||||
{ date: '2024-01-02', newUsers: 15, logins: 220 },
|
||||
],
|
||||
};
|
||||
|
||||
const mockViewData = {
|
||||
stats: {
|
||||
totalUsers: 1250,
|
||||
activeUsers: 1100,
|
||||
suspendedUsers: 50,
|
||||
deletedUsers: 100,
|
||||
systemAdmins: 5,
|
||||
recentLogins: 450,
|
||||
newUsersToday: 12,
|
||||
},
|
||||
};
|
||||
|
||||
mockServiceInstance.getDashboardStats.mockResolvedValue(Result.ok(mockStats));
|
||||
(AdminDashboardViewDataBuilder.build as any).mockReturnValue(mockViewData);
|
||||
|
||||
const result = await AdminDashboardPageQuery.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual(mockViewData);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user