core tests
This commit is contained in:
303
core/admin/domain/errors/AdminDomainError.test.ts
Normal file
303
core/admin/domain/errors/AdminDomainError.test.ts
Normal file
@@ -0,0 +1,303 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { AdminDomainError, AdminDomainValidationError, AdminDomainInvariantError, AuthorizationError } from './AdminDomainError';
|
||||
|
||||
describe('AdminDomainError', () => {
|
||||
describe('TDD - Test First', () => {
|
||||
describe('AdminDomainError', () => {
|
||||
it('should create an error with correct properties', () => {
|
||||
// Arrange & Act
|
||||
const error = new (class extends AdminDomainError {
|
||||
readonly kind = 'validation' as const;
|
||||
})('Test error message');
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe('Test error message');
|
||||
expect(error.type).toBe('domain');
|
||||
expect(error.context).toBe('admin-domain');
|
||||
expect(error.kind).toBe('validation');
|
||||
});
|
||||
|
||||
it('should have correct error name', () => {
|
||||
// Arrange & Act
|
||||
const error = new (class extends AdminDomainError {
|
||||
readonly kind = 'validation' as const;
|
||||
})('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error.name).toBe('AdminDomainError');
|
||||
});
|
||||
|
||||
it('should preserve prototype chain', () => {
|
||||
// Arrange & Act
|
||||
const error = new (class extends AdminDomainError {
|
||||
readonly kind = 'validation' as const;
|
||||
})('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error instanceof AdminDomainError).toBe(true);
|
||||
expect(error instanceof Error).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle empty message', () => {
|
||||
// Arrange & Act
|
||||
const error = new (class extends AdminDomainError {
|
||||
readonly kind = 'validation' as const;
|
||||
})('');
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe('');
|
||||
});
|
||||
|
||||
it('should handle long message', () => {
|
||||
// Arrange
|
||||
const longMessage = 'This is a very long error message that contains many characters and should be handled correctly by the error class';
|
||||
|
||||
// Act
|
||||
const error = new (class extends AdminDomainError {
|
||||
readonly kind = 'validation' as const;
|
||||
})(longMessage);
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe(longMessage);
|
||||
});
|
||||
});
|
||||
|
||||
describe('AdminDomainValidationError', () => {
|
||||
it('should create a validation error', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainValidationError('Invalid email format');
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe('Invalid email format');
|
||||
expect(error.type).toBe('domain');
|
||||
expect(error.context).toBe('admin-domain');
|
||||
expect(error.kind).toBe('validation');
|
||||
});
|
||||
|
||||
it('should have correct error name', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainValidationError('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error.name).toBe('AdminDomainValidationError');
|
||||
});
|
||||
|
||||
it('should be instance of AdminDomainError', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainValidationError('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error instanceof AdminDomainError).toBe(true);
|
||||
expect(error instanceof AdminDomainValidationError).toBe(true);
|
||||
expect(error instanceof Error).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle empty message', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainValidationError('');
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe('');
|
||||
});
|
||||
|
||||
it('should handle complex validation message', () => {
|
||||
// Arrange
|
||||
const message = 'Field "email" must be a valid email address. Received: "invalid-email"';
|
||||
|
||||
// Act
|
||||
const error = new AdminDomainValidationError(message);
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe(message);
|
||||
});
|
||||
});
|
||||
|
||||
describe('AdminDomainInvariantError', () => {
|
||||
it('should create an invariant error', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainInvariantError('User must have at least one role');
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe('User must have at least one role');
|
||||
expect(error.type).toBe('domain');
|
||||
expect(error.context).toBe('admin-domain');
|
||||
expect(error.kind).toBe('invariant');
|
||||
});
|
||||
|
||||
it('should have correct error name', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainInvariantError('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error.name).toBe('AdminDomainInvariantError');
|
||||
});
|
||||
|
||||
it('should be instance of AdminDomainError', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainInvariantError('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error instanceof AdminDomainError).toBe(true);
|
||||
expect(error instanceof AdminDomainInvariantError).toBe(true);
|
||||
expect(error instanceof Error).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle empty message', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainInvariantError('');
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe('');
|
||||
});
|
||||
|
||||
it('should handle complex invariant message', () => {
|
||||
// Arrange
|
||||
const message = 'Invariant violation: User status "active" cannot be changed to "deleted" without proper authorization';
|
||||
|
||||
// Act
|
||||
const error = new AdminDomainInvariantError(message);
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe(message);
|
||||
});
|
||||
});
|
||||
|
||||
describe('AuthorizationError', () => {
|
||||
it('should create an authorization error', () => {
|
||||
// Arrange & Act
|
||||
const error = new AuthorizationError('User does not have permission to perform this action');
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe('User does not have permission to perform this action');
|
||||
expect(error.type).toBe('domain');
|
||||
expect(error.context).toBe('admin-domain');
|
||||
expect(error.kind).toBe('authorization');
|
||||
});
|
||||
|
||||
it('should have correct error name', () => {
|
||||
// Arrange & Act
|
||||
const error = new AuthorizationError('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error.name).toBe('AuthorizationError');
|
||||
});
|
||||
|
||||
it('should be instance of AdminDomainError', () => {
|
||||
// Arrange & Act
|
||||
const error = new AuthorizationError('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error instanceof AdminDomainError).toBe(true);
|
||||
expect(error instanceof AuthorizationError).toBe(true);
|
||||
expect(error instanceof Error).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle empty message', () => {
|
||||
// Arrange & Act
|
||||
const error = new AuthorizationError('');
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe('');
|
||||
});
|
||||
|
||||
it('should handle complex authorization message', () => {
|
||||
// Arrange
|
||||
const message = 'Authorization failed: User "admin@example.com" (role: admin) attempted to modify role of user "owner@example.com" (role: owner)';
|
||||
|
||||
// Act
|
||||
const error = new AuthorizationError(message);
|
||||
|
||||
// Assert
|
||||
expect(error.message).toBe(message);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Error hierarchy', () => {
|
||||
it('should have correct inheritance chain for AdminDomainValidationError', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainValidationError('Test');
|
||||
|
||||
// Assert
|
||||
expect(error instanceof AdminDomainError).toBe(true);
|
||||
expect(error instanceof Error).toBe(true);
|
||||
});
|
||||
|
||||
it('should have correct inheritance chain for AdminDomainInvariantError', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainInvariantError('Test');
|
||||
|
||||
// Assert
|
||||
expect(error instanceof AdminDomainError).toBe(true);
|
||||
expect(error instanceof Error).toBe(true);
|
||||
});
|
||||
|
||||
it('should have correct inheritance chain for AuthorizationError', () => {
|
||||
// Arrange & Act
|
||||
const error = new AuthorizationError('Test');
|
||||
|
||||
// Assert
|
||||
expect(error instanceof AdminDomainError).toBe(true);
|
||||
expect(error instanceof Error).toBe(true);
|
||||
});
|
||||
|
||||
it('should have consistent type and context across all error types', () => {
|
||||
// Arrange
|
||||
const errors = [
|
||||
new AdminDomainValidationError('Test'),
|
||||
new AdminDomainInvariantError('Test'),
|
||||
new AuthorizationError('Test'),
|
||||
];
|
||||
|
||||
// Assert
|
||||
errors.forEach(error => {
|
||||
expect(error.type).toBe('domain');
|
||||
expect(error.context).toBe('admin-domain');
|
||||
});
|
||||
});
|
||||
|
||||
it('should have different kinds for different error types', () => {
|
||||
// Arrange
|
||||
const validationError = new AdminDomainValidationError('Test');
|
||||
const invariantError = new AdminDomainInvariantError('Test');
|
||||
const authorizationError = new AuthorizationError('Test');
|
||||
|
||||
// Assert
|
||||
expect(validationError.kind).toBe('validation');
|
||||
expect(invariantError.kind).toBe('invariant');
|
||||
expect(authorizationError.kind).toBe('authorization');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Error stack trace', () => {
|
||||
it('should have a stack trace', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainValidationError('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error.stack).toBeDefined();
|
||||
expect(typeof error.stack).toBe('string');
|
||||
expect(error.stack).toContain('AdminDomainValidationError');
|
||||
});
|
||||
|
||||
it('should have stack trace for AdminDomainInvariantError', () => {
|
||||
// Arrange & Act
|
||||
const error = new AdminDomainInvariantError('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error.stack).toBeDefined();
|
||||
expect(typeof error.stack).toBe('string');
|
||||
expect(error.stack).toContain('AdminDomainInvariantError');
|
||||
});
|
||||
|
||||
it('should have stack trace for AuthorizationError', () => {
|
||||
// Arrange & Act
|
||||
const error = new AuthorizationError('Test error');
|
||||
|
||||
// Assert
|
||||
expect(error.stack).toBeDefined();
|
||||
expect(typeof error.stack).toBe('string');
|
||||
expect(error.stack).toContain('AuthorizationError');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user