campverse-backend / __tests__ /unit /simpleUnit.test.js
CampVerse CI
Deploy backend from GitHub Actions
c904036
Raw History Blame Contribute Delete
6.34 kB
// Simple Unit Tests for Core Functions
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
// Mock external dependencies
jest.mock('bcrypt');
jest.mock('jsonwebtoken');
describe('Simple Unit Tests', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('Password Hashing', () => {
it('should hash password correctly', async () => {
const password = 'testpassword';
const hashedPassword = 'hashedpassword123';
bcrypt.hash.mockResolvedValue(hashedPassword);
const result = await bcrypt.hash(password, 10);
expect(result).toBe(hashedPassword);
expect(bcrypt.hash).toHaveBeenCalledWith(password, 10);
});
it('should compare password correctly', async () => {
const password = 'testpassword';
const hashedPassword = 'hashedpassword123';
bcrypt.compare.mockResolvedValue(true);
const result = await bcrypt.compare(password, hashedPassword);
expect(result).toBe(true);
expect(bcrypt.compare).toHaveBeenCalledWith(password, hashedPassword);
});
});
describe('JWT Token', () => {
it('should generate JWT token', () => {
const payload = { userId: 'user123' };
const token = 'mock.jwt.token';
jwt.sign.mockReturnValue(token);
const result = jwt.sign(payload, 'secret', { expiresIn: '7d' });
expect(result).toBe(token);
expect(jwt.sign).toHaveBeenCalledWith(payload, 'secret', {
expiresIn: '7d',
});
});
it('should verify JWT token', () => {
const token = 'mock.jwt.token';
const decoded = { userId: 'user123' };
jwt.verify.mockReturnValue(decoded);
const result = jwt.verify(token, 'secret');
expect(result).toEqual(decoded);
expect(jwt.verify).toHaveBeenCalledWith(token, 'secret');
});
});
describe('Utility Functions', () => {
it('should validate email format', () => {
const validateEmail = (email) => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
};
expect(validateEmail('test@example.com')).toBe(true);
expect(validateEmail('invalid-email')).toBe(false);
expect(validateEmail('test@')).toBe(false);
});
it('should validate phone number format', () => {
const validatePhone = (phone) => {
const phoneRegex = /^[0-9]{10}$/;
return phoneRegex.test(phone);
};
expect(validatePhone('1234567890')).toBe(true);
expect(validatePhone('123456789')).toBe(false);
expect(validatePhone('12345678901')).toBe(false);
expect(validatePhone('123456789a')).toBe(false);
});
it('should validate password strength', () => {
const { validatePassword } = require('../../Utils/passwordUtils');
// Valid passwords with uppercase, lowercase, number, and special char
expect(validatePassword('Password123!')).toBe(true);
expect(validatePassword('Valid@Pass1')).toBe(true);
// Invalid passwords
expect(validatePassword('pass')).toBe(false);
expect(validatePassword('')).toBe(false);
expect(validatePassword(null)).toBe(false);
expect(validatePassword(undefined)).toBe(false);
});
it('should validate name format', () => {
const validateName = (name) => {
return Boolean(name && name.trim().length >= 2);
};
expect(validateName('John')).toBe(true);
expect(validateName('A')).toBe(false);
expect(validateName('')).toBe(false);
expect(validateName(' ')).toBe(false);
expect(validateName(null)).toBe(false);
expect(validateName(undefined)).toBe(false);
});
});
describe('Date and Time Functions', () => {
it('should check if date is in the future', () => {
const isFutureDate = (date) => {
return new Date(date) > new Date();
};
const futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 1);
const pastDate = new Date();
pastDate.setDate(pastDate.getDate() - 1);
expect(isFutureDate(futureDate)).toBe(true);
expect(isFutureDate(pastDate)).toBe(false);
});
it('should format date correctly', () => {
const formatDate = (date) => {
return new Date(date).toISOString().split('T')[0];
};
const testDate = new Date('2024-01-15');
expect(formatDate(testDate)).toBe('2024-01-15');
});
});
describe('String Manipulation', () => {
it('should capitalize first letter', () => {
const capitalize = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
expect(capitalize('hello')).toBe('Hello');
expect(capitalize('world')).toBe('World');
expect(capitalize('')).toBe('');
});
it('should truncate long strings', () => {
const truncate = (str, length) => {
return str.length > length ? `${str.substring(0, length)}...` : str;
};
expect(truncate('Hello World', 5)).toBe('Hello...');
expect(truncate('Short', 10)).toBe('Short');
});
});
describe('Array Operations', () => {
it('should remove duplicates from array', () => {
const removeDuplicates = (arr) => {
return [...new Set(arr)];
};
expect(removeDuplicates([1, 2, 2, 3, 3, 4])).toEqual([1, 2, 3, 4]);
expect(removeDuplicates(['a', 'b', 'a', 'c'])).toEqual(['a', 'b', 'c']);
});
it('should find unique values', () => {
const findUnique = (arr) => {
return arr.filter((item, index) => arr.indexOf(item) === index);
};
expect(findUnique([1, 2, 2, 3, 3, 4])).toEqual([1, 2, 3, 4]);
});
});
describe('Object Operations', () => {
it('should merge objects', () => {
const mergeObjects = (obj1, obj2) => {
return { ...obj1, ...obj2 };
};
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
expect(mergeObjects(obj1, obj2)).toEqual({ a: 1, b: 3, c: 4 });
});
it('should remove null/undefined values', () => {
const removeNullValues = (obj) => {
return Object.fromEntries(
Object.entries(obj).filter(([_, value]) => value != null),
);
};
const testObj = { a: 1, b: null, c: undefined, d: 2 };
expect(removeNullValues(testObj)).toEqual({ a: 1, d: 2 });
});
});
});