Files
minstrel/web/src/routes/library/liked/liked.test.ts
T
bvandeusenandClaude Opus 5.5 5f88fe941d test(web): the search and liked page tests take page() from the shared fixture (DRY W10)
Four test files defined the same Page<T> envelope helper; they now import
it from test-utils/fixtures/page.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 03:22:07 -05:00

135 lines
5.5 KiB
TypeScript

import { afterEach, describe, expect, test, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/svelte';
import { mockInfiniteQuery } from '../../../test-utils/query';
import { apiClientMock } from '../../../test-utils/mocks/client';
import type { ArtistRef, AlbumRef, TrackRef, Page } from '#lib/api/types.js';
import { readable } from 'svelte/store';
import { makeTrack } from '#test-utils/fixtures/track.js';
vi.mock('#lib/api/likes.js', () => ({
createLikedIdsQuery: () => readable({
data: { track_ids: [], album_ids: [], artist_ids: [] },
isPending: false,
isError: false
}),
createLikedTracksInfiniteQuery: vi.fn(),
createLikedAlbumsInfiniteQuery: vi.fn(),
createLikedArtistsInfiniteQuery: vi.fn(),
likeEntity: vi.fn(),
unlikeEntity: vi.fn()
}));
vi.mock('#lib/api/client.js', () => apiClientMock());
vi.mock('#lib/player/store.svelte.js', () => ({
playQueue: vi.fn(), enqueueTrack: vi.fn(), enqueueTracks: vi.fn(), playRadio: vi.fn(),
player: { current: undefined }
}));
import LikedPage from './+page.svelte';
import {
import { page } from '#test-utils/fixtures/page.js';
createLikedTracksInfiniteQuery,
createLikedAlbumsInfiniteQuery,
createLikedArtistsInfiniteQuery
} from '#lib/api/likes.js';
afterEach(() => vi.clearAllMocks());
const ar: ArtistRef = { id: 'a1', name: 'Marlo', sort_name: 'Marlo', album_count: 1, cover_url: '' };
const al: AlbumRef = {
id: 'al1', title: 'Kind of Grey', sort_title: 'Kind of Grey', artist_id: 'a1', artist_name: 'Marlo',
year: 1959, track_count: 1, duration_sec: 100, cover_url: '/x', cover_art_source: null
};
const tr: TrackRef = makeTrack({
title: 'So Then', album_id: 'al1', album_title: 'Kind of Grey',
artist_id: 'a1', artist_name: 'Marlo', duration_sec: 100
});
function setup(opts: {
artists?: Page<ArtistRef>;
albums?: Page<AlbumRef>;
tracks?: Page<TrackRef>;
tracksQuery?: Parameters<typeof mockInfiniteQuery>[0];
}) {
(createLikedArtistsInfiniteQuery as ReturnType<typeof vi.fn>).mockReturnValue(
mockInfiniteQuery({ pages: [opts.artists ?? page<ArtistRef>([], 0)] })
);
(createLikedAlbumsInfiniteQuery as ReturnType<typeof vi.fn>).mockReturnValue(
mockInfiniteQuery({ pages: [opts.albums ?? page<AlbumRef>([], 0)] })
);
(createLikedTracksInfiniteQuery as ReturnType<typeof vi.fn>).mockReturnValue(
mockInfiniteQuery({ pages: [opts.tracks ?? page<TrackRef>([], 0)], ...opts.tracksQuery })
);
}
describe('liked library page', () => {
// One list at a time. Stacked, a long Artists list that loads as you
// scroll would bury Albums and Tracks.
test('tabs carry each count and show one list at a time', async () => {
setup({ artists: page([ar], 1), albums: page([al], 1), tracks: page([tr], 1) });
render(LikedPage);
const tabs = screen.getAllByRole('tab');
expect(tabs.map((t) => t.textContent?.replace(/\s+/g, ' ').trim())).toEqual([
'Artists 1',
'Albums 1',
'Tracks 1'
]);
expect(screen.getByText('Marlo')).toBeInTheDocument();
expect(screen.queryByText('So Then')).not.toBeInTheDocument();
await fireEvent.click(screen.getByRole('tab', { name: /albums/i }));
expect(screen.getByText('Kind of Grey')).toBeInTheDocument();
await fireEvent.click(screen.getByRole('tab', { name: /tracks/i }));
expect(screen.getByText('So Then')).toBeInTheDocument();
expect(screen.getByRole('tab', { name: /tracks/i })).toHaveAttribute('aria-selected', 'true');
});
test('opens on the first tab that has likes', () => {
setup({ tracks: page([tr], 1) });
render(LikedPage);
expect(screen.getByRole('tab', { name: /tracks/i })).toHaveAttribute('aria-selected', 'true');
expect(screen.getByText('So Then')).toBeInTheDocument();
});
test('all three empty renders the whole-page onboarding card and no tabs', () => {
setup({});
render(LikedPage);
expect(screen.getByRole('heading', { name: /no likes yet/i })).toBeInTheDocument();
expect(screen.getByRole('link', { name: /explore home/i })).toBeInTheDocument();
expect(screen.getByRole('link', { name: /browse albums/i })).toBeInTheDocument();
expect(screen.queryByRole('tablist')).not.toBeInTheDocument();
});
test('an empty tab shows its inline hint', async () => {
setup({ artists: page([ar], 1) });
render(LikedPage);
await fireEvent.click(screen.getByRole('tab', { name: /albums/i }));
expect(screen.getByText(/no liked albums yet/i)).toBeInTheDocument();
await fireEvent.click(screen.getByRole('tab', { name: /tracks/i }));
expect(screen.getByText(/no liked tracks yet/i)).toBeInTheDocument();
expect(screen.queryByRole('heading', { name: /no likes yet/i })).not.toBeInTheDocument();
});
// Preference 172: the list continues itself; there is no button to press.
test('more tracks load as you scroll, with no Load more button', () => {
setup({ tracks: page([tr], 100), tracksQuery: { hasNextPage: true } });
const { container } = render(LikedPage);
expect(screen.queryByRole('button', { name: /load more/i })).not.toBeInTheDocument();
expect(container.querySelector('div[aria-hidden="true"].h-px')).not.toBeNull();
});
test('a failed page offers Try again, which fetches it', async () => {
const fetchNextPage = vi.fn();
setup({
tracks: page([tr], 100),
tracksQuery: { hasNextPage: true, isFetchNextPageError: true, fetchNextPage }
});
render(LikedPage);
await fireEvent.click(screen.getByRole('button', { name: 'Try again' }));
expect(fetchNextPage).toHaveBeenCalledTimes(1);
});
});