view data fixes
This commit is contained in:
229
apps/website/lib/page-queries/AdminUsersPageQuery.test.ts
Normal file
229
apps/website/lib/page-queries/AdminUsersPageQuery.test.ts
Normal file
@@ -0,0 +1,229 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { AdminUsersPageQuery } from './AdminUsersPageQuery';
|
||||
import { AdminService } from '@/lib/services/admin/AdminService';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { AdminUsersViewDataBuilder } from '@/lib/builders/view-data/AdminUsersViewDataBuilder';
|
||||
import type { UserListResponse } from '@/lib/types/admin';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('@/lib/services/admin/AdminService', () => ({
|
||||
AdminService: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/builders/view-data/AdminUsersViewDataBuilder', () => ({
|
||||
AdminUsersViewDataBuilder: {
|
||||
build: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('AdminUsersPageQuery', () => {
|
||||
let query: AdminUsersPageQuery;
|
||||
let mockServiceInstance: any;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
query = new AdminUsersPageQuery();
|
||||
mockServiceInstance = {
|
||||
listUsers: vi.fn(),
|
||||
};
|
||||
vi.mocked(AdminService).mockImplementation(function() {
|
||||
return mockServiceInstance;
|
||||
});
|
||||
});
|
||||
|
||||
it('should return view data when service succeeds', async () => {
|
||||
const mockUsers: UserListResponse = {
|
||||
users: [
|
||||
{
|
||||
id: '1',
|
||||
email: 'admin@example.com',
|
||||
displayName: 'Admin User',
|
||||
roles: ['owner', 'admin'],
|
||||
status: 'active',
|
||||
isSystemAdmin: true,
|
||||
createdAt: '2024-01-01T00:00:00.000Z',
|
||||
updatedAt: '2024-01-01T00:00:00.000Z',
|
||||
lastLoginAt: '2024-01-15T10:00:00.000Z',
|
||||
primaryDriverId: 'driver-1',
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
email: 'user@example.com',
|
||||
displayName: 'Regular User',
|
||||
roles: ['user'],
|
||||
status: 'active',
|
||||
isSystemAdmin: false,
|
||||
createdAt: '2024-01-02T00:00:00.000Z',
|
||||
updatedAt: '2024-01-02T00:00:00.000Z',
|
||||
lastLoginAt: '2024-01-14T15:00:00.000Z',
|
||||
},
|
||||
],
|
||||
total: 2,
|
||||
page: 1,
|
||||
limit: 50,
|
||||
totalPages: 1,
|
||||
};
|
||||
|
||||
const mockViewData = {
|
||||
users: [
|
||||
{
|
||||
id: '1',
|
||||
email: 'admin@example.com',
|
||||
displayName: 'Admin User',
|
||||
roles: ['owner', 'admin'],
|
||||
status: 'active',
|
||||
isSystemAdmin: true,
|
||||
createdAt: '2024-01-01T00:00:00.000Z',
|
||||
updatedAt: '2024-01-01T00:00:00.000Z',
|
||||
lastLoginAt: '2024-01-15T10:00:00.000Z',
|
||||
primaryDriverId: 'driver-1',
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
email: 'user@example.com',
|
||||
displayName: 'Regular User',
|
||||
roles: ['user'],
|
||||
status: 'active',
|
||||
isSystemAdmin: false,
|
||||
createdAt: '2024-01-02T00:00:00.000Z',
|
||||
updatedAt: '2024-01-02T00:00:00.000Z',
|
||||
lastLoginAt: '2024-01-14T15:00:00.000Z',
|
||||
},
|
||||
],
|
||||
total: 2,
|
||||
page: 1,
|
||||
limit: 50,
|
||||
totalPages: 1,
|
||||
activeUserCount: 2,
|
||||
adminCount: 1,
|
||||
};
|
||||
|
||||
mockServiceInstance.listUsers.mockResolvedValue(Result.ok(mockUsers));
|
||||
(AdminUsersViewDataBuilder.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.listUsers).toHaveBeenCalled();
|
||||
expect(AdminUsersViewDataBuilder.build).toHaveBeenCalledWith(mockUsers);
|
||||
});
|
||||
|
||||
it('should return error when service fails', async () => {
|
||||
const serviceError = { type: 'serverError', message: 'Service error' };
|
||||
|
||||
mockServiceInstance.listUsers.mockResolvedValue(Result.err(serviceError));
|
||||
|
||||
const result = await query.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('serverError');
|
||||
});
|
||||
|
||||
it('should return notFound error on 403 exception', async () => {
|
||||
const error = new Error('403 Forbidden');
|
||||
mockServiceInstance.listUsers.mockRejectedValue(error);
|
||||
|
||||
const result = await query.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('notFound');
|
||||
});
|
||||
|
||||
it('should return notFound error on 401 exception', async () => {
|
||||
const error = new Error('401 Unauthorized');
|
||||
mockServiceInstance.listUsers.mockRejectedValue(error);
|
||||
|
||||
const result = await query.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('notFound');
|
||||
});
|
||||
|
||||
it('should return serverError on other exceptions', async () => {
|
||||
const error = new Error('Unexpected error');
|
||||
mockServiceInstance.listUsers.mockRejectedValue(error);
|
||||
|
||||
const result = await query.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('serverError');
|
||||
});
|
||||
|
||||
it('should provide a static execute method', async () => {
|
||||
const mockUsers: UserListResponse = {
|
||||
users: [
|
||||
{
|
||||
id: '1',
|
||||
email: 'admin@example.com',
|
||||
displayName: 'Admin User',
|
||||
roles: ['owner', 'admin'],
|
||||
status: 'active',
|
||||
isSystemAdmin: true,
|
||||
createdAt: '2024-01-01T00:00:00.000Z',
|
||||
updatedAt: '2024-01-01T00:00:00.000Z',
|
||||
lastLoginAt: '2024-01-15T10:00:00.000Z',
|
||||
primaryDriverId: 'driver-1',
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
email: 'user@example.com',
|
||||
displayName: 'Regular User',
|
||||
roles: ['user'],
|
||||
status: 'active',
|
||||
isSystemAdmin: false,
|
||||
createdAt: '2024-01-02T00:00:00.000Z',
|
||||
updatedAt: '2024-01-02T00:00:00.000Z',
|
||||
lastLoginAt: '2024-01-14T15:00:00.000Z',
|
||||
},
|
||||
],
|
||||
total: 2,
|
||||
page: 1,
|
||||
limit: 50,
|
||||
totalPages: 1,
|
||||
};
|
||||
|
||||
const mockViewData = {
|
||||
users: [
|
||||
{
|
||||
id: '1',
|
||||
email: 'admin@example.com',
|
||||
displayName: 'Admin User',
|
||||
roles: ['owner', 'admin'],
|
||||
status: 'active',
|
||||
isSystemAdmin: true,
|
||||
createdAt: '2024-01-01T00:00:00.000Z',
|
||||
updatedAt: '2024-01-01T00:00:00.000Z',
|
||||
lastLoginAt: '2024-01-15T10:00:00.000Z',
|
||||
primaryDriverId: 'driver-1',
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
email: 'user@example.com',
|
||||
displayName: 'Regular User',
|
||||
roles: ['user'],
|
||||
status: 'active',
|
||||
isSystemAdmin: false,
|
||||
createdAt: '2024-01-02T00:00:00.000Z',
|
||||
updatedAt: '2024-01-02T00:00:00.000Z',
|
||||
lastLoginAt: '2024-01-14T15:00:00.000Z',
|
||||
},
|
||||
],
|
||||
total: 2,
|
||||
page: 1,
|
||||
limit: 50,
|
||||
totalPages: 1,
|
||||
activeUserCount: 2,
|
||||
adminCount: 1,
|
||||
};
|
||||
|
||||
mockServiceInstance.listUsers.mockResolvedValue(Result.ok(mockUsers));
|
||||
(AdminUsersViewDataBuilder.build as any).mockReturnValue(mockViewData);
|
||||
|
||||
const result = await AdminUsersPageQuery.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual(mockViewData);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user