feat: migrate from npm to Bun
- Install Bun via mise - Update package.json scripts to use Bun - Migrate unit tests from Vitest to Bun test runner - Migrate component tests from Vitest to Bun test runner - Configure Bun with DOM environment for React Testing Library - Keep Playwright for E2E tests (as planned) - 93/100 tests passing (7 flaky tests when running all together, pass individually)
This commit is contained in:
@@ -5,31 +5,31 @@
|
||||
* Tests the navigation component for proper display based on session state
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { describe, it, expect, mock, beforeEach, afterEach } from 'bun:test'
|
||||
import { render, screen, waitFor } from '@testing-library/react'
|
||||
import Navigation from '@/components/Navigation'
|
||||
|
||||
// Mock the SessionProvider
|
||||
vi.mock('@/components/SessionProvider', () => ({
|
||||
useSession: vi.fn(),
|
||||
mock.module('@/components/SessionProvider', () => ({
|
||||
useSession: mock(() => {}),
|
||||
}))
|
||||
|
||||
// Mock the auth-client
|
||||
vi.mock('@/lib/auth-client', () => ({
|
||||
mock.module('@/lib/auth-client', () => ({
|
||||
authClient: {
|
||||
signOut: vi.fn(),
|
||||
signOut: mock(() => {}),
|
||||
},
|
||||
}))
|
||||
|
||||
// Mock fetch for role API call
|
||||
global.fetch = vi.fn()
|
||||
global.fetch = mock(() => {})
|
||||
|
||||
import { useSession } from '@/components/SessionProvider'
|
||||
|
||||
describe('Epic 1: Navigation Component', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.mocked(global.fetch).mockImplementation(async (url) => {
|
||||
mock.clearAllMocks();
|
||||
(global.fetch).mockImplementation(async (url) => {
|
||||
if (url?.toString().includes('/api/users/')) {
|
||||
return new Response(JSON.stringify({ role: 'player' }), {
|
||||
status: 200,
|
||||
@@ -41,14 +41,14 @@ describe('Epic 1: Navigation Component', () => {
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
mock.clearAllMocks();
|
||||
})
|
||||
|
||||
it('renders the logo and basic links when not logged in', () => {
|
||||
vi.mocked(useSession).mockReturnValue({
|
||||
(useSession).mockReturnValue({
|
||||
session: null,
|
||||
loading: false,
|
||||
refreshSession: vi.fn(),
|
||||
refreshSession: mock(() => {}),
|
||||
})
|
||||
|
||||
render(<Navigation />)
|
||||
@@ -60,7 +60,7 @@ describe('Epic 1: Navigation Component', () => {
|
||||
})
|
||||
|
||||
it('shows user menu when logged in', async () => {
|
||||
vi.mocked(useSession).mockReturnValue({
|
||||
(useSession).mockReturnValue({
|
||||
session: {
|
||||
user: {
|
||||
id: 'user-123',
|
||||
@@ -71,7 +71,7 @@ describe('Epic 1: Navigation Component', () => {
|
||||
session: { token: 'abc123' },
|
||||
},
|
||||
loading: false,
|
||||
refreshSession: vi.fn(),
|
||||
refreshSession: mock(() => {}),
|
||||
})
|
||||
|
||||
render(<Navigation />)
|
||||
@@ -85,7 +85,7 @@ describe('Epic 1: Navigation Component', () => {
|
||||
})
|
||||
|
||||
it('shows Tournaments link when logged in', async () => {
|
||||
vi.mocked(useSession).mockReturnValue({
|
||||
(useSession).mockReturnValue({
|
||||
session: {
|
||||
user: {
|
||||
id: 'user-123',
|
||||
@@ -96,7 +96,7 @@ describe('Epic 1: Navigation Component', () => {
|
||||
session: { token: 'abc123' },
|
||||
},
|
||||
loading: false,
|
||||
refreshSession: vi.fn(),
|
||||
refreshSession: mock(() => {}),
|
||||
})
|
||||
|
||||
render(<Navigation />)
|
||||
@@ -107,7 +107,7 @@ describe('Epic 1: Navigation Component', () => {
|
||||
})
|
||||
|
||||
it('shows admin link for club_admin role', async () => {
|
||||
vi.mocked(useSession).mockReturnValue({
|
||||
(useSession).mockReturnValue({
|
||||
session: {
|
||||
user: {
|
||||
id: 'admin-123',
|
||||
@@ -118,11 +118,11 @@ describe('Epic 1: Navigation Component', () => {
|
||||
session: { token: 'abc123' },
|
||||
},
|
||||
loading: false,
|
||||
refreshSession: vi.fn(),
|
||||
})
|
||||
refreshSession: mock(() => {}),
|
||||
});
|
||||
|
||||
// Mock fetch to return club_admin role
|
||||
vi.mocked(global.fetch).mockImplementation(async (url) => {
|
||||
(global.fetch).mockImplementation(async (url) => {
|
||||
if (url?.toString().includes('/api/users/admin-123/role')) {
|
||||
return new Response(JSON.stringify({ role: 'club_admin' }), {
|
||||
status: 200,
|
||||
@@ -141,7 +141,7 @@ describe('Epic 1: Navigation Component', () => {
|
||||
})
|
||||
|
||||
it('hides admin link for non-admin users', async () => {
|
||||
vi.mocked(useSession).mockReturnValue({
|
||||
(useSession).mockReturnValue({
|
||||
session: {
|
||||
user: {
|
||||
id: 'player-123',
|
||||
@@ -152,11 +152,11 @@ describe('Epic 1: Navigation Component', () => {
|
||||
session: { token: 'abc123' },
|
||||
},
|
||||
loading: false,
|
||||
refreshSession: vi.fn(),
|
||||
})
|
||||
refreshSession: mock(() => {}),
|
||||
});
|
||||
|
||||
// Mock fetch to return player role (already set in beforeEach, but explicit here)
|
||||
vi.mocked(global.fetch).mockImplementation(async (url) => {
|
||||
(global.fetch).mockImplementation(async (url) => {
|
||||
if (url?.toString().includes('/api/users/')) {
|
||||
return {
|
||||
json: () => Promise.resolve({ role: 'player' }),
|
||||
@@ -176,10 +176,10 @@ describe('Epic 1: Navigation Component', () => {
|
||||
})
|
||||
|
||||
it('shows loading state', () => {
|
||||
vi.mocked(useSession).mockReturnValue({
|
||||
(useSession).mockReturnValue({
|
||||
session: null,
|
||||
loading: true,
|
||||
refreshSession: vi.fn(),
|
||||
refreshSession: mock(() => {}),
|
||||
})
|
||||
|
||||
render(<Navigation />)
|
||||
|
||||
Reference in New Issue
Block a user