Files
gridpilot.gg/apps/website/lib/mutations/admin/DeleteUserMutation.test.ts
Marc Mintel 1b0a1f4aee
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 7m11s
Contract Testing / contract-snapshot (pull_request) Has been skipped
view data fixes
2026-01-24 23:29:55 +01:00

194 lines
6.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { DeleteUserMutation } from './DeleteUserMutation';
import { AdminService } from '@/lib/services/admin/AdminService';
import { Result } from '@/lib/contracts/Result';
// Mock dependencies
vi.mock('@/lib/services/admin/AdminService', () => {
return {
AdminService: vi.fn(),
};
});
vi.mock('@/lib/config/apiBaseUrl', () => ({
getWebsiteApiBaseUrl: () => 'http://localhost:3000',
}));
vi.mock('@/lib/config/env', () => ({
getWebsiteServerEnv: () => ({ NODE_ENV: 'test' }),
}));
describe('DeleteUserMutation', () => {
let mutation: DeleteUserMutation;
let mockServiceInstance: any;
beforeEach(() => {
vi.clearAllMocks();
mutation = new DeleteUserMutation();
mockServiceInstance = {
deleteUser: vi.fn(),
};
// Use mockImplementation to return the instance
(AdminService as any).mockImplementation(function() {
return mockServiceInstance;
});
});
describe('execute', () => {
describe('happy paths', () => {
it('should successfully delete a user', async () => {
// Arrange
const input = { userId: 'user-123' };
mockServiceInstance.deleteUser.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(mockServiceInstance.deleteUser).toHaveBeenCalledTimes(1);
});
it('should handle deletion without userId parameter', async () => {
// Arrange
const input = { userId: 'user-456' };
mockServiceInstance.deleteUser.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(mockServiceInstance.deleteUser).toHaveBeenCalledWith();
});
});
describe('failure modes', () => {
it('should handle service failure during deletion', async () => {
// Arrange
const input = { userId: 'user-123' };
const serviceError = new Error('Service error');
mockServiceInstance.deleteUser.mockRejectedValue(serviceError);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('deleteFailed');
expect(mockServiceInstance.deleteUser).toHaveBeenCalledTimes(1);
});
it('should handle service returning error result', async () => {
// Arrange
const input = { userId: 'user-123' };
const domainError = { type: 'serverError', message: 'Database connection failed' };
mockServiceInstance.deleteUser.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('serverError');
expect(mockServiceInstance.deleteUser).toHaveBeenCalledTimes(1);
});
it('should handle service returning userNotFound error', async () => {
// Arrange
const input = { userId: 'user-999' };
const domainError = { type: 'notFound', message: 'User not found' };
mockServiceInstance.deleteUser.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('userNotFound');
expect(mockServiceInstance.deleteUser).toHaveBeenCalledTimes(1);
});
it('should handle service returning noPermission error', async () => {
// Arrange
const input = { userId: 'user-123' };
const domainError = { type: 'unauthorized', message: 'Insufficient permissions' };
mockServiceInstance.deleteUser.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('noPermission');
expect(mockServiceInstance.deleteUser).toHaveBeenCalledTimes(1);
});
});
describe('error mapping', () => {
it('should map various domain errors to mutation errors', async () => {
// Arrange
const input = { userId: 'user-123' };
const testCases = [
{ domainError: { type: 'notFound' }, expectedError: 'userNotFound' },
{ domainError: { type: 'unauthorized' }, expectedError: 'noPermission' },
{ domainError: { type: 'validationError' }, expectedError: 'invalidData' },
{ domainError: { type: 'serverError' }, expectedError: 'serverError' },
{ domainError: { type: 'networkError' }, expectedError: 'networkError' },
{ domainError: { type: 'notImplemented' }, expectedError: 'notImplemented' },
{ domainError: { type: 'unknown' }, expectedError: 'unknown' },
];
for (const testCase of testCases) {
mockServiceInstance.deleteUser.mockResolvedValue(Result.err(testCase.domainError));
const result = await mutation.execute(input);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe(testCase.expectedError);
}
});
});
describe('input validation', () => {
it('should accept valid userId input', async () => {
// Arrange
const input = { userId: 'user-123' };
mockServiceInstance.deleteUser.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.deleteUser).toHaveBeenCalledTimes(1);
});
it('should handle empty userId gracefully', async () => {
// Arrange
const input = { userId: '' };
mockServiceInstance.deleteUser.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.deleteUser).toHaveBeenCalledTimes(1);
});
});
describe('service instantiation', () => {
it('should create AdminService instance', () => {
// Arrange & Act
const mutation = new DeleteUserMutation();
// Assert
expect(mutation).toBeInstanceOf(DeleteUserMutation);
});
});
});
});