feat(web/m7-377): AlbumMenu component + listAlbumTracks helper
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import { afterEach, describe, expect, test, vi } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/svelte';
|
||||
import { readable } from 'svelte/store';
|
||||
import type { TrackRef } from '$lib/api/types';
|
||||
|
||||
vi.mock('$app/navigation', () => ({ goto: vi.fn() }));
|
||||
|
||||
vi.mock('$lib/api/likes', () => ({
|
||||
createLikedIdsQuery: () =>
|
||||
readable({ data: { track_ids: [], album_ids: [], artist_ids: [] }, isPending: false, isError: false }),
|
||||
likeEntity: vi.fn().mockResolvedValue(undefined),
|
||||
unlikeEntity: vi.fn().mockResolvedValue(undefined)
|
||||
}));
|
||||
|
||||
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', () => ({
|
||||
createPlaylistsQuery: () =>
|
||||
readable({ data: { owned: [], public: [] }, isPending: false, isError: false }),
|
||||
appendTracks: vi.fn().mockResolvedValue({ id: 'p1', tracks: [] }),
|
||||
createPlaylist: vi.fn().mockResolvedValue({ id: 'p1', name: 'New' })
|
||||
}));
|
||||
|
||||
vi.mock('$lib/player/store.svelte', () => ({
|
||||
enqueueTracks: vi.fn()
|
||||
}));
|
||||
|
||||
vi.mock('@tanstack/svelte-query', async (orig) => {
|
||||
const actual = (await orig()) as Record<string, unknown>;
|
||||
return { ...actual, useQueryClient: () => ({ invalidateQueries: 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[] = [
|
||||
{ id: 't1', title: 'A', album_id: 'al1', album_title: 'Disc', artist_id: 'a1', artist_name: 'Test', duration_sec: 100, stream_url: '/s/t1' },
|
||||
{ id: 't2', title: 'B', album_id: 'al1', album_title: 'Disc', artist_id: 'a1', artist_name: 'Test', duration_sec: 100, stream_url: '/s/t2' }
|
||||
];
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user