Files
gridpilot.gg/apps/website/lib/mutations/leagues/CreateLeagueMutation.test.ts
Marc Mintel 1b0a1f4aee
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 7m11s
Contract Testing / contract-snapshot (pull_request) Has been skipped
view data fixes
2026-01-24 23:29:55 +01:00

111 lines
3.3 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { CreateLeagueMutation } from './CreateLeagueMutation';
import { LeagueService } from '@/lib/services/leagues/LeagueService';
// Mock dependencies
vi.mock('@/lib/services/leagues/LeagueService', () => {
return {
LeagueService: vi.fn(),
};
});
describe('CreateLeagueMutation', () => {
let mutation: CreateLeagueMutation;
let mockServiceInstance: any;
beforeEach(() => {
vi.clearAllMocks();
mockServiceInstance = {
createLeague: vi.fn(),
};
// Use mockImplementation to return the instance
(LeagueService as any).mockImplementation(function() {
return mockServiceInstance;
});
mutation = new CreateLeagueMutation();
});
describe('execute', () => {
describe('happy paths', () => {
it('should successfully create a league with valid input', async () => {
// Arrange
const input = {
name: 'Test League',
description: 'A test league',
visibility: 'public',
ownerId: 'owner-123',
};
const mockResult = { leagueId: 'league-123' };
mockServiceInstance.createLeague.mockResolvedValue(mockResult);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBe('league-123');
expect(mockServiceInstance.createLeague).toHaveBeenCalledWith(input);
expect(mockServiceInstance.createLeague).toHaveBeenCalledTimes(1);
});
});
describe('failure modes', () => {
it('should handle service failure during league creation', async () => {
// Arrange
const input = {
name: 'Test League',
description: 'A test league',
visibility: 'public',
ownerId: 'owner-123',
};
const serviceError = new Error('Service error');
mockServiceInstance.createLeague.mockRejectedValue(serviceError);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toStrictEqual({
type: 'serverError',
message: 'Service error',
});
expect(mockServiceInstance.createLeague).toHaveBeenCalledTimes(1);
});
});
describe('service instantiation', () => {
it('should create LeagueService instance', () => {
// Arrange & Act
const mutation = new CreateLeagueMutation();
// Assert
expect(mutation).toBeInstanceOf(CreateLeagueMutation);
});
});
describe('result shape', () => {
it('should return leagueId string on success', async () => {
// Arrange
const input = {
name: 'Test League',
description: 'A test league',
visibility: 'public',
ownerId: 'owner-123',
};
const mockResult = { leagueId: 'league-123' };
mockServiceInstance.createLeague.mockResolvedValue(mockResult);
// Act
const result = await mutation.execute(input);
// Assert
expect(result.isOk()).toBe(true);
const leagueId = result.unwrap();
expect(typeof leagueId).toBe('string');
expect(leagueId).toBe('league-123');
});
});
});
});