Files
minstrel/web/src/lib/components/AlbumMenu.test.ts
T
bvandeusen 9acad5461e refactor(web): extract emptyPlaylistsMock + apiClientMock test helpers
The DRY pass audit (#375) found two inline mock patterns repeated across the
vitest suite: a default empty-playlists mock for $lib/api/playlists (4 exact
copies in menu/card tests) and an api-client spy mock for $lib/api/client
(9 callers split between get-only and full-RESTy shapes — unified into one
helper that always returns all four verb spies).

Mirrors the existing test-utils/mocks/likes.ts and test-utils/mocks/quarantine.ts
convention. Tests with intentionally divergent shapes (AddToPlaylistMenu's
richer createPlaylist payload, PlaylistCard's getPlaylist, route-specific
mocks, events.svelte's specific resolved value) stay inline.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 12:45:15 -04:00

88 lines
3.7 KiB
TypeScript

import { afterEach, describe, expect, test, vi } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/svelte';
import { emptyLikesMock } from '../../test-utils/mocks/likes';
import { emptyPlaylistsMock } from '../../test-utils/mocks/playlists';
import type { TrackRef } from '$lib/api/types';
import { makeTracks } from '$test-utils/fixtures/track';
vi.mock('$app/navigation', () => ({ goto: vi.fn() }));
vi.mock('$lib/api/likes', () => emptyLikesMock());
vi.mock('$lib/api/albums', async (orig) => {
const actual = (await orig()) as Record<string, unknown>;
return { ...actual, listAlbumTracks: vi.fn().mockResolvedValue([]) };
});
vi.mock('$lib/api/playlists', () => emptyPlaylistsMock());
vi.mock('$lib/player/store.svelte', () => ({
enqueueTracks: vi.fn()
}));
import AlbumMenu from './AlbumMenu.svelte';
import { enqueueTracks } from '$lib/player/store.svelte';
import { listAlbumTracks } from '$lib/api/albums';
import { likeEntity } from '$lib/api/likes';
import { goto } from '$app/navigation';
const TRACKS: TrackRef[] = makeTracks(2, {
album_id: 'al1', album_title: 'Disc', artist_id: 'a1', artist_name: 'Test'
});
const PROPS = { albumId: 'al1', albumTitle: 'Disc', artistId: 'a1', artistName: 'Test' };
afterEach(() => {
vi.clearAllMocks();
});
describe('AlbumMenu', () => {
test('kebab opens menu with action items', async () => {
render(AlbumMenu, { props: { ...PROPS, tracks: TRACKS } });
await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
expect(await screen.findByText(/Like album/i)).toBeInTheDocument();
expect(screen.getByText(/Add to queue/i)).toBeInTheDocument();
expect(screen.getByText(/Add to playlist/i)).toBeInTheDocument();
expect(screen.getByText(/Go to artist/i)).toBeInTheDocument();
});
test('Add to queue uses cached tracks when provided', async () => {
render(AlbumMenu, { props: { ...PROPS, tracks: TRACKS } });
await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
await fireEvent.click(screen.getByText(/Add to queue/i));
expect(enqueueTracks).toHaveBeenCalledWith(TRACKS);
expect(listAlbumTracks).not.toHaveBeenCalled();
});
test('Add to queue lazily fetches when no cached tracks', async () => {
(listAlbumTracks as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(TRACKS);
render(AlbumMenu, { props: PROPS });
await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
await fireEvent.click(screen.getByText(/Add to queue/i));
await waitFor(() => expect(enqueueTracks).toHaveBeenCalledWith(TRACKS));
expect(listAlbumTracks).toHaveBeenCalledWith('al1');
});
test('Like item calls likeEntity', async () => {
render(AlbumMenu, { props: { ...PROPS, tracks: TRACKS } });
await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
await fireEvent.click(screen.getByText(/Like album/i));
expect(likeEntity).toHaveBeenCalled();
});
test('Go to artist navigates', async () => {
render(AlbumMenu, { props: { ...PROPS, tracks: TRACKS } });
await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
await fireEvent.click(screen.getByText(/Go to artist/i));
expect(goto).toHaveBeenCalledWith('/artists/a1');
});
test('Escape key closes the menu', async () => {
render(AlbumMenu, { props: { ...PROPS, tracks: TRACKS } });
await fireEvent.click(screen.getByRole('button', { name: /album actions for disc/i }));
expect(await screen.findByText(/Like album/i)).toBeInTheDocument();
await fireEvent.keyDown(window, { key: 'Escape' });
expect(screen.queryByText(/Like album/i)).not.toBeInTheDocument();
});
});