// tests/unit/services/authService.test.js const authService = require('../../../services/authService'); // Mock user data for testing const mockUsers = []; // Since we're testing the service logic, we'll need to test with the actual implementation // which uses a global array - this is a limitation of the current implementation // In a real app, we would use dependency injection or mocking describe('AuthService', () => { // These tests will need to be adjusted since the service maintains state in a global array test('should return an error for invalid credentials', async () => { const result = await authService.login('nonexistent@example.com', 'wrongpassword'); expect(result).toHaveProperty('error'); expect(result.error).toBe('Invalid email or password'); }); test('should successfully register a new user', async () => { const userData = { email: 'newuser@example.com', password: 'securepassword', firstName: 'New', lastName: 'User', userType: 'job-seeker', tenantId: 'test-tenant' }; const result = await authService.register( userData.email, userData.password, userData.firstName, userData.lastName, userData.userType, userData.tenantId ); expect(result).toHaveProperty('user'); expect(result.user).toHaveProperty('id'); expect(result.user.email).toBe(userData.email); expect(result.user.firstName).toBe(userData.firstName); expect(result.user.lastName).toBe(userData.lastName); expect(result.user.userType).toBe(userData.userType); expect(result.user.tenantId).toBe(userData.tenantId); }); test('should return error when trying to register with existing email', async () => { const userData = { email: 'existing@example.com', password: 'password', firstName: 'Existing', lastName: 'User', userType: 'job-seeker', tenantId: 'test-tenant' }; // Register the user first await authService.register( userData.email, userData.password, userData.firstName, userData.lastName, userData.userType, userData.tenantId ); // Try to register the same email again const result = await authService.register( userData.email, userData.password, userData.firstName, userData.lastName, userData.userType, userData.tenantId ); expect(result).toHaveProperty('error'); expect(result.error).toBe('User with this email already exists'); }); test('should return error for invalid user type', async () => { const result = await authService.register( 'invalidtype@example.com', 'password', 'Test', 'User', 'invalid-type', 'test-tenant' ); expect(result).toHaveProperty('error'); expect(result.error).toBe('User type must be either job-seeker or job-provider'); }); });