import { afterEach, describe, expect, test, vi } from 'vitest'; import { fireEvent, render, screen, waitFor } from '@testing-library/svelte'; import { mockQuery } from '../../../test-utils/query'; import type { Follow, ReleaseProfilesResponse } from '#lib/api/types.js'; vi.mock('#lib/api/wanted.js', () => ({ createFollowsQuery: vi.fn(), createReleaseProfilesQuery: vi.fn(), putFollow: vi.fn().mockResolvedValue({ following: true }), deleteFollow: vi.fn().mockResolvedValue(undefined) })); vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() })); import FollowList from './FollowList.svelte'; import { createFollowsQuery, createReleaseProfilesQuery, deleteFollow, putFollow } from '#lib/api/wanted.js'; const ago = (ms: number) => new Date(Date.now() - ms).toISOString(); const follow: Follow = { artist_mbid: 'blur', artist_name: 'Blur', artist_id: 'art-1', profile_id: 'p-std', profile_name: 'Standard', followed_at: ago(10 * 86_400_000), checked_at: ago(3 * 3_600_000), open_wants: 2, image_url: '' }; const profiles: ReleaseProfilesResponse = { profiles: [ { id: 'p-std', name: 'Standard', primary_types: ['Album'], secondary_types: [], is_default: true, follow_count: 1 }, { id: 'p-live', name: 'With live', primary_types: ['Album'], secondary_types: ['Live'], is_default: false, follow_count: 0 } ], primary_types: ['Album'], secondary_types: ['Live'] }; afterEach(() => vi.clearAllMocks()); function setup(follows: Follow[] = [follow]) { vi.mocked(createFollowsQuery).mockReturnValue( mockQuery({ data: follows }) as unknown as ReturnType ); vi.mocked(createReleaseProfilesQuery).mockReturnValue( mockQuery({ data: profiles }) as unknown as ReturnType ); return render(FollowList); } describe('FollowList', () => { test('lists artists; the profile can change and the artist be unfollowed', async () => { setup(); expect(screen.getAllByTestId('follow-row')).toHaveLength(1); expect(screen.getByText(/2 albums wanted/)).toBeTruthy(); const select = screen.getByLabelText(/profile for blur/i) as HTMLSelectElement; expect(select.value).toBe('p-std'); await fireEvent.change(select, { target: { value: 'p-live' } }); await waitFor(() => expect(putFollow).toHaveBeenCalledWith('blur', { artist_name: 'Blur', profile_id: 'p-live' }) ); await fireEvent.click(screen.getByRole('button', { name: /unfollow blur/i })); await waitFor(() => expect(deleteFollow).toHaveBeenCalledWith('blur')); }); test('with no follows it says where to follow from', () => { setup([]); expect(screen.getByText(/follow one from its artist page/i)).toBeTruthy(); }); });