refactor(tests): update remaining test files for ephemeral team model

This commit is contained in:
2026-04-03 22:00:01 -07:00
parent 9137fabc2b
commit eeb0f7970a
7 changed files with 76 additions and 85 deletions
@@ -28,6 +28,7 @@ const mockTournament = {
description: 'A test tournament',
eventDate: new Date('2024-01-15'),
eventType: 'tournament',
tournamentType: 'individual',
format: 'round_robin',
status: 'planned',
maxParticipants: 16,
@@ -36,6 +37,12 @@ const mockTournament = {
allowTies: false,
createdAt: new Date(),
updatedAt: new Date(),
teamDurability: 'permanent',
partnerRotation: 'none',
allowByes: true,
teamConfiguration: null,
maxRosterChanges: null,
requireAdminVerify: false,
}
describe('EditTournamentForm', () => {
@@ -8,8 +8,8 @@ import { describe, test, expect, mock, beforeEach,} from 'bun:test';
import { prisma } from '@/lib/prisma';
// Create mock functions at module level
const playerFindFirstMock = mock(() => {});
const playerCreateMock = mock(() => {});
const playerFindFirstMock = mock(async (_args: any): Promise<any> => null);
const playerCreateMock = mock(async (_args: any): Promise<any> => ({}));
// Mock the prisma module
mock.module('@/lib/prisma', () => ({
@@ -326,10 +326,16 @@ describe('Player Deduplication', () => {
rating: 0,
};
playerFindFirstMock
.mockResolvedValueOnce(existingPlayer) // First call for "Emily"
.mockResolvedValueOnce(existingPlayer) // Second call for "EMILY"
.mockResolvedValueOnce(existingPlayer); // Third call for " Emily "
// Configure mock to return existing player for these specific calls
playerFindFirstMock.mockImplementation(async (args: any) => {
if (args.where.normalizedName === 'emily') {
return existingPlayer;
}
if (args.where.normalizedName === 'emily') {
return existingPlayer;
}
return null;
});
const result1 = await findOrCreatePlayer('Emily');
const result2 = await findOrCreatePlayer('EMILY');
+9 -9
View File
@@ -11,21 +11,21 @@ import { recalculateAllElo } from '@/lib/elo-utils';
// Mock Prisma client
const mockPrisma = {
player: {
updateMany: mock(() => {}).mockResolvedValue({ count: 0 }),
update: mock(() => {}).mockResolvedValue({}),
updateMany: mock(async () => ({ count: 0 })),
update: mock(async () => ({})),
},
eloSnapshot: {
deleteMany: mock(() => {}).mockResolvedValue({ count: 0 }),
create: mock(() => {}).mockResolvedValue({}),
deleteMany: mock(async () => ({ count: 0 })),
create: mock(async () => ({})),
},
partnershipStat: {
deleteMany: mock(() => {}).mockResolvedValue({ count: 0 }),
findFirst: mock(() => {}).mockResolvedValue(null), // No existing stats initially
update: mock(() => {}).mockResolvedValue({}),
create: mock(() => {}).mockResolvedValue({}),
deleteMany: mock(async () => ({ count: 0 })),
findFirst: mock(async () => null), // No existing stats initially
update: mock(async () => ({})),
create: mock(async () => ({})),
},
match: {
findMany: mock(() => {}).mockResolvedValue([]),
findMany: mock(async () => []),
},
};
@@ -55,6 +55,7 @@ const createMockTournament = (id: number, ownerId: string | null): Event => ({
description: null,
eventDate: new Date(),
eventType: 'tournament',
tournamentType: 'individual',
format: 'round_robin',
status: 'planned',
maxParticipants: null,
@@ -63,6 +64,12 @@ const createMockTournament = (id: number, ownerId: string | null): Event => ({
allowTies: false,
createdAt: new Date(),
updatedAt: new Date(),
teamDurability: 'permanent',
partnerRotation: 'none',
allowByes: true,
teamConfiguration: null,
maxRosterChanges: null,
requireAdminVerify: false,
});
describe('Tournament Permissions', () => {
@@ -185,9 +192,9 @@ describe('Tournament Permissions', () => {
);
const mockTournaments = [
createMockTournament(1, 'user-1'),
createMockTournament(2, 'user-2'),
createMockTournament(3, 'user-3'),
{ ...createMockTournament(1, 'user-1'), participants: [] },
{ ...createMockTournament(2, 'user-2'), participants: [] },
{ ...createMockTournament(3, 'user-3'), participants: [] },
];
eventFindManyMock.mockImplementation(async () => mockTournaments);
@@ -210,8 +217,8 @@ describe('Tournament Permissions', () => {
);
const mockTournaments = [
createMockTournament(1, 'tour-admin-1'),
createMockTournament(2, 'tour-admin-1'),
{ ...createMockTournament(1, 'tour-admin-1'), participants: [] },
{ ...createMockTournament(2, 'tour-admin-1'), participants: [] },
];
eventFindManyMock.mockImplementation(async () => mockTournaments);
@@ -237,8 +244,8 @@ describe('Tournament Permissions', () => {
);
const mockTournaments = [
createMockTournament(1, 'user-1'),
createMockTournament(2, 'user-2'),
{ ...createMockTournament(1, 'user-1'), participants: [] },
{ ...createMockTournament(2, 'user-2'), participants: [] },
];
eventFindManyMock.mockImplementation(async () => mockTournaments);
+23 -28
View File
@@ -7,29 +7,15 @@ import { describe, it, expect, mock, beforeEach,} from 'bun:test';
import { prisma } from '@/lib/prisma';
// Create mock functions at module level
const eventFindUniqueMock = mock(() => {});
const eventUpdateMock = mock(() => {});
const canManageTournamentMock = mock(() => {});
const canDeleteTournamentMock = mock(() => {});
// Store default implementations
const defaultCanManageTournament = canManageTournamentMock.mockResolvedValue({ allowed: true });
const defaultCanDeleteTournament = canDeleteTournamentMock.mockResolvedValue({ allowed: true });
// Mock the prisma client
mock.module('@/lib/prisma', () => ({
prisma: {
event: {
findUnique: eventFindUniqueMock,
update: eventUpdateMock,
},
},
}));
const eventFindUniqueMock = mock(async () => ({}));
const eventUpdateMock = mock(async () => ({}));
const canManageTournamentMock = mock(async () => ({ allowed: true }));
const canDeleteTournamentMock = mock(async () => ({ allowed: true }));
// Mock the permissions module
mock.module('@/lib/permissions', () => ({
canManageTournament: defaultCanManageTournament,
canDeleteTournament: defaultCanDeleteTournament,
canManageTournament: canManageTournamentMock,
canDeleteTournament: canDeleteTournamentMock,
}));
// Import the route handler after mocking
@@ -151,10 +137,15 @@ describe('Tournament Update API', () => {
expect(response.status).toBe(200);
// When allowTies is not provided, it should NOT be in the update data
// (it will keep its existing value in the database)
const updateCall = eventUpdateMock.mock.calls[0][0];
expect(updateCall.data.allowTies).toBeUndefined();
expect(updateCall.data.name).toBe('Test Tournament');
expect(updateCall.data.targetScore).toBe(5);
expect(eventUpdateMock.mock.calls.length).toBeGreaterThan(0);
const updateCallArgs = (eventUpdateMock.mock.calls as any[][])[0];
expect(updateCallArgs).toBeDefined();
if (updateCallArgs && updateCallArgs[0]) {
const updateData = updateCallArgs[0];
expect((updateData as any).data.allowTies).toBeUndefined();
expect((updateData as any).data.name).toBe('Test Tournament');
expect((updateData as any).data.targetScore).toBe(5);
}
});
it('should preserve allowTies value when updating other fields', async () => {
@@ -205,9 +196,13 @@ describe('Tournament Update API', () => {
const response = await PUT(request, { params });
expect(response.status).toBe(200);
const updateCall = eventUpdateMock.mock.calls[0][0];
expect(updateCall.data.allowTies).toBe(true);
expect(updateCall.data.name).toBe('Updated Tournament Name');
expect(updateCall.data.targetScore).toBe(10);
const updateCallArgs = (eventUpdateMock.mock.calls as any[][])[0];
expect(updateCallArgs).toBeDefined();
if (updateCallArgs && updateCallArgs[0]) {
const updateData = updateCallArgs[0];
expect((updateData as any).data.allowTies).toBe(true);
expect((updateData as any).data.name).toBe('Updated Tournament Name');
expect((updateData as any).data.targetScore).toBe(10);
}
});
});
+8 -8
View File
@@ -11,10 +11,10 @@ import { prisma } from '@/lib/prisma';
import type { User, Player } from '@prisma/client';
// Create mock functions at module level
const getSessionMock = mock(() => {});
const userFindUniqueMock = mock(() => {});
const userUpdateMock = mock(() => {});
const playerFindUniqueMock = mock(() => {});
const getSessionMock = mock(async (): Promise<any> => null);
const userFindUniqueMock = mock(async (): Promise<any> => null);
const userUpdateMock = mock(async (): Promise<any> => ({}));
const playerFindUniqueMock = mock(async (): Promise<any> => null);
// Mock the getSession and prisma functions
mock.module('@/lib/auth-simple', () => ({
@@ -63,10 +63,10 @@ const createMockPlayer = (id: number, name: string): Player => ({
describe('User Management', () => {
beforeEach(() => {
// Reset mock implementations to default (no-op) before each test
getSessionMock.mockImplementation(() => undefined);
userFindUniqueMock.mockImplementation(() => undefined);
userUpdateMock.mockImplementation(() => undefined);
playerFindUniqueMock.mockImplementation(() => undefined);
getSessionMock.mockImplementation(async () => undefined);
userFindUniqueMock.mockImplementation(async () => undefined);
userUpdateMock.mockImplementation(async () => undefined);
playerFindUniqueMock.mockImplementation(async () => undefined);
});
describe('User Name Editing', () => {