import React from 'react'; import { describe, it, expect, beforeEach, vi } from 'vitest'; import { http, HttpResponse } from 'msw'; import { render, screen, fireEvent, waitFor } from '../../../tests/helpers/render'; import { server } from '../../../tests/helpers/msw/server'; import CollectionsWidget from './CollectionsWidget'; // FE-COMP-COLWIDGET-001 onwards function collections(list: unknown[]) { server.use(http.get('/api/addons/collections', () => HttpResponse.json({ collections: list }))); } const list = (over: Record = {}) => ({ id: 1, name: 'Tokyo eats', color: '#ff0000', cover_image: null, place_count: 12, ...over, }); beforeEach(() => { collections([]); }); describe('CollectionsWidget', () => { it('FE-COMP-COLWIDGET-001: shows the empty hint once the fetch resolves', async () => { render( {}} />); expect(await screen.findByText('No saved places yet')).toBeInTheDocument(); expect(screen.getByText('Collections')).toBeInTheDocument(); }); it('FE-COMP-COLWIDGET-002: renders at most six badges with their counts', async () => { collections([1, 2, 3, 4, 5, 6, 7].map(id => list({ id, name: `List ${id}`, place_count: id }))); render( {}} />); expect(await screen.findByText('List 1')).toBeInTheDocument(); expect(screen.getByText('List 6')).toBeInTheDocument(); expect(screen.queryByText('List 7')).not.toBeInTheDocument(); }); it('FE-COMP-COLWIDGET-003: a list without a count renders a zero', async () => { collections([list({ place_count: null })]); render( {}} />); await screen.findByText('Tokyo eats'); expect(screen.getByText('0')).toBeInTheDocument(); }); it('FE-COMP-COLWIDGET-004: a cover image replaces the gradient tile', async () => { collections([list({ cover_image: '/uploads/collections/1.jpg' })]); const { container } = render( {}} />); await screen.findByText('Tokyo eats'); expect(container.querySelector('img.col-badge-media')).toHaveAttribute('src', '/uploads/collections/1.jpg'); }); it('FE-COMP-COLWIDGET-005: a list without a colour falls back to the entity gradient', async () => { collections([list({ color: null })]); const { container } = render( {}} />); await screen.findByText('Tokyo eats'); const media = container.querySelector('div.col-badge-media') as HTMLElement; expect(media.style.backgroundImage).toContain('gradient'); }); it('FE-COMP-COLWIDGET-006: the header arrow runs the onOpen callback', async () => { const onOpen = vi.fn(); render(); fireEvent.click(screen.getByRole('button', { name: 'Collections' })); expect(onOpen).toHaveBeenCalledTimes(1); }); it('FE-COMP-COLWIDGET-007: a failing fetch degrades to the empty hint', async () => { server.use(http.get('/api/addons/collections', () => new HttpResponse(null, { status: 500 }))); render( {}} />); expect(await screen.findByText('No saved places yet')).toBeInTheDocument(); }); it('FE-COMP-COLWIDGET-008: only a placeholder shows while the fetch is still in flight', async () => { const { container } = render( {}} />); // Nothing remembered yet: the empty hint's shape, hidden from assistive tech, holds the place. expect(container.querySelector('.col-empty.trek-skeleton')).toHaveAttribute('aria-hidden', 'true'); await waitFor(() => expect(container.querySelector('.trek-skeleton')).toBeNull()); expect(screen.getByText('No saved places yet')).toBeInTheDocument(); }); it('FE-COMP-COLWIDGET-009: #3009 holds the badges it showed last time while the lists load, then remembers the new count', async () => { localStorage.setItem('trek_dash_collections_shown', '3'); collections([1, 2].map(id => list({ id, name: `List ${id}` }))); const { container } = render( {}} />); expect(container.querySelectorAll('.col-badge-pending')).toHaveLength(3); expect(await screen.findByText('List 1')).toBeInTheDocument(); expect(container.querySelectorAll('.col-badge-pending')).toHaveLength(0); await waitFor(() => expect(localStorage.getItem('trek_dash_collections_shown')).toBe('2')); localStorage.clear(); }); });