test: update mocks for Prisma v7 stricter type checking

This commit is contained in:
2026-03-31 00:00:50 -07:00
parent 9649c62c28
commit 77de648200
4 changed files with 59 additions and 11 deletions
@@ -71,8 +71,12 @@ test.describe('CSV Upload Player Deduplication', () => {
try { try {
// Upload the CSV // Upload the CSV
const csvFileContent = fs.readFileSync(csvPath, 'utf-8');
const blob = new Blob([csvFileContent], { type: 'text/csv' });
const file = new File([blob], 'deduplication-test.csv', { type: 'text/csv' });
const formData = new FormData(); const formData = new FormData();
formData.append('csvFile', fs.createReadStream(csvPath), 'deduplication-test.csv'); formData.append('csvFile', file);
formData.append('eventId', testTournamentId.toString()); formData.append('eventId', testTournamentId.toString());
const response = await request.post('http://localhost:3000/api/matches/upload', { const response = await request.post('http://localhost:3000/api/matches/upload', {
@@ -97,11 +101,11 @@ test.describe('CSV Upload Player Deduplication', () => {
expect(dedupePlayers.length).toBe(2); expect(dedupePlayers.length).toBe(2);
// Verify the players have the correct names (original case preserved) // Verify the players have the correct names (original case preserved)
const playerNames = dedupePlayers.map(p => p.name).sort(); const playerNames = dedupePlayers.map((p: { name: string }) => p.name).sort();
expect(playerNames).toEqual(['Dedupe Player', 'Dedupe Player2']); expect(playerNames).toEqual(['Dedupe Player', 'Dedupe Player2']);
// Verify each player has games played // Verify each player has games played
dedupePlayers.forEach(player => { dedupePlayers.forEach((player: { gamesPlayed: number }) => {
expect(player.gamesPlayed).toBeGreaterThan(0); expect(player.gamesPlayed).toBeGreaterThan(0);
}); });
} finally { } finally {
@@ -121,8 +125,12 @@ test.describe('CSV Upload Player Deduplication', () => {
fs.writeFileSync(csvPath, csvContent); fs.writeFileSync(csvPath, csvContent);
try { try {
const csvFileContent = fs.readFileSync(csvPath, 'utf-8');
const blob = new Blob([csvFileContent], { type: 'text/csv' });
const file = new File([blob], 'whitespace-test.csv', { type: 'text/csv' });
const formData = new FormData(); const formData = new FormData();
formData.append('csvFile', fs.createReadStream(csvPath), 'whitespace-test.csv'); formData.append('csvFile', file);
formData.append('eventId', testTournamentId.toString()); formData.append('eventId', testTournamentId.toString());
const response = await request.post('http://localhost:3000/api/matches/upload', { const response = await request.post('http://localhost:3000/api/matches/upload', {
@@ -141,7 +149,7 @@ test.describe('CSV Upload Player Deduplication', () => {
}); });
// Verify no players have leading/trailing whitespace // Verify no players have leading/trailing whitespace
whitespacePlayers.forEach(player => { whitespacePlayers.forEach((player: { name: string, normalizedName: string }) => {
expect(player.name).toBe(player.name.trim()); expect(player.name).toBe(player.name.trim());
expect(player.normalizedName).toBe(player.name.trim().toLowerCase()); expect(player.normalizedName).toBe(player.name.trim().toLowerCase());
}); });
@@ -174,8 +182,12 @@ test.describe('CSV Upload Player Deduplication', () => {
fs.writeFileSync(csvPath, csvContent); fs.writeFileSync(csvPath, csvContent);
try { try {
const csvFileContent = fs.readFileSync(csvPath, 'utf-8');
const blob = new Blob([csvFileContent], { type: 'text/csv' });
const file = new File([blob], 'aggregate-test.csv', { type: 'text/csv' });
const formData = new FormData(); const formData = new FormData();
formData.append('csvFile', fs.createReadStream(csvPath), 'aggregate-test.csv'); formData.append('csvFile', file);
formData.append('eventId', testTournamentId.toString()); formData.append('eventId', testTournamentId.toString());
const response = await request.post('http://localhost:3000/api/matches/upload', { const response = await request.post('http://localhost:3000/api/matches/upload', {
@@ -35,6 +35,8 @@ async function findOrCreatePlayer(name: string) {
normalizedName, normalizedName,
rating: 1000, rating: 1000,
currentElo: 1000, currentElo: 1000,
createdAt: new Date(),
updatedAt: new Date(),
}, },
}); });
} }
@@ -57,6 +59,9 @@ describe('Player Deduplication', () => {
gamesPlayed: 5, gamesPlayed: 5,
wins: 4, wins: 4,
losses: 1, losses: 1,
createdAt: new Date(),
updatedAt: new Date(),
rating: 0,
}; };
vi.mocked(prisma.player.findFirst).mockResolvedValue(mockPlayer); vi.mocked(prisma.player.findFirst).mockResolvedValue(mockPlayer);
@@ -79,6 +84,9 @@ describe('Player Deduplication', () => {
gamesPlayed: 5, gamesPlayed: 5,
wins: 4, wins: 4,
losses: 1, losses: 1,
createdAt: new Date(),
updatedAt: new Date(),
rating: 0,
}; };
vi.mocked(prisma.player.findFirst).mockResolvedValue(mockPlayer); vi.mocked(prisma.player.findFirst).mockResolvedValue(mockPlayer);
@@ -101,6 +109,9 @@ describe('Player Deduplication', () => {
gamesPlayed: 5, gamesPlayed: 5,
wins: 4, wins: 4,
losses: 1, losses: 1,
createdAt: new Date(),
updatedAt: new Date(),
rating: 0,
}; };
vi.mocked(prisma.player.findFirst).mockResolvedValue(mockPlayer); vi.mocked(prisma.player.findFirst).mockResolvedValue(mockPlayer);
@@ -126,6 +137,8 @@ describe('Player Deduplication', () => {
gamesPlayed: 0, gamesPlayed: 0,
wins: 0, wins: 0,
losses: 0, losses: 0,
createdAt: new Date(),
updatedAt: new Date(),
}; };
vi.mocked(prisma.player.create).mockResolvedValue(newPlayer); vi.mocked(prisma.player.create).mockResolvedValue(newPlayer);
@@ -142,6 +155,8 @@ describe('Player Deduplication', () => {
normalizedName: 'newplayer', normalizedName: 'newplayer',
rating: 1000, rating: 1000,
currentElo: 1000, currentElo: 1000,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
}, },
}); });
}); });
@@ -158,6 +173,8 @@ describe('Player Deduplication', () => {
gamesPlayed: 0, gamesPlayed: 0,
wins: 0, wins: 0,
losses: 0, losses: 0,
createdAt: new Date(),
updatedAt: new Date(),
}; };
vi.mocked(prisma.player.create).mockResolvedValue(newPlayer); vi.mocked(prisma.player.create).mockResolvedValue(newPlayer);
@@ -171,6 +188,8 @@ describe('Player Deduplication', () => {
normalizedName: 'test-player_123', normalizedName: 'test-player_123',
rating: 1000, rating: 1000,
currentElo: 1000, currentElo: 1000,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
}, },
}); });
}); });
@@ -187,6 +206,8 @@ describe('Player Deduplication', () => {
gamesPlayed: 0, gamesPlayed: 0,
wins: 0, wins: 0,
losses: 0, losses: 0,
createdAt: new Date(),
updatedAt: new Date(),
}; };
vi.mocked(prisma.player.create).mockResolvedValue(newPlayer); vi.mocked(prisma.player.create).mockResolvedValue(newPlayer);
@@ -200,6 +221,8 @@ describe('Player Deduplication', () => {
normalizedName: 'dave b', normalizedName: 'dave b',
rating: 1000, rating: 1000,
currentElo: 1000, currentElo: 1000,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
}, },
}); });
}); });
@@ -214,6 +237,9 @@ describe('Player Deduplication', () => {
gamesPlayed: 5, gamesPlayed: 5,
wins: 4, wins: 4,
losses: 1, losses: 1,
createdAt: new Date(),
updatedAt: new Date(),
rating: 0,
}; };
vi.mocked(prisma.player.findFirst).mockResolvedValue(mockPlayer); vi.mocked(prisma.player.findFirst).mockResolvedValue(mockPlayer);
@@ -240,6 +266,8 @@ describe('Player Deduplication', () => {
gamesPlayed: 0, gamesPlayed: 0,
wins: 0, wins: 0,
losses: 0, losses: 0,
createdAt: new Date(),
updatedAt: new Date(),
}; };
vi.mocked(prisma.player.create).mockResolvedValue(newPlayer); vi.mocked(prisma.player.create).mockResolvedValue(newPlayer);
@@ -253,6 +281,8 @@ describe('Player Deduplication', () => {
normalizedName: '', normalizedName: '',
rating: 1000, rating: 1000,
currentElo: 1000, currentElo: 1000,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
}, },
}); });
}); });
@@ -285,6 +315,9 @@ describe('Player Deduplication', () => {
gamesPlayed: 5, gamesPlayed: 5,
wins: 4, wins: 4,
losses: 1, losses: 1,
createdAt: new Date(),
updatedAt: new Date(),
rating: 0,
}; };
vi.mocked(prisma.player.findFirst) vi.mocked(prisma.player.findFirst)
+5 -1
View File
@@ -7,7 +7,7 @@
* - Partnership performance * - Partnership performance
*/ */
import { describe, test, expect, vi } from 'vitest'; import { describe, test, expect, vi, beforeEach } from 'vitest';
import { prisma } from '@/lib/prisma'; import { prisma } from '@/lib/prisma';
import type { Player, Event, Match, PartnershipStat } from '@prisma/client'; import type { Player, Event, Match, PartnershipStat } from '@prisma/client';
@@ -56,6 +56,9 @@ const createMockTournament = (id: number, name: string): Event => ({
ownerId: null, ownerId: null,
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
event_id: null,
targetScore: null,
allowTies: false,
}); });
// Helper to create mock match // Helper to create mock match
@@ -82,6 +85,7 @@ const createMockMatch = (
createdById: null, createdById: null,
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
isCasual: false,
}); });
// Helper to create mock partnership stat // Helper to create mock partnership stat
+3 -4
View File
@@ -4,7 +4,7 @@
* Tests for user name editing and profile management * Tests for user name editing and profile management
*/ */
import { describe, test, expect, vi } from 'vitest'; import { describe, test, expect, vi, beforeEach } from 'vitest';
import { hasRole } from '@/lib/permissions'; import { hasRole } from '@/lib/permissions';
import { getSession } from '@/lib/auth-simple'; import { getSession } from '@/lib/auth-simple';
import { prisma } from '@/lib/prisma'; import { prisma } from '@/lib/prisma';
@@ -41,7 +41,7 @@ const createMockUser = (id: string, email: string, role: string, name?: string):
}); });
// Helper to create mock player // Helper to create mock player
const createMockPlayer = (id: number, userId: string, name: string): Player => ({ const createMockPlayer = (id: number, name: string): Player => ({
id, id,
name, name,
normalizedName: name.toLowerCase(), normalizedName: name.toLowerCase(),
@@ -52,7 +52,6 @@ const createMockPlayer = (id: number, userId: string, name: string): Player => (
losses: 0, losses: 0,
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
userId,
}); });
describe('User Management', () => { describe('User Management', () => {
@@ -158,7 +157,7 @@ describe('User Management', () => {
describe('Player Name Updates', () => { describe('Player Name Updates', () => {
test('player name should be updated when user name is updated', async () => { test('player name should be updated when user name is updated', async () => {
const mockUser = createMockUser('user-1', 'user@example.com', 'club_admin'); const mockUser = createMockUser('user-1', 'user@example.com', 'club_admin');
const mockPlayer = createMockPlayer(1, 'user-1', 'Old Name'); const mockPlayer = createMockPlayer(1, 'Old Name');
vi.mocked(getSession).mockResolvedValue({ vi.mocked(getSession).mockResolvedValue({
user: { id: 'user-1', email: 'user@example.com' }, user: { id: 'user-1', email: 'user@example.com' },