136 lines
4.4 KiB
TypeScript
136 lines
4.4 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { LogoutMutation } from './LogoutMutation';
|
|
import { AuthService } from '@/lib/services/auth/AuthService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/auth/AuthService', () => {
|
|
return {
|
|
AuthService: vi.fn(),
|
|
};
|
|
});
|
|
|
|
describe('LogoutMutation', () => {
|
|
let mutation: LogoutMutation;
|
|
let mockServiceInstance: { logout: ReturnType<typeof vi.fn> };
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mutation = new LogoutMutation();
|
|
mockServiceInstance = {
|
|
logout: vi.fn(),
|
|
};
|
|
// Use mockImplementation to return the instance
|
|
(AuthService as any).mockImplementation(function() {
|
|
return mockServiceInstance;
|
|
});
|
|
});
|
|
|
|
describe('execute', () => {
|
|
describe('happy paths', () => {
|
|
it('should successfully logout', async () => {
|
|
// Arrange
|
|
mockServiceInstance.logout.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute();
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.logout).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('failure modes', () => {
|
|
it('should handle service failure during logout', async () => {
|
|
// Arrange
|
|
const serviceError = new Error('Session expired');
|
|
mockServiceInstance.logout.mockRejectedValue(serviceError);
|
|
|
|
// Act
|
|
const result = await mutation.execute();
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Session expired');
|
|
expect(mockServiceInstance.logout).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning error result', async () => {
|
|
// Arrange
|
|
const domainError = { type: 'serverError', message: 'Failed to clear session' };
|
|
mockServiceInstance.logout.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.execute();
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Failed to clear session');
|
|
expect(mockServiceInstance.logout).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning unauthorized error', async () => {
|
|
// Arrange
|
|
const domainError = { type: 'unauthorized', message: 'Not authenticated' };
|
|
mockServiceInstance.logout.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.execute();
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Not authenticated');
|
|
expect(mockServiceInstance.logout).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('error mapping', () => {
|
|
it('should map various domain errors to mutation errors', async () => {
|
|
// Arrange
|
|
const testCases = [
|
|
{ domainError: { type: 'serverError', message: 'Server error' }, expectedError: 'Server error' },
|
|
{ domainError: { type: 'unauthorized', message: 'Unauthorized' }, expectedError: 'Unauthorized' },
|
|
{ domainError: { type: 'notFound', message: 'Session not found' }, expectedError: 'Session not found' },
|
|
{ domainError: { type: 'networkError', message: 'Network error' }, expectedError: 'Network error' },
|
|
];
|
|
|
|
for (const testCase of testCases) {
|
|
mockServiceInstance.logout.mockResolvedValue(Result.err(testCase.domainError));
|
|
|
|
const result = await mutation.execute();
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe(testCase.expectedError);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('service instantiation', () => {
|
|
it('should create AuthService instance', () => {
|
|
// Arrange & Act
|
|
const mutation = new LogoutMutation();
|
|
|
|
// Assert
|
|
expect(mutation).toBeInstanceOf(LogoutMutation);
|
|
});
|
|
});
|
|
|
|
describe('result shape', () => {
|
|
it('should return void result on success', async () => {
|
|
// Arrange
|
|
mockServiceInstance.logout.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute();
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(typeof result.unwrap()).toBe('undefined');
|
|
});
|
|
});
|
|
});
|
|
});
|