102 lines
2.7 KiB
TypeScript
102 lines
2.7 KiB
TypeScript
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', () => {
|
|
const ActualLink = ({ href, children, ...rest }: any) => (
|
|
<a href={href} {...rest}>
|
|
{children}
|
|
</a>
|
|
);
|
|
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 = ({ initialSession, children }: { initialSession?: any; children: React.ReactNode }) => (
|
|
<AuthContext.Provider value={{ session: initialSession, loading: false, login: () => {}, logout: async () => {}, refreshSession: async () => {} }}>{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 uses Home when unauthenticated', () => {
|
|
render(
|
|
<AuthProvider
|
|
initialSession={null}
|
|
>
|
|
<AlphaNav />
|
|
</AuthProvider>,
|
|
);
|
|
|
|
const dashboardLinks = screen.queryAllByText('Dashboard');
|
|
expect(dashboardLinks.length).toBe(0);
|
|
|
|
const homeLink = screen.getByText('Home');
|
|
expect(homeLink).toBeInTheDocument();
|
|
});
|
|
|
|
it('shows Dashboard link and hides Home when authenticated', () => {
|
|
render(
|
|
<AuthProvider
|
|
initialSession={{
|
|
user: { id: 'user-1', displayName: 'Test User' },
|
|
issuedAt: Date.now(),
|
|
expiresAt: Date.now() + 3600000,
|
|
token: 'fake-token',
|
|
}}
|
|
>
|
|
<AlphaNav />
|
|
</AuthProvider>,
|
|
);
|
|
|
|
const dashboard = screen.getByText('Dashboard');
|
|
expect(dashboard).toBeInTheDocument();
|
|
expect((dashboard as HTMLAnchorElement).getAttribute('href')).toBe('/dashboard');
|
|
|
|
const homeLink = screen.queryByText('Home');
|
|
expect(homeLink).toBeNull();
|
|
});
|
|
}); |