test: fix Navigation unit tests to handle updated session handling
This commit is contained in:
@@ -29,9 +29,15 @@ import { useSession } from '@/components/SessionProvider'
|
|||||||
describe('Epic 1: Navigation Component', () => {
|
describe('Epic 1: Navigation Component', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
vi.mocked(global.fetch).mockResolvedValue({
|
vi.mocked(global.fetch).mockImplementation(async (url) => {
|
||||||
json: () => Promise.resolve({ role: 'player' }),
|
if (url?.toString().includes('/api/users/')) {
|
||||||
} as Response)
|
return new Response(JSON.stringify({ role: 'player' }), {
|
||||||
|
status: 200,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return new Response(JSON.stringify({}), { status: 200 })
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -116,15 +122,21 @@ describe('Epic 1: Navigation Component', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Mock fetch to return club_admin role
|
// Mock fetch to return club_admin role
|
||||||
vi.mocked(global.fetch).mockResolvedValue({
|
vi.mocked(global.fetch).mockImplementation(async (url) => {
|
||||||
json: () => Promise.resolve({ role: 'club_admin' }),
|
if (url?.toString().includes('/api/users/admin-123/role')) {
|
||||||
} as Response)
|
return new Response(JSON.stringify({ role: 'club_admin' }), {
|
||||||
|
status: 200,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return new Response(JSON.stringify({}), { status: 200 })
|
||||||
|
})
|
||||||
|
|
||||||
render(<Navigation />)
|
render(<Navigation />)
|
||||||
|
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(screen.getByText('Admin')).toBeInTheDocument()
|
expect(screen.getByText('Admin')).toBeInTheDocument()
|
||||||
})
|
}, { timeout: 3000 })
|
||||||
expect(screen.getByText('Tournaments')).toBeInTheDocument()
|
expect(screen.getByText('Tournaments')).toBeInTheDocument()
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -143,10 +155,17 @@ describe('Epic 1: Navigation Component', () => {
|
|||||||
refreshSession: vi.fn(),
|
refreshSession: vi.fn(),
|
||||||
})
|
})
|
||||||
|
|
||||||
// Mock fetch to return player role
|
// Mock fetch to return player role (already set in beforeEach, but explicit here)
|
||||||
vi.mocked(global.fetch).mockResolvedValue({
|
vi.mocked(global.fetch).mockImplementation(async (url) => {
|
||||||
|
if (url?.toString().includes('/api/users/')) {
|
||||||
|
return {
|
||||||
json: () => Promise.resolve({ role: 'player' }),
|
json: () => Promise.resolve({ role: 'player' }),
|
||||||
} as Response)
|
} as Response
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
json: () => Promise.resolve({}),
|
||||||
|
} as Response
|
||||||
|
})
|
||||||
|
|
||||||
render(<Navigation />)
|
render(<Navigation />)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user