109 lines
3.1 KiB
TypeScript
109 lines
3.1 KiB
TypeScript
import { vi } from 'vitest';
|
|
import { AuthController } from './AuthController';
|
|
import { AuthService } from './AuthService';
|
|
import { SignupParams, LoginParams, AuthSessionDTO } from './dtos/AuthDto';
|
|
|
|
describe('AuthController', () => {
|
|
let controller: AuthController;
|
|
let service: AuthService;
|
|
|
|
beforeEach(() => {
|
|
service = {
|
|
signupWithEmail: vi.fn(),
|
|
loginWithEmail: vi.fn(),
|
|
getCurrentSession: vi.fn(),
|
|
logout: vi.fn(),
|
|
} as unknown as AuthService;
|
|
|
|
controller = new AuthController(service);
|
|
});
|
|
|
|
describe('signup', () => {
|
|
it('should call service.signupWithEmail and return session DTO', async () => {
|
|
const params: SignupParams = {
|
|
email: 'test@example.com',
|
|
password: 'password123',
|
|
displayName: 'Test User',
|
|
iracingCustomerId: '12345',
|
|
primaryDriverId: 'driver1',
|
|
avatarUrl: 'http://example.com/avatar.jpg',
|
|
};
|
|
const session: AuthSessionDTO = {
|
|
token: 'token123',
|
|
user: {
|
|
userId: 'user1',
|
|
email: 'test@example.com',
|
|
displayName: 'Test User',
|
|
},
|
|
};
|
|
(service.signupWithEmail as jest.Mock).mockResolvedValue(session);
|
|
|
|
const result = await controller.signup(params);
|
|
|
|
expect(service.signupWithEmail).toHaveBeenCalledWith(params);
|
|
expect(result).toEqual(session);
|
|
});
|
|
});
|
|
|
|
describe('login', () => {
|
|
it('should call service.loginWithEmail and return session DTO', async () => {
|
|
const params: LoginParams = {
|
|
email: 'test@example.com',
|
|
password: 'password123',
|
|
};
|
|
const session: AuthSessionDTO = {
|
|
token: 'token123',
|
|
user: {
|
|
userId: 'user1',
|
|
email: 'test@example.com',
|
|
displayName: 'Test User',
|
|
},
|
|
};
|
|
(service.loginWithEmail as jest.Mock).mockResolvedValue(session);
|
|
|
|
const result = await controller.login(params);
|
|
|
|
expect(service.loginWithEmail).toHaveBeenCalledWith(params);
|
|
expect(result).toEqual(session);
|
|
});
|
|
});
|
|
|
|
describe('getSession', () => {
|
|
it('should call service.getCurrentSession and return session DTO', async () => {
|
|
const session: AuthSessionDTO = {
|
|
token: 'token123',
|
|
user: {
|
|
userId: 'user1',
|
|
email: 'test@example.com',
|
|
displayName: 'Test User',
|
|
},
|
|
};
|
|
(service.getCurrentSession as jest.Mock).mockResolvedValue(session);
|
|
|
|
const result = await controller.getSession();
|
|
|
|
expect(service.getCurrentSession).toHaveBeenCalled();
|
|
expect(result).toEqual(session);
|
|
});
|
|
|
|
it('should return null if no session', async () => {
|
|
(service.getCurrentSession as jest.Mock).mockResolvedValue(null);
|
|
|
|
const result = await controller.getSession();
|
|
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('logout', () => {
|
|
it('should call service.logout and return DTO', async () => {
|
|
const dto = { success: true };
|
|
(service.logout as jest.Mock).mockResolvedValue(dto);
|
|
|
|
const result = await controller.logout();
|
|
|
|
expect(service.logout).toHaveBeenCalled();
|
|
expect(result).toEqual(dto);
|
|
});
|
|
});
|
|
}); |