test: improve mock error handling and add test cleanup
This commit is contained in:
@@ -47,7 +47,9 @@ describe('getSession', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('returns null when an error occurs', async () => {
|
it('returns null when an error occurs', async () => {
|
||||||
mockFetch.mockRejectedValue(new Error('Network error'))
|
mockFetch.mockImplementation(async () => {
|
||||||
|
throw new Error('Network error')
|
||||||
|
})
|
||||||
|
|
||||||
const result = await getSession()
|
const result = await getSession()
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Unit Tests: Permissions
|
* Unit Tests: Permissions
|
||||||
*
|
*
|
||||||
* Tests the permission system for tournament management
|
* Tests the permission system for tournament management
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ describe('Permissions', () => {
|
|||||||
user: { id: '1', email: 'test@example.com' },
|
user: { id: '1', email: 'test@example.com' },
|
||||||
session: { token: 'test', expiresAt: new Date() }
|
session: { token: 'test', expiresAt: new Date() }
|
||||||
}));
|
}));
|
||||||
userFindUniqueMock.mockImplementation(async () =>
|
userFindUniqueMock.mockImplementation(async () =>
|
||||||
createMockUser('1', 'test@example.com', 'club_admin')
|
createMockUser('1', 'test@example.com', 'club_admin')
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -70,7 +70,7 @@ describe('Permissions', () => {
|
|||||||
user: { id: '1', email: 'test@example.com' },
|
user: { id: '1', email: 'test@example.com' },
|
||||||
session: { token: 'test', expiresAt: new Date() }
|
session: { token: 'test', expiresAt: new Date() }
|
||||||
}));
|
}));
|
||||||
userFindUniqueMock.mockImplementation(async () =>
|
userFindUniqueMock.mockImplementation(async () =>
|
||||||
createMockUser('1', 'test@example.com', 'player')
|
createMockUser('1', 'test@example.com', 'player')
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -93,7 +93,7 @@ describe('Permissions', () => {
|
|||||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||||
session: { token: 'test', expiresAt: new Date() }
|
session: { token: 'test', expiresAt: new Date() }
|
||||||
}));
|
}));
|
||||||
userFindUniqueMock.mockImplementation(async () =>
|
userFindUniqueMock.mockImplementation(async () =>
|
||||||
createMockUser('admin-1', 'admin@example.com', 'club_admin')
|
createMockUser('admin-1', 'admin@example.com', 'club_admin')
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ describe('Permissions', () => {
|
|||||||
user: { id: 'player-1', email: 'player@example.com' },
|
user: { id: 'player-1', email: 'player@example.com' },
|
||||||
session: { token: 'test', expiresAt: new Date() }
|
session: { token: 'test', expiresAt: new Date() }
|
||||||
}));
|
}));
|
||||||
userFindUniqueMock.mockImplementation(async () =>
|
userFindUniqueMock.mockImplementation(async () =>
|
||||||
createMockUser('player-1', 'player@example.com', 'player')
|
createMockUser('player-1', 'player@example.com', 'player')
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -122,7 +122,7 @@ describe('Permissions', () => {
|
|||||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||||
session: { token: 'test', expiresAt: new Date() }
|
session: { token: 'test', expiresAt: new Date() }
|
||||||
}));
|
}));
|
||||||
userFindUniqueMock.mockImplementation(async () =>
|
userFindUniqueMock.mockImplementation(async () =>
|
||||||
createMockUser('admin-1', 'admin@example.com', 'tournament_admin')
|
createMockUser('admin-1', 'admin@example.com', 'tournament_admin')
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -135,7 +135,7 @@ describe('Permissions', () => {
|
|||||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||||
session: { token: 'test', expiresAt: new Date() }
|
session: { token: 'test', expiresAt: new Date() }
|
||||||
}));
|
}));
|
||||||
userFindUniqueMock.mockImplementation(async () =>
|
userFindUniqueMock.mockImplementation(async () =>
|
||||||
createMockUser('admin-1', 'admin@example.com', 'club_admin')
|
createMockUser('admin-1', 'admin@example.com', 'club_admin')
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -148,7 +148,7 @@ describe('Permissions', () => {
|
|||||||
user: { id: 'player-1', email: 'player@example.com' },
|
user: { id: 'player-1', email: 'player@example.com' },
|
||||||
session: { token: 'test', expiresAt: new Date() }
|
session: { token: 'test', expiresAt: new Date() }
|
||||||
}));
|
}));
|
||||||
userFindUniqueMock.mockImplementation(async () =>
|
userFindUniqueMock.mockImplementation(async () =>
|
||||||
createMockUser('player-1', 'player@example.com', 'player')
|
createMockUser('player-1', 'player@example.com', 'player')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
* Tests the allowTies field is properly saved when updating tournaments
|
* Tests the allowTies field is properly saved when updating tournaments
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, mock, beforeEach,} from 'bun:test';
|
import { describe, it, expect, mock, beforeEach, afterAll } from 'bun:test';
|
||||||
|
|
||||||
// Create mock functions at module level
|
// Create mock functions at module level
|
||||||
const eventFindUniqueMock = mock(async () => ({}));
|
const eventFindUniqueMock = mock(async () => ({}));
|
||||||
@@ -27,6 +27,11 @@ mock.module('@/lib/permissions', () => ({
|
|||||||
canDeleteTournament: canDeleteTournamentMock,
|
canDeleteTournament: canDeleteTournamentMock,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Cleanup after all tests in this file
|
||||||
|
afterAll(() => {
|
||||||
|
mock.restore('module');
|
||||||
|
});
|
||||||
|
|
||||||
// Import the route handler after mocking
|
// Import the route handler after mocking
|
||||||
import { PUT } from '@/app/api/tournaments/[id]/route';
|
import { PUT } from '@/app/api/tournaments/[id]/route';
|
||||||
import { prisma } from '@/lib/prisma';
|
import { prisma } from '@/lib/prisma';
|
||||||
|
|||||||
Reference in New Issue
Block a user