release / govulncheck (push) Successful in 23s
release / web (push) Successful in 1m22s
release / go (push) Successful in 1m41s
release / integration (push) Successful in 4m49s
release / android (push) Successful in 5m32s
release / Build signed APK (releases and dev) (push) Successful in 5m39s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 28s
release / Verify release artifacts (tag releases only) (push) Skipped
CI run 8789 warned on both: - kit 3 deprecates `config.alias`. The $test-utils alias becomes a `#test-utils/*` subpath import, matching #lib, and its 43 import sites move with it. - Vite's coming native config loader needs the extension on vitest.config.ts's import of vite.config. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
4.1 KiB
TypeScript
118 lines
4.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { render, screen } from '@testing-library/svelte';
|
|
import HistoryPage from './+page.svelte';
|
|
import type { HistoryEvent, HistoryResponse } from '#lib/api/history.js';
|
|
import { makeTrack } from '#test-utils/fixtures/track.js';
|
|
|
|
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: () => ({
|
|
subscribe: (run: (v: unknown) => void) => {
|
|
run({
|
|
data: mockData,
|
|
isPending: mockIsPending,
|
|
isError: mockIsError,
|
|
error: mockIsError ? new Error('test') : null,
|
|
hasNextPage: mockHasNextPage,
|
|
isFetchingNextPage: mockIsFetchingNextPage,
|
|
fetchNextPage: mockFetchNextPage,
|
|
refetch: mockRefetch
|
|
});
|
|
return () => {};
|
|
}
|
|
})
|
|
};
|
|
});
|
|
|
|
vi.mock('#lib/player/store.svelte.js', () => ({
|
|
playQueue: vi.fn()
|
|
}));
|
|
|
|
vi.mock('#lib/branding.js', () => ({
|
|
pageTitle: (s: string) => `Minstrel · ${s}`
|
|
}));
|
|
|
|
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();
|
|
});
|
|
});
|