fix(web/test): update playlist API tests for api.* call surface (#375)
After e8eff1b migrated playlists.ts from raw apiFetch to the api.*
wrapper, three test files mocked the wrong surface:
- playlists.test.ts: GET case asserted init.method === 'GET' but
api.get omits init.method entirely (fetch defaults to GET). Fall
back to 'GET' when init.method is undefined.
- playlists.refresh-discover.test.ts: re-mock ./client to expose
`api: { post: vi.fn() }` instead of `apiFetch`; assertions check
api.post call args.
- playlists.refresh-foryou.test.ts: same.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,28 +2,25 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { refreshDiscover } from './playlists';
|
||||
|
||||
vi.mock('./client', () => ({
|
||||
apiFetch: vi.fn()
|
||||
api: { post: vi.fn() }
|
||||
}));
|
||||
|
||||
import { apiFetch } from './client';
|
||||
import { api } from './client';
|
||||
|
||||
describe('refreshDiscover', () => {
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
it('POSTs the correct path and returns the response', async () => {
|
||||
const sample = { playlist_id: 'abc-123', track_count: 100 };
|
||||
(apiFetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(sample);
|
||||
(api.post as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(sample);
|
||||
const got = await refreshDiscover();
|
||||
expect(apiFetch).toHaveBeenCalledWith(
|
||||
'/api/playlists/system/discover/refresh',
|
||||
expect.objectContaining({ method: 'POST' })
|
||||
);
|
||||
expect(api.post).toHaveBeenCalledWith('/api/playlists/system/discover/refresh', {});
|
||||
expect(got).toEqual(sample);
|
||||
});
|
||||
|
||||
it('handles empty-library response', async () => {
|
||||
const sample = { playlist_id: null, track_count: 0 };
|
||||
(apiFetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(sample);
|
||||
(api.post as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(sample);
|
||||
const got = await refreshDiscover();
|
||||
expect(got.playlist_id).toBe(null);
|
||||
expect(got.track_count).toBe(0);
|
||||
|
||||
@@ -2,28 +2,25 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { refreshForYou } from './playlists';
|
||||
|
||||
vi.mock('./client', () => ({
|
||||
apiFetch: vi.fn()
|
||||
api: { post: vi.fn() }
|
||||
}));
|
||||
|
||||
import { apiFetch } from './client';
|
||||
import { api } from './client';
|
||||
|
||||
describe('refreshForYou', () => {
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
it('POSTs the correct path and returns the response', async () => {
|
||||
const sample = { playlist_id: 'pl-abc', track_count: 25, track_ids: ['t1', 't2'] };
|
||||
(apiFetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(sample);
|
||||
(api.post as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(sample);
|
||||
const got = await refreshForYou();
|
||||
expect(apiFetch).toHaveBeenCalledWith(
|
||||
'/api/playlists/system/for-you/refresh',
|
||||
expect.objectContaining({ method: 'POST' })
|
||||
);
|
||||
expect(api.post).toHaveBeenCalledWith('/api/playlists/system/for-you/refresh', {});
|
||||
expect(got).toEqual(sample);
|
||||
});
|
||||
|
||||
it('handles empty-library response', async () => {
|
||||
const sample = { playlist_id: null, track_count: 0, track_ids: [] };
|
||||
(apiFetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(sample);
|
||||
(api.post as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(sample);
|
||||
const got = await refreshForYou();
|
||||
expect(got.playlist_id).toBe(null);
|
||||
expect(got.track_ids).toEqual([]);
|
||||
|
||||
@@ -29,7 +29,8 @@ describe('playlists API helper', () => {
|
||||
expect(r.public).toEqual([]);
|
||||
const call = spy.mock.calls[0];
|
||||
expect(call[0]).toBe('/api/playlists?kind=user');
|
||||
expect((call[1] as RequestInit).method).toBe('GET');
|
||||
// GET is fetch's default method; api.get omits init.method entirely.
|
||||
expect((call[1] as RequestInit | undefined)?.method ?? 'GET').toBe('GET');
|
||||
});
|
||||
|
||||
test('listPlaylists passes kind=system through the query string', async () => {
|
||||
|
||||
Reference in New Issue
Block a user