Files
minstrel/web/src/lib/components/ArtistMenu.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

77 lines
3.3 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/artists', () => ({
listArtistTracks: vi.fn().mockResolvedValue([])
}));
vi.mock('$lib/api/playlists', () => emptyPlaylistsMock());
vi.mock('$lib/player/store.svelte', () => ({
enqueueTracks: vi.fn()
}));
import ArtistMenu from './ArtistMenu.svelte';
import { enqueueTracks } from '$lib/player/store.svelte';
import { listArtistTracks } from '$lib/api/artists';
import { likeEntity, unlikeEntity } from '$lib/api/likes';
const TRACKS: TrackRef[] = makeTracks(2, {
album_id: 'al1', artist_id: 'a1', artist_name: 'Test'
});
afterEach(() => {
vi.clearAllMocks();
});
describe('ArtistMenu', () => {
test('kebab opens menu with action items', async () => {
render(ArtistMenu, { props: { artistId: 'a1', artistName: 'Test', tracks: TRACKS } });
const kebab = screen.getByRole('button', { name: /artist actions for test/i });
await fireEvent.click(kebab);
expect(await screen.findByText(/Like artist/i)).toBeInTheDocument();
expect(screen.getByText(/Add all to queue/i)).toBeInTheDocument();
expect(screen.getByText(/Add all to playlist/i)).toBeInTheDocument();
});
test('Add all to queue uses cached tracks when provided', async () => {
render(ArtistMenu, { props: { artistId: 'a1', artistName: 'Test', tracks: TRACKS } });
await fireEvent.click(screen.getByRole('button', { name: /artist actions for test/i }));
await fireEvent.click(screen.getByText(/Add all to queue/i));
expect(enqueueTracks).toHaveBeenCalledWith(TRACKS);
expect(listArtistTracks).not.toHaveBeenCalled();
});
test('Add all to queue lazily fetches when no cached tracks', async () => {
(listArtistTracks as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(TRACKS);
render(ArtistMenu, { props: { artistId: 'a1', artistName: 'Test' } });
await fireEvent.click(screen.getByRole('button', { name: /artist actions for test/i }));
await fireEvent.click(screen.getByText(/Add all to queue/i));
await waitFor(() => expect(enqueueTracks).toHaveBeenCalledWith(TRACKS));
expect(listArtistTracks).toHaveBeenCalledWith('a1');
});
test('Like item calls likeEntity', async () => {
render(ArtistMenu, { props: { artistId: 'a1', artistName: 'Test', tracks: TRACKS } });
await fireEvent.click(screen.getByRole('button', { name: /artist actions for test/i }));
await fireEvent.click(screen.getByText(/Like artist/i));
expect(likeEntity).toHaveBeenCalled();
});
test('Escape key closes the menu', async () => {
render(ArtistMenu, { props: { artistId: 'a1', artistName: 'Test', tracks: TRACKS } });
await fireEvent.click(screen.getByRole('button', { name: /artist actions for test/i }));
expect(screen.getByRole('menu')).toBeInTheDocument();
await fireEvent.keyDown(window, { key: 'Escape' });
expect(screen.queryByRole('menu')).not.toBeInTheDocument();
});
});