test(core): add unit tests and test infrastructure

Add test structure:
- Unit tests for BaseEntity
- Unit tests for Guards utility
- Test fixtures with code samples
- Integration and unit test directories
This commit is contained in:
imfozilbek
2025-11-23 21:43:55 +05:00
parent 3dc531886e
commit 8b81e6030d
3 changed files with 119 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
export function add(a: number, b: number): number {
return a + b;
}
export const multiply = (a: number, b: number): number => {
return a * b;
};
export class Calculator {
public divide(a: number, b: number): number {
if (b === 0) {
throw new Error('Division by zero');
}
return a / b;
}
}

View File

@@ -0,0 +1,46 @@
import { describe, it, expect } from 'vitest';
import { BaseEntity } from '../../../src/domain/entities/BaseEntity';
class TestEntity extends BaseEntity {
constructor(id?: string) {
super(id);
}
}
describe('BaseEntity', () => {
it('should create an entity with generated id', () => {
const entity = new TestEntity();
expect(entity.id).toBeDefined();
expect(typeof entity.id).toBe('string');
});
it('should create an entity with provided id', () => {
const customId = 'custom-id-123';
const entity = new TestEntity(customId);
expect(entity.id).toBe(customId);
});
it('should have createdAt and updatedAt timestamps', () => {
const entity = new TestEntity();
expect(entity.createdAt).toBeInstanceOf(Date);
expect(entity.updatedAt).toBeInstanceOf(Date);
});
it('should return true when comparing same entity', () => {
const entity = new TestEntity();
expect(entity.equals(entity)).toBe(true);
});
it('should return true when comparing entities with same id', () => {
const id = 'same-id';
const entity1 = new TestEntity(id);
const entity2 = new TestEntity(id);
expect(entity1.equals(entity2)).toBe(true);
});
it('should return false when comparing entities with different ids', () => {
const entity1 = new TestEntity();
const entity2 = new TestEntity();
expect(entity1.equals(entity2)).toBe(false);
});
});

View File

@@ -0,0 +1,57 @@
import { describe, it, expect } from 'vitest';
import { Guards } from '../../../src/shared/utils/Guards';
describe('Guards', () => {
describe('isNullOrUndefined', () => {
it('should return true for null', () => {
expect(Guards.isNullOrUndefined(null)).toBe(true);
});
it('should return true for undefined', () => {
expect(Guards.isNullOrUndefined(undefined)).toBe(true);
});
it('should return false for other values', () => {
expect(Guards.isNullOrUndefined(0)).toBe(false);
expect(Guards.isNullOrUndefined('')).toBe(false);
expect(Guards.isNullOrUndefined(false)).toBe(false);
});
});
describe('isString', () => {
it('should return true for strings', () => {
expect(Guards.isString('hello')).toBe(true);
expect(Guards.isString('')).toBe(true);
});
it('should return false for non-strings', () => {
expect(Guards.isString(123)).toBe(false);
expect(Guards.isString(null)).toBe(false);
});
});
describe('isEmpty', () => {
it('should return true for empty strings', () => {
expect(Guards.isEmpty('')).toBe(true);
});
it('should return true for empty arrays', () => {
expect(Guards.isEmpty([])).toBe(true);
});
it('should return true for empty objects', () => {
expect(Guards.isEmpty({})).toBe(true);
});
it('should return true for null/undefined', () => {
expect(Guards.isEmpty(null)).toBe(true);
expect(Guards.isEmpty(undefined)).toBe(true);
});
it('should return false for non-empty values', () => {
expect(Guards.isEmpty('text')).toBe(false);
expect(Guards.isEmpty([1])).toBe(false);
expect(Guards.isEmpty({ key: 'value' })).toBe(false);
});
});
});