// FE-W4CCS-001 to FE-W4CCS-019 import { describe, it, expect, vi, beforeEach } from 'vitest' import { render, screen, fireEvent, waitFor } from '../../../tests/helpers/render' import type { ChatReaction } from './CollabChat.types' import type { NoteAuthor } from './CollabNotes.types' const linkPreview = vi.fn(async (_tripId: number, _url: string) => ({ title: 'TREK', image: null as string | null })) vi.mock('../../api/client', () => ({ collabApi: { linkPreview: (tripId: number, url: string) => linkPreview(tripId, url) } })) import { TwemojiImg } from './CollabChatTwemojiImg' import { ReactionBadge } from './CollabChatReactionBadge' import { UserAvatar } from './CollabNotesUserAvatar' import { WebsiteThumbnail } from './CollabNotesWebsiteThumbnail' import { LinkPreview } from './CollabChatLinkPreview' function reaction(overrides: Partial = {}): ChatReaction { return { emoji: '👍', count: 1, users: [{ id: 1, username: 'ada' }], ...overrides } as unknown as ChatReaction } beforeEach(() => { linkPreview.mockReset() linkPreview.mockResolvedValue({ title: 'TREK', image: null }) }) describe('TwemojiImg', () => { it('FE-W4CCS-001: renders the twemoji asset for the codepoint', () => { const { container } = render() const img = container.querySelector('img') as HTMLImageElement expect(img).toHaveAttribute('alt', '👍') expect(img.getAttribute('src')).toContain('/1f44d.png') expect(img).toHaveStyle({ width: '20px', height: '20px' }) }) it('FE-W4CCS-002: strips the variation selector from a multi-codepoint emoji', () => { const { container } = render() expect((container.querySelector('img') as HTMLImageElement).getAttribute('src')).toContain('/2764.png') }) it('FE-W4CCS-003: falls back to the plain glyph when the asset fails to load', () => { const { container } = render() fireEvent.error(container.querySelector('img')!) expect(container.querySelector('img')).toBeNull() expect(screen.getByText('👍')).toHaveStyle({ fontSize: '24px' }) }) }) describe('ReactionBadge', () => { it('FE-W4CCS-004: hides the counter for a single reactor', () => { render( {}} />) expect(screen.queryByText('1')).toBeNull() }) it('FE-W4CCS-005: shows the counter once more than one person reacted', () => { render( )} currentUserId={1} onReact={() => {}} />, ) expect(screen.getByText('3')).toBeInTheDocument() }) it('FE-W4CCS-006: clicking toggles the own reaction', () => { const onReact = vi.fn() render() fireEvent.click(screen.getByRole('button')) expect(onReact).toHaveBeenCalledOnce() }) it('FE-W4CCS-007: hovering portals the list of reactors and leaving removes it', () => { render( )} currentUserId={1} onReact={() => {}} />, ) const badge = screen.getByRole('button') fireEvent.mouseEnter(badge) expect(screen.getByText('ada, bob')).toBeInTheDocument() fireEvent.mouseLeave(badge) expect(screen.queryByText('ada, bob')).toBeNull() }) it('FE-W4CCS-008: shows no tooltip when nobody is named', () => { render()} currentUserId={1} onReact={() => {}} />) fireEvent.mouseEnter(screen.getByRole('button')) expect(document.body.querySelectorAll('[style*="translate(-50%, -100%)"]')).toHaveLength(0) }) }) describe('UserAvatar', () => { it('FE-W4CCS-009: renders nothing without a user', () => { const { container } = render() expect(container).toBeEmptyDOMElement() }) it('FE-W4CCS-010: renders the avatar image when one is set', () => { const { container } = render() const img = container.querySelector('img') as HTMLImageElement expect(img).toHaveAttribute('src', '/uploads/avatars/ada.png') expect(img).toHaveAttribute('alt', 'ada') expect(img).toHaveStyle({ width: '20px' }) }) it('FE-W4CCS-011: falls back to the first letter, and to ? without a name', () => { const { unmount } = render() expect(screen.getByText('a')).toBeInTheDocument() unmount() render() expect(screen.getByText('?')).toBeInTheDocument() }) }) describe('WebsiteThumbnail', () => { it('FE-W4CCS-012: shows the domain until an OG image arrives', async () => { render() expect(screen.getByText('liketrek.com')).toBeInTheDocument() await waitFor(() => expect(screen.getByRole('link')).toHaveAttribute('aria-label', 'TREK')) expect(linkPreview).toHaveBeenCalledWith(4, 'https://www.liketrek.com/docs') }) it('FE-W4CCS-013: renders the OG image once the preview resolves', async () => { linkPreview.mockResolvedValue({ title: 'Docs', image: 'https://cdn.example/og.png' }) const { container } = render() await waitFor(() => expect(container.querySelector('img')).not.toBeNull()) expect(container.querySelector('img')).toHaveAttribute('src', 'https://cdn.example/og.png') // A broken OG image falls back to the domain chip. fireEvent.error(container.querySelector('img')!) expect(screen.getByText('example.com')).toBeInTheDocument() }) it('FE-W4CCS-014: keeps the domain chip when the preview request fails', async () => { linkPreview.mockRejectedValue(new Error('blocked')) render() expect(screen.getByText('blocked.example')).toBeInTheDocument() await waitFor(() => expect(linkPreview).toHaveBeenCalled()) expect(screen.getByRole('link')).toHaveAttribute('aria-label', 'https://blocked.example/a') }) it('FE-W4CCS-017: a url that is not http(s) is shown but neither linked nor previewed', async () => { const { unmount, container } = render() expect(screen.queryByRole('link')).toBeNull() fireEvent.mouseEnter(container.firstElementChild!) expect(await screen.findByRole('tooltip')).toHaveTextContent('javascript:alert(1)') unmount() render() expect(screen.getByText('link')).toBeInTheDocument() expect(screen.queryByRole('link')).toBeNull() expect(linkPreview).not.toHaveBeenCalled() }) it('FE-W4CCS-015: caches per trip, because the preview endpoint is trip-scoped', async () => { linkPreview.mockResolvedValue({ title: 'Trip 4', image: null }) const shared = 'https://example.com/shared' const first = render() await waitFor(() => expect(screen.getByRole('link')).toHaveAttribute('aria-label', 'Trip 4')) first.unmount() linkPreview.mockResolvedValue({ title: 'Trip 9', image: null }) render() await waitFor(() => expect(screen.getByRole('link')).toHaveAttribute('aria-label', 'Trip 9')) expect(linkPreview).toHaveBeenCalledTimes(2) expect(linkPreview).toHaveBeenLastCalledWith(9, shared) }) it('FE-W4CCS-016: a broken OG image does not poison the next url', async () => { linkPreview.mockResolvedValue({ title: 'A', image: 'https://cdn.example/a.png' }) const { container, rerender } = render() await waitFor(() => expect(container.querySelector('img')).not.toBeNull()) fireEvent.error(container.querySelector('img')!) expect(container.querySelector('img')).toBeNull() linkPreview.mockResolvedValue({ title: 'B', image: 'https://cdn.example/b.png' }) rerender() await waitFor(() => expect(container.querySelector('img')).not.toBeNull()) expect(container.querySelector('img')).toHaveAttribute('src', 'https://cdn.example/b.png') }) }) describe('LinkPreview', () => { it('FE-W4CCS-018: caches per trip, because the preview endpoint is trip-scoped', async () => { const shared = 'https://example.com/preview' linkPreview.mockResolvedValue({ title: 'Trip 4', image: null }) const first = render() expect(await screen.findByText('Trip 4')).toBeInTheDocument() first.unmount() linkPreview.mockResolvedValue({ title: 'Trip 9', image: null }) render() expect(await screen.findByText('Trip 9')).toBeInTheDocument() expect(linkPreview).toHaveBeenCalledTimes(2) expect(linkPreview).toHaveBeenLastCalledWith(9, shared) }) it('FE-W4CCS-019: a preview that resolves after the url changed is not shown', async () => { let resolveFirst: (v: { title: string; image: string | null }) => void = () => {} linkPreview.mockImplementationOnce(() => new Promise(res => { resolveFirst = res })) const { rerender } = render() linkPreview.mockResolvedValue({ title: 'Second', image: null }) rerender() expect(await screen.findByText('Second')).toBeInTheDocument() resolveFirst({ title: 'First', image: null }) await waitFor(() => expect(screen.queryByText('First')).toBeNull()) expect(screen.getByText('Second')).toBeInTheDocument() }) })