This commit is contained in:
2025-12-11 00:57:32 +01:00
parent 1303a14493
commit 6a427eab57
112 changed files with 6148 additions and 2272 deletions

View File

@@ -1,9 +1,14 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect, vi } from 'vitest';
import React from 'react';
import { render, screen } from '@testing-library/react';
vi.mock('next/navigation', () => ({
usePathname: () => '/',
useRouter: () => ({
push: () => {},
replace: () => {},
prefetch: () => {},
}),
}));
vi.mock('next/link', () => {
@@ -15,27 +20,86 @@ vi.mock('next/link', () => {
return { default: ActualLink };
});
vi.mock('../../../apps/website/components/profile/UserPill', () => {
return {
__esModule: true,
default: function MockUserPill() {
return (
<div>
<a href="/auth/login">Sign In</a>
<a href="/auth/signup">Get Started</a>
<button type="button">Logout</button>
</div>
);
},
};
});
vi.mock('../../../apps/website/lib/auth/AuthContext', () => {
const React = require('react');
const AuthContext = React.createContext({
session: null,
loading: false,
login: () => {},
logout: async () => {},
refreshSession: async () => {},
});
const AuthProvider = ({ value, children }: { value: any; children: React.ReactNode }) => (
<AuthContext.Provider value={value}>{children}</AuthContext.Provider>
);
const useAuth = () => React.useContext(AuthContext);
return {
__esModule: true,
AuthProvider,
useAuth,
};
});
import { AuthProvider } from '../../../apps/website/lib/auth/AuthContext';
import { AlphaNav } from '../../../apps/website/components/alpha/AlphaNav';
describe('AlphaNav', () => {
it('hides Dashboard link and shows login when unauthenticated', () => {
render(<AlphaNav isAuthenticated={false} />);
it('hides Dashboard link and uses Home when unauthenticated', () => {
render(
<AuthProvider
value={{
session: null,
loading: false,
login: () => {},
logout: async () => {},
refreshSession: async () => {},
}}
>
<AlphaNav />
</AuthProvider>,
);
const dashboardLinks = screen.queryAllByText('Dashboard');
expect(dashboardLinks.length).toBe(0);
const homeLink = screen.getByText('Home');
expect(homeLink).toBeInTheDocument();
const login = screen.getByText('Authenticate with iRacing');
expect(login).toBeInTheDocument();
expect((login as HTMLAnchorElement).getAttribute('href')).toContain(
'/auth/iracing/start?returnTo=/dashboard',
);
});
it('shows Dashboard link, hides Home, and logout control when authenticated', () => {
render(<AlphaNav isAuthenticated />);
it('shows Dashboard link and hides Home when authenticated', () => {
render(
<AuthProvider
value={{
session: {
user: { id: 'user-1' },
},
loading: false,
login: () => {},
logout: async () => {},
refreshSession: async () => {},
}}
>
<AlphaNav />
</AuthProvider>,
);
const dashboard = screen.getByText('Dashboard');
expect(dashboard).toBeInTheDocument();
@@ -43,11 +107,5 @@ describe('AlphaNav', () => {
const homeLink = screen.queryByText('Home');
expect(homeLink).toBeNull();
const login = screen.queryByText('Authenticate with iRacing');
expect(login).toBeNull();
const logout = screen.getByText('Logout');
expect(logout).toBeInTheDocument();
});
});