mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-21 06:11:45 +00:00
test: expand frontend test suite to 82% coverage
Adds ~45 new and updated test files covering Admin, Collab, Dashboard, Map, Memories, PDF, Photos, Planner, Settings, Vacay, Weather components, pages, stores, and a WebSocket integration test.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
// FE-COMP-NAVBAR-001 to FE-COMP-NAVBAR-015
|
||||
// FE-COMP-NAVBAR-001 to FE-COMP-NAVBAR-028
|
||||
import { render, screen, waitFor } from '../../../tests/helpers/render';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { server } from '../../../tests/helpers/msw/server';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
import { useAddonStore } from '../../store/addonStore';
|
||||
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
|
||||
import { buildUser, buildSettings } from '../../../tests/helpers/factories';
|
||||
import Navbar from './Navbar';
|
||||
@@ -13,6 +14,7 @@ beforeEach(() => {
|
||||
resetAllStores();
|
||||
server.use(
|
||||
http.get('/api/auth/app-config', () => HttpResponse.json({ version: '2.9.10' })),
|
||||
http.get('/api/addons', () => HttpResponse.json({ addons: [] })),
|
||||
);
|
||||
seedStore(useAuthStore, { user: buildUser({ username: 'testuser', role: 'user' }), isAuthenticated: true });
|
||||
seedStore(useSettingsStore, { settings: buildSettings() });
|
||||
@@ -128,4 +130,178 @@ describe('Navbar', () => {
|
||||
const darkModeEls = screen.getAllByRole('button');
|
||||
expect(darkModeEls.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-016: app version shown in user menu', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Navbar />);
|
||||
await user.click(screen.getByText('testuser'));
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('v2.9.10')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-017: Settings link navigates to /settings', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Navbar />);
|
||||
await user.click(screen.getByText('testuser'));
|
||||
const settingsLink = screen.getByRole('link', { name: /settings/i });
|
||||
expect(settingsLink).toHaveAttribute('href', '/settings');
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-018: Admin link navigates to /admin for admin user', async () => {
|
||||
const user = userEvent.setup();
|
||||
seedStore(useAuthStore, { user: buildUser({ username: 'adminuser', role: 'admin' }), isAuthenticated: true });
|
||||
render(<Navbar />);
|
||||
await user.click(screen.getByText('adminuser'));
|
||||
const adminLink = screen.getByRole('link', { name: /admin/i });
|
||||
expect(adminLink).toHaveAttribute('href', '/admin');
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-019: share button rendered when onShare prop provided', () => {
|
||||
render(<Navbar onShare={vi.fn()} />);
|
||||
const shareBtn = screen.getByRole('button', { name: /share/i });
|
||||
expect(shareBtn).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-020: share button click calls onShare', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onShare = vi.fn();
|
||||
render(<Navbar onShare={onShare} />);
|
||||
const shareBtn = screen.getByRole('button', { name: /share/i });
|
||||
await user.click(shareBtn);
|
||||
expect(onShare).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-021: share button NOT rendered when onShare prop omitted', () => {
|
||||
render(<Navbar />);
|
||||
expect(screen.queryByRole('button', { name: /share/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-022: dark mode toggle shows Moon when light, Sun when dark', () => {
|
||||
seedStore(useSettingsStore, { settings: buildSettings({ dark_mode: false }) });
|
||||
const { unmount } = render(<Navbar />);
|
||||
// Moon icon button should be present (title = 'nav.darkMode' i.e. 'Dark mode')
|
||||
expect(document.querySelector('[title]')).toBeTruthy();
|
||||
unmount();
|
||||
|
||||
seedStore(useSettingsStore, { settings: buildSettings({ dark_mode: 'dark' }) });
|
||||
render(<Navbar />);
|
||||
// Sun icon button should be present when dark mode is on
|
||||
const buttons = screen.getAllByRole('button');
|
||||
expect(buttons.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-023: dark mode toggle calls updateSetting', async () => {
|
||||
const user = userEvent.setup();
|
||||
const updateSetting = vi.fn().mockResolvedValue(undefined);
|
||||
seedStore(useSettingsStore, { settings: buildSettings({ dark_mode: false }), updateSetting });
|
||||
render(<Navbar />);
|
||||
// Find the dark mode toggle button by title attribute
|
||||
const toggleBtn = document.querySelector('button[title]') as HTMLElement;
|
||||
expect(toggleBtn).toBeTruthy();
|
||||
await user.click(toggleBtn);
|
||||
expect(updateSetting).toHaveBeenCalledWith('dark_mode', 'dark');
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-024: global addon nav links appear when addons enabled', () => {
|
||||
server.use(
|
||||
http.get('/api/addons', () => HttpResponse.json({
|
||||
addons: [{ id: 'vacay', name: 'Vacay', icon: 'CalendarDays', type: 'global', enabled: true }],
|
||||
})),
|
||||
);
|
||||
seedStore(useAddonStore, {
|
||||
addons: [{ id: 'vacay', name: 'Vacay', icon: 'CalendarDays', type: 'global', enabled: true }],
|
||||
});
|
||||
render(<Navbar />);
|
||||
expect(screen.getByRole('link', { name: /vacay/i })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-025: global addon links hidden when in trip view (tripTitle set)', () => {
|
||||
seedStore(useAddonStore, {
|
||||
addons: [{ id: 'vacay', name: 'Vacay', icon: 'CalendarDays', type: 'global', enabled: true }],
|
||||
});
|
||||
render(<Navbar tripTitle="Japan 2025" />);
|
||||
expect(screen.queryByRole('link', { name: /vacay/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-026: notification bell visible when tripId provided', () => {
|
||||
render(<Navbar tripId="1" />);
|
||||
// InAppNotificationBell renders a button — check it is present
|
||||
const buttons = screen.getAllByRole('button');
|
||||
expect(buttons.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-027: user avatar image shown when avatar_url set', () => {
|
||||
seedStore(useAuthStore, {
|
||||
user: buildUser({ username: 'testuser', avatar_url: 'https://example.com/av.jpg' }),
|
||||
isAuthenticated: true,
|
||||
});
|
||||
render(<Navbar />);
|
||||
const avatarImg = document.querySelector('img[src="https://example.com/av.jpg"]');
|
||||
expect(avatarImg).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-028: user initial shown when no avatar_url', () => {
|
||||
seedStore(useAuthStore, {
|
||||
user: buildUser({ username: 'testuser', avatar_url: null }),
|
||||
isAuthenticated: true,
|
||||
});
|
||||
render(<Navbar />);
|
||||
// The initial is rendered as the first char uppercased in a div
|
||||
expect(screen.getAllByText('T')[0]).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-029: clicking backdrop overlay closes user menu', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Navbar />);
|
||||
await user.click(screen.getByText('testuser'));
|
||||
expect(screen.getByText('Settings')).toBeInTheDocument();
|
||||
// The backdrop overlay is a fixed-inset div rendered in the portal
|
||||
const backdrop = document.querySelector('[style*="inset: 0"]') as HTMLElement;
|
||||
if (backdrop) {
|
||||
await user.click(backdrop);
|
||||
expect(screen.queryByText('Settings')).not.toBeInTheDocument();
|
||||
}
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-030: dark mode auto uses system preference', () => {
|
||||
// 'auto' dark_mode relies on matchMedia — seed with auto and render
|
||||
seedStore(useSettingsStore, { settings: buildSettings({ dark_mode: 'auto' }) });
|
||||
render(<Navbar />);
|
||||
// Component should render without errors regardless of system preference
|
||||
expect(document.querySelector('nav')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-031: dark mode toggle calls updateSetting with light when currently dark', async () => {
|
||||
const user = userEvent.setup();
|
||||
const updateSetting = vi.fn().mockResolvedValue(undefined);
|
||||
seedStore(useSettingsStore, { settings: buildSettings({ dark_mode: 'dark' }), updateSetting });
|
||||
render(<Navbar />);
|
||||
const toggleBtn = document.querySelector('button[title]') as HTMLElement;
|
||||
expect(toggleBtn).toBeTruthy();
|
||||
await user.click(toggleBtn);
|
||||
expect(updateSetting).toHaveBeenCalledWith('dark_mode', 'light');
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-032: user email shown in open user menu', async () => {
|
||||
const user = userEvent.setup();
|
||||
seedStore(useAuthStore, {
|
||||
user: buildUser({ username: 'testuser', email: 'testuser@example.com' }),
|
||||
isAuthenticated: true,
|
||||
});
|
||||
render(<Navbar />);
|
||||
await user.click(screen.getByText('testuser'));
|
||||
expect(screen.getByText('testuser@example.com')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-033: administrator badge shown for admin user in open menu', async () => {
|
||||
const user = userEvent.setup();
|
||||
seedStore(useAuthStore, {
|
||||
user: buildUser({ username: 'adminuser', role: 'admin' }),
|
||||
isAuthenticated: true,
|
||||
});
|
||||
render(<Navbar />);
|
||||
await user.click(screen.getByText('adminuser'));
|
||||
expect(screen.getByText('Administrator')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user