Files
minstrel/web/src/lib/components/Shell.test.ts
T
bvandeusenandClaude Opus 5.5 94a9c8cbe3 chore(deps): SvelteKit 3 with adapter-static 4 and TypeScript 6, full-tree npm audit (#5021)
Merges Renovate's kit 3 (PR #149) and adapter-static 4 (PR #148) bumps,
plus the migration they need. The mechanical part is `sv migrate
sveltekit-3`, run one task at a time and reviewed:

- svelte.config.js is gone. Its options move into sveltekit() in
  vite.config.ts, exported as kitOptions so vitest.config.ts runs the
  same kit setup, including the $test-utils alias the tests import.
- $lib becomes #lib through package.json "imports". There is no
  src/lib/index, so only the "#lib/*" entry is kept.
- tsconfig extends $app/tsconfig.
- Peer floors raised to kit 3's requirements: svelte ^5.57.1, vite
  ^8.0.12, svelte-check ^4.7.5.

By hand, from the codemod's list of non-automated tasks:

- goto's replaceState option is now replace; keepFocus becomes
  reset: false. For the search typeahead, reset: false also stops the
  scroll-to-top, which is wanted while typing.
- The test setup mocks drop pushState/replaceState and $app/paths
  base/assets, which kit 3 removed, and mock refreshAll in place of
  invalidateAll.
- The other flagged files only read page.url or goto internal routes,
  so they needed no change.

TypeScript goes to ^6, not the ^7 Renovate offers: kit 3 declares
typescript ^6 as a peer and svelte-check 4.7 accepts ^5 || ^6. Move to
7 once both accept it.

With Tailwind 4 and kit 3 in, `npm audit` on the whole tree reports 0,
so the web lane now audits every dependency rather than only what
ships to browsers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 10:50:39 -04:00

116 lines
4.6 KiB
TypeScript

import { afterEach, describe, expect, test, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/svelte';
vi.mock('$app/state', () => ({
page: { url: new URL('http://localhost/') }
}));
vi.mock('$app/navigation', () => ({
goto: vi.fn()
}));
// Mutable handle so individual tests can flip the user between admin /
// non-admin without re-importing the module.
const userState = vi.hoisted(() => ({
current: { id: '1', username: 'alice', is_admin: false } as {
id: string;
username: string;
is_admin: boolean;
} | null
}));
vi.mock('#lib/auth/store.svelte.js', () => ({
user: { get value() { return userState.current; } },
logout: vi.fn().mockResolvedValue(undefined)
}));
// The bell's queries need a QueryClient the Shell tests do not provide; its own
// behaviour is covered in NotificationBell.test.ts.
vi.mock('#lib/api/notifications.js', async () => {
const { mockQuery } = await import('../../test-utils/query');
return {
createUnreadCountQuery: () => mockQuery({ data: 0 }),
createNotificationsQuery: () => mockQuery({ data: { items: [], unread_count: 0 } }),
markNotificationRead: vi.fn(),
markAllNotificationsRead: vi.fn(),
badgeLabel: () => ''
};
});
import Shell from './Shell.svelte';
import { logout } from '#lib/auth/store.svelte.js';
import { goto } from '$app/navigation';
afterEach(() => {
vi.clearAllMocks();
userState.current = { id: '1', username: 'alice', is_admin: false };
});
describe('Shell', () => {
test('renders the username in the header', () => {
render(Shell);
expect(screen.getByText('alice')).toBeInTheDocument();
});
test('main nav renders the three primary surfaces in order', () => {
render(Shell);
// Library is a parent for Artists / Albums / Liked / History /
// Playlists (tabs under /library); only the parent appears here.
const labels = ['Home', 'Library', 'Discover'];
for (const label of labels) {
expect(screen.getByRole('link', { name: label })).toBeInTheDocument();
}
expect(screen.getByRole('link', { name: 'Home' })).toHaveAttribute('href', '/');
// Library jumps to the Artists tab directly so SPA navigation
// bypasses the /library 308-redirect.
expect(screen.getByRole('link', { name: 'Library' })).toHaveAttribute('href', '/library/artists');
expect(screen.getByRole('link', { name: 'Discover' })).toHaveAttribute('href', '/discover');
});
test('main nav omits Artists/Albums/Liked/History/Playlists/Search/Settings/Admin', () => {
userState.current = { id: '1', username: 'alice', is_admin: true };
render(Shell);
// Library sub-pages reach their content via the /library tab bar
// (in routes/library/+layout.svelte), not via the global Shell nav.
// Search reaches /search via the global SearchInput on the right.
// Settings + Admin live in the username dropdown — they're per-
// operator chrome, not primary surfaces.
const navLinks = screen
.getAllByRole('link')
.filter((el) => el.closest('nav[aria-label="Primary"]') !== null)
.map((el) => el.textContent?.trim());
for (const label of [
'Artists', 'Albums', 'Liked', 'History', 'Playlists',
'Search', 'Requests', 'Hidden', 'Settings', 'Admin'
]) {
expect(navLinks).not.toContain(label);
}
});
test('user-menu shows Settings + Log out for non-admin users', async () => {
userState.current = { id: '1', username: 'alice', is_admin: false };
render(Shell);
await fireEvent.click(screen.getByRole('button', { name: /alice/i }));
expect(screen.getByRole('menuitem', { name: 'Settings' })).toHaveAttribute('href', '/settings');
expect(screen.getByRole('menuitem', { name: /log out/i })).toBeInTheDocument();
expect(screen.queryByRole('menuitem', { name: 'Admin' })).not.toBeInTheDocument();
});
test('user-menu adds Admin between Settings and Log out for admin users', async () => {
userState.current = { id: '1', username: 'alice', is_admin: true };
render(Shell);
await fireEvent.click(screen.getByRole('button', { name: /alice/i }));
const items = screen.getAllByRole('menuitem').map((el) => el.textContent?.trim());
expect(items).toEqual(['Settings', 'Admin', 'Log out']);
expect(screen.getByRole('menuitem', { name: 'Admin' })).toHaveAttribute('href', '/admin');
});
test('user-menu "Log out" calls logout() and navigates to /login', async () => {
render(Shell);
await fireEvent.click(screen.getByRole('button', { name: /alice/i }));
await fireEvent.click(screen.getByRole('menuitem', { name: /log out/i }));
expect(logout).toHaveBeenCalledTimes(1);
expect(goto).toHaveBeenCalledWith('/login', { replace: true });
});
});