jest.mock('./firebase/config'); import functionsTest from 'firebase-functions-test';…
- User
jest.mock('./firebase/config');
import functionsTest from 'firebase-functions-test'; import { createGame } from './createGame'; import { db } from './firebase/config';
const testEnv = functionsTest();
const mockCollection = db.collection as jest.Mock; const mockAdd = jest.fn();
describe('createGame', () => { let wrapped: any;
beforeAll(() => { wrapped = testEnv.wrap(createGame); }); beforeEach(() => { jest.resetAllMocks(); const mockWhere = jest.fn().mockReturnThis(); const mockGet = jest.fn().mockResolvedValue({ empty: true }); mockCollection.mockImplementation(() => ({ where: mockWhere, get: mockGet, add: mockAdd, })); mockAdd.mockResolvedValue({ id: 'game123' }); }); afterAll(() => { testEnv.cleanup(); }); it('should successfully create a new game with valid inputs', async () => { const data = { name: 'TestPlayer', numberOfTeams: 2, playersPerTeam: 3, numberOfRounds: 5, jokerUsage: true, }; const context = { auth: { uid: 'test-uid' } }; const result = await wrapped(data, context); expect(result).toHaveProperty('gameId', 'game123'); expect(result).toHaveProperty('gameCode'); expect(mockCollection).toHaveBeenCalledWith('games'); expect(mockAdd).toHaveBeenCalledWith(expect.objectContaining({ createdBy: 'TestPlayer', roundHost: 'TestPlayer', teams: expect.arrayContaining([ expect.objectContaining({ players: expect.arrayContaining([{ name: 'TestPlayer' }]) }) ]) })); }); it('should throw an unauthenticated error if user is not authenticated', async () => { const data = { name: 'TestPlayer', numberOfTeams: 2, playersPerTeam: 3, numberOfRounds: 5, jokerUsage: true, }; const context = {}; // No auth await expect(wrapped(data, context)).rejects.toThrow( 'The function must be called while authenticated.' ); }); it('should throw an invalid-argument error for missing name', async () => { const data = { numberOfTeams: 2, playersPerTeam: 3, numberOfRounds: 5, jokerUsage: true, }; const context = { auth: { uid: 'test-uid' } }; await expect(wrapped(data, context)).rejects.toThrow( 'Name is required and must be between 3 and 20 characters.' ); }); it('should throw an invalid-argument error for missing numberOfTeams', async () => { const data = { name: 'TestPlayer', playersPerTeam: 3, numberOfRounds: 5, jokerUsage: true, }; const context = { auth: { uid: 'test-uid' } }; await expect(wrapped(data, context)).rejects.toThrow( 'Number of teams is required and must be a positive integer.' ); }); it('should throw an invalid-argument error for invalid playersPerTeam', async () => { const data = { name: 'TestPlayer', numberOfTeams: 2, playersPerTeam: -1, numberOfRounds: 5, jokerUsage: true, }; const context = { auth: { uid: 'test-uid' } }; await expect(wrapped(data, context)).rejects.toThrow( 'Number of players per team is required and must be a positive integer.' ); }); it('should throw an invalid-argument error for invalid numberOfRounds', async () => { const data = { name: 'TestPlayer', numberOfTeams: 2, playersPerTeam: 3, numberOfRounds: 0, jokerUsage: true, }; const context = { auth: { uid: 'test-uid' } }; await expect(wrapped(data, context)).rejects.toThrow( 'Number of rounds is required and must be a positive integer.' ); }); it('should throw an invalid-argument error for invalid jokerUsage', async () => { const data = { name: 'TestPlayer', numberOfTeams: 2, playersPerTeam: 3, numberOfRounds: 5, jokerUsage: 'yes', // Invalid boolean }; const context = { auth: { uid: 'test-uid' } }; await expect(wrapped(data, context)).rejects.toThrow( 'Joker usage must be a boolean.' ); }); it('should throw an internal error if Firestore add fails', async () => { mockAdd.mockRejectedValueOnce(new Error('Firestore add error')); const data = { name: 'TestPlayer', numberOfTeams: 2, playersPerTeam: 3, numberOfRounds: 5, jokerUsage: true, }; const context = { auth: { uid: 'test-uid' } }; await expect(wrapped(data, context)).rejects.toThrow( 'Unable to create game at this time.' ); expect(mockAdd).toHaveBeenCalled(); }); it('should retry generating a unique game code on collision', async () => { const mockGet = jest.fn() .mockResolvedValueOnce({ empty: false }) // Collision .mockResolvedValueOnce({ empty: true }); // No collision mockCollection.mockImplementation(() => ({ where: jest.fn().mockReturnThis(), get: mockGet, add: mockAdd, })); const data = { name: 'TestPlayer', numberOfTeams: 2, playersPerTeam: 3, numberOfRounds: 5, jokerUsage: true, }; const context = { auth: { uid: 'test-uid' } }; const result = await wrapped(data, context); expect(result).toHaveProperty('gameId', 'game123'); expect(result).toHaveProperty('gameCode'); expect(mockGet).toHaveBeenCalledTimes(2); }); - Assistant