Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VITE_API_URL=http://localhost:5001/api
VITE_API_URL=http://localhost:5001
2 changes: 0 additions & 2 deletions client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ lerna-debug.log*
node_modules
dist
dist-ssr
src/tests
src/test
*.local
src/assets

Expand Down
21 changes: 21 additions & 0 deletions client/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ import reactRefresh from 'eslint-plugin-react-refresh'

export default [
{ ignores: ['dist'] },
// CommonJS config files (Tailwind)
{
files: ['tailwind.config.js'],
languageOptions: {
ecmaVersion: 2020,
globals: { ...globals.node },
sourceType: 'commonjs',
},
},
// Node ES-module config files (Vite, PostCSS)
{
files: ['vite.config.js', 'postcss.config.js'],
languageOptions: {
ecmaVersion: 2020,
globals: { ...globals.node, ...globals.browser },
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
},
},
{
files: ['**/*.{js,jsx}'],
languageOptions: {
Expand Down
1 change: 0 additions & 1 deletion client/src/components/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { useAuth } from '../contexts/AuthContext';

Expand Down
2 changes: 1 addition & 1 deletion client/src/components/PrivateRoute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function PrivateRoute({ children }) {
}

// If not loading and no user, redirect to login
if (!loading && !currentUser) {
if (!currentUser) {
// Save the attempted url for redirecting after login
return <Navigate to="/login" state={{ from: location }} replace />;
}
Expand Down
5 changes: 2 additions & 3 deletions client/src/components/ProfileForm.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useState, useEffect } from 'react';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import axios from 'axios';
import { userAPI } from '../services/api';

const ProfileForm = ({ initialData }) => {
Expand Down Expand Up @@ -44,7 +43,7 @@ const ProfileForm = ({ initialData }) => {
setSuccess('');

try {
await userAPI.updateProfile(initialData._id, formData);
await userAPI.updateProfile(formData);
setSuccess('Profile updated successfully!');
setTimeout(() => {
navigate(`/profile/${initialData._id}`);
Expand Down
14 changes: 0 additions & 14 deletions client/src/components/ProtectedRoute.jsx

This file was deleted.

79 changes: 79 additions & 0 deletions client/src/components/__tests__/DeveloperCard.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { describe, it, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import DeveloperCard from '../DeveloperCard';

const renderCard = (developer) =>
render(
<MemoryRouter>
<DeveloperCard developer={developer} />
</MemoryRouter>
);

describe('DeveloperCard', () => {
const baseDev = {
_id: 'dev1',
name: 'Jane Doe',
bio: 'Full stack developer',
skills: ['React', 'Node.js'],
};

it('renders developer name', () => {
renderCard(baseDev);
expect(screen.getByText('Jane Doe')).toBeInTheDocument();
});

it('renders developer bio', () => {
renderCard(baseDev);
expect(screen.getByText('Full stack developer')).toBeInTheDocument();
});

it('renders all skills as badges', () => {
renderCard(baseDev);
expect(screen.getByText('React')).toBeInTheDocument();
expect(screen.getByText('Node.js')).toBeInTheDocument();
});

it('links to the developer profile page', () => {
renderCard(baseDev);
const link = screen.getByRole('link');
expect(link).toHaveAttribute('href', '/profile/dev1');
});

it('uses fallback avatar when no avatar prop is given', () => {
renderCard(baseDev);
const img = screen.getByAltText('Jane Doe');
expect(img.src).toContain('ui-avatars.com');
});

it('uses provided avatar when given', () => {
renderCard({ ...baseDev, avatar: 'https://example.com/avatar.png' });
const img = screen.getByAltText('Jane Doe');
expect(img.src).toBe('https://example.com/avatar.png');
});

it('shows "Developer" as default title when no title prop given', () => {
renderCard(baseDev);
expect(screen.getByText('Developer')).toBeInTheDocument();
});

it('shows provided title', () => {
renderCard({ ...baseDev, title: 'Backend Engineer' });
expect(screen.getByText('Backend Engineer')).toBeInTheDocument();
});

it('shows "No bio provided." when bio is empty', () => {
renderCard({ ...baseDev, bio: '' });
expect(screen.getByText('No bio provided.')).toBeInTheDocument();
});

it('renders with no skills gracefully', () => {
renderCard({ ...baseDev, skills: undefined });
expect(screen.getByText('Jane Doe')).toBeInTheDocument();
});

it('renders with empty skills array', () => {
renderCard({ ...baseDev, skills: [] });
expect(screen.getByText('Jane Doe')).toBeInTheDocument();
});
});
108 changes: 108 additions & 0 deletions client/src/components/__tests__/Navbar.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import Navbar from '../Navbar';

const mockNavigate = vi.fn();
vi.mock('react-router-dom', async () => {
const actual = await vi.importActual('react-router-dom');
return { ...actual, useNavigate: () => mockNavigate };
});

let mockUseAuth;
vi.mock('../../contexts/AuthContext', async (importActual) => {
const actual = await importActual();
return { ...actual, useAuth: () => mockUseAuth() };
});

const renderNavbar = () =>
render(
<MemoryRouter>
<Navbar />
</MemoryRouter>
);

describe('Navbar', () => {
beforeEach(() => {
vi.clearAllMocks();
mockUseAuth = () => ({ currentUser: null, logout: vi.fn() });
});

describe('when user is logged out', () => {
it('renders the DevLinkUp brand logo', () => {
renderNavbar();
expect(screen.getByText('DevLinkUp')).toBeInTheDocument();
});

it('shows Login link', () => {
renderNavbar();
expect(screen.getByText('Login')).toBeInTheDocument();
});

it('shows Sign Up link', () => {
renderNavbar();
expect(screen.getByText('Sign Up')).toBeInTheDocument();
});

it('shows Discover link', () => {
renderNavbar();
expect(screen.getByText('Discover')).toBeInTheDocument();
});

it('does not show Dashboard when logged out', () => {
renderNavbar();
expect(screen.queryByText('Dashboard')).not.toBeInTheDocument();
});

it('does not show Logout button when logged out', () => {
renderNavbar();
expect(screen.queryByText('Logout')).not.toBeInTheDocument();
});
});

describe('when user is logged in', () => {
beforeEach(() => {
mockUseAuth = () => ({
currentUser: { _id: 'user1', name: 'Alice', email: 'alice@example.com' },
logout: vi.fn(),
});
});

it('shows the user name as a profile link', () => {
renderNavbar();
expect(screen.getByText('Alice')).toBeInTheDocument();
});

it('shows Logout button', () => {
renderNavbar();
expect(screen.getByText('Logout')).toBeInTheDocument();
});

it('shows Dashboard link', () => {
renderNavbar();
expect(screen.getByText('Dashboard')).toBeInTheDocument();
});

it('does not show Login link when logged in', () => {
renderNavbar();
expect(screen.queryByText('Login')).not.toBeInTheDocument();
});

it('profile link points to current user profile', () => {
renderNavbar();
const profileLink = screen.getByText('Alice').closest('a');
expect(profileLink).toHaveAttribute('href', '/profile/user1');
});

it('calls logout and navigates to /login when Logout clicked', async () => {
const mockLogout = vi.fn();
mockUseAuth = () => ({
currentUser: { _id: 'user1', name: 'Alice' },
logout: mockLogout,
});
renderNavbar();
fireEvent.click(screen.getByText('Logout'));
expect(mockLogout).toHaveBeenCalledTimes(1);
});
});
});
58 changes: 58 additions & 0 deletions client/src/components/__tests__/PrivateRoute.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen } from '@testing-library/react';
import { MemoryRouter, Routes, Route } from 'react-router-dom';
import PrivateRoute from '../PrivateRoute';

let mockUseAuth;
vi.mock('../../contexts/AuthContext', async (importActual) => {
const actual = await importActual();
return { ...actual, useAuth: () => mockUseAuth() };
});

const renderPrivateRoute = (children = <div>Protected Content</div>) =>
render(
<MemoryRouter initialEntries={['/protected']}>
<Routes>
<Route
path="/protected"
element={<PrivateRoute>{children}</PrivateRoute>}
/>
<Route path="/login" element={<div>Login Page</div>} />
</Routes>
</MemoryRouter>
);

describe('PrivateRoute', () => {
beforeEach(() => vi.clearAllMocks());

it('shows loading spinner while auth is loading', () => {
mockUseAuth = () => ({ currentUser: null, loading: true });
renderPrivateRoute();
// The spinner div is present (uses animate-spin class)
const spinner = document.querySelector('.animate-spin');
expect(spinner).not.toBeNull();
});

it('renders children when user is authenticated', () => {
mockUseAuth = () => ({
currentUser: { _id: 'user1', name: 'Alice' },
loading: false,
});
renderPrivateRoute();
expect(screen.getByText('Protected Content')).toBeInTheDocument();
});

it('redirects to /login when user is not authenticated', () => {
mockUseAuth = () => ({ currentUser: null, loading: false });
renderPrivateRoute();
expect(screen.getByText('Login Page')).toBeInTheDocument();
expect(screen.queryByText('Protected Content')).not.toBeInTheDocument();
});

it('preserves location state for post-login redirect', () => {
mockUseAuth = () => ({ currentUser: null, loading: false });
// Just check the redirect happened — location state is handled internally
renderPrivateRoute();
expect(screen.queryByText('Protected Content')).not.toBeInTheDocument();
});
});
Loading