Files
minstrel/web/src/routes/library/history/page.test.ts
T
bvandeusenandClaude Opus 5.5 feef626ba5
release / go (push) Successful in 1m55s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 17s
release / integration (push) Successful in 2m40s
release / android (push) Successful in 2m6s
release / Build signed APK (push) Successful in 2m0s
release / Build + push container image (push) Successful in 37s
web: Vitest after svelte-query 6; spaces that Svelte trimmed out of four labels (#8532)
Run 11484 failed 18 Vitest tests in 7 files:
- Two tests still mocked a query as a hand-written store ({ subscribe }).
  They now mock a plain query object.
- TrackMenu now creates its hidden-tracks query when it mounts, not lazily
  as the old $derived store did, so page tests that render track rows mock
  #lib/api/quarantine like the component tests already did.
- The tuning trends headers read "Plays(window)". Svelte drops the
  leading space inside <span> (window)</span>, and jsdom 31 no longer
  hides that. The same gluing showed in:
  - "Title· File missing" on track rows;
  - the track position on Suspect sources;
  - the screen-reader "(new)" on notifications.
  Each space now sits outside the span, or is an expression Svelte keeps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 14:09:20 -05:00

127 lines
4.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen } from '@testing-library/svelte';
import type { HistoryEvent, HistoryResponse } from '#lib/api/history.js';
import { makeTrack } from '#test-utils/fixtures/track.js';
import { emptyLikesMock } from '../../../test-utils/mocks/likes';
import { emptyQuarantineMock } from '../../../test-utils/mocks/quarantine';
const mkTrack = (id: string) => makeTrack({ id: `track-${id}`, title: `Song ${id}` });
const mkEvent = (id: string, playedAt: Date): HistoryEvent => ({
id: `event-${id}`,
played_at: playedAt.toISOString(),
track: mkTrack(id)
});
let mockData: { pages: HistoryResponse[] } = { pages: [] };
let mockHasNextPage = false;
let mockIsPending = false;
let mockIsError = false;
let mockIsFetchingNextPage = false;
const mockFetchNextPage = vi.fn();
const mockRefetch = vi.fn();
vi.mock('#lib/api/history.js', async () => {
const actual = await vi.importActual<typeof import('#lib/api/history.js')>(
'#lib/api/history.js'
);
return {
...actual,
createHistoryQuery: () => ({
data: mockData,
isPending: mockIsPending,
isError: mockIsError,
error: mockIsError ? new Error('test') : null,
hasNextPage: mockHasNextPage,
isFetchingNextPage: mockIsFetchingNextPage,
fetchNextPage: mockFetchNextPage,
refetch: mockRefetch
})
};
});
// Rows are TrackListRows, so LikeButton and TrackMenu render too.
vi.mock('#lib/player/store.svelte.js', () => ({
playQueue: vi.fn(),
playNext: vi.fn(),
enqueueTrack: vi.fn(),
playRadio: vi.fn(),
player: { current: undefined }
}));
vi.mock('#lib/api/likes.js', () => emptyLikesMock());
vi.mock('#lib/api/quarantine.js', () => emptyQuarantineMock());
vi.mock('#lib/branding.js', () => ({
pageTitle: (s: string) => `Minstrel · ${s}`
}));
// Imported after the mocks: the component pulls in LikeButton, whose
// mocked module must already be initialised.
import HistoryPage from './+page.svelte';
describe('library/history page', () => {
beforeEach(() => {
mockData = { pages: [] };
mockHasNextPage = false;
mockIsPending = false;
mockIsError = false;
mockIsFetchingNextPage = false;
mockFetchNextPage.mockClear();
mockRefetch.mockClear();
});
it('renders Today and Yesterday section headers across page boundary', () => {
const today = new Date();
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
mockData = {
pages: [
{ events: [mkEvent('a', today), mkEvent('b', today)], has_more: true },
{ events: [mkEvent('c', yesterday)], has_more: false }
]
};
render(HistoryPage);
expect(screen.getByText('Today')).toBeInTheDocument();
expect(screen.getByText('Yesterday')).toBeInTheDocument();
expect(screen.getByText('Song a')).toBeInTheDocument();
expect(screen.getByText('Song b')).toBeInTheDocument();
expect(screen.getByText('Song c')).toBeInTheDocument();
});
it('renders empty state when query data is empty', () => {
mockData = { pages: [{ events: [], has_more: false }] };
render(HistoryPage);
expect(screen.getByText(/no listening history yet/i)).toBeInTheDocument();
});
it('renders error banner on isError', () => {
mockIsError = true;
render(HistoryPage);
// ApiErrorBanner renders a role="alert" container. Assert by role
// rather than by message text, since the message comes from the
// error object's `.message` property and varies.
expect(screen.getByRole('alert')).toBeInTheDocument();
});
it('does not render the InfiniteScrollSentinel when hasNextPage is false', () => {
mockData = { pages: [{ events: [mkEvent('a', new Date())], has_more: false }] };
mockHasNextPage = false;
const { container } = render(HistoryPage);
// InfiniteScrollSentinel renders <div aria-hidden="true" class="h-px">.
// When hasNextPage is false, no such div should be in the DOM.
expect(container.querySelector('div[aria-hidden="true"].h-px')).toBeNull();
// Confirm the page rendered the row so we know we hit the success branch.
expect(screen.getByText('Song a')).toBeInTheDocument();
});
it('renders the InfiniteScrollSentinel when hasNextPage is true', () => {
mockData = { pages: [{ events: [mkEvent('a', new Date())], has_more: true }] };
mockHasNextPage = true;
mockIsFetchingNextPage = false;
const { container } = render(HistoryPage);
expect(container.querySelector('div[aria-hidden="true"].h-px')).not.toBeNull();
});
});