// FE-COMP-DUPECARD-001 to FE-COMP-DUPECARD-013 import userEvent from '@testing-library/user-event'; import React from 'react'; import { duplicatePair, duplicateSide, listOrigin, tripOrigin } from '../../../tests/helpers/placesLibrary'; import { render, screen, within } from '../../../tests/helpers/render'; import { useTranslation } from '../../i18n/TranslationContext'; import DuplicatePairCard from './DuplicatePairCard'; type Props = Omit, 't'>; function Harness(props: Props): React.ReactElement { const { t } = useTranslation(); return ; } function setup(over: Partial = {}) { const props: Props = { pair: duplicatePair(), busy: false, onMerge: vi.fn(), onDismiss: vi.fn(), variant: 'dialog', ...over, }; const view = render(); return { ...view, props }; } describe('DuplicatePairCard', () => { it('FE-COMP-DUPECARD-001: shows both sides with their names and the places they come from', () => { setup(); expect(screen.getByText('Colosseum')).toBeInTheDocument(); expect(screen.getByText('Colosseo')).toBeInTheDocument(); expect(screen.getByText('Rome 2026')).toBeInTheDocument(); expect(screen.getByText('Favourites')).toBeInTheDocument(); }); it('FE-COMP-DUPECARD-002: a shared provider id is explained as the same place on the map provider', () => { setup({ pair: duplicatePair({ reasons: ['provider_id'] }) }); expect(screen.getByText('The same place on the map provider')).toBeInTheDocument(); expect(screen.queryByText('The same name at the same spot')).not.toBeInTheDocument(); }); it('FE-COMP-DUPECARD-003: the same name nearby is explained as such', () => { setup({ pair: duplicatePair({ reasons: ['name_nearby'] }) }); expect(screen.getByText('The same name at the same spot')).toBeInTheDocument(); }); it('FE-COMP-DUPECARD-004: when both reasons apply the provider id wins', () => { setup({ pair: duplicatePair({ reasons: ['name_nearby', 'provider_id'] }) }); expect(screen.getByText('The same place on the map provider')).toBeInTheDocument(); expect(screen.queryByText('The same name at the same spot')).not.toBeInTheDocument(); }); it('FE-COMP-DUPECARD-005: marks only the facts that differ', () => { setup({ pair: duplicatePair({ differing: ['name'] }) }); expect(screen.getByText('Colosseum').className).toContain('bg-warning-soft'); expect(screen.getByText('Colosseo').className).toContain('bg-warning-soft'); for (const address of screen.getAllByText('Piazza del Colosseo')) { expect(address.className).not.toContain('bg-warning-soft'); } }); it('FE-COMP-DUPECARD-006: a differing address is marked, and a missing one reads "No address"', () => { setup({ pair: duplicatePair({ a: duplicateSide({ address: 'Via Roma 1' }), b: duplicateSide({ key: 'list:900', name: 'Colosseo', address: null, origins: [listOrigin()] }), differing: ['address'], }), }); expect(screen.getByText('Via Roma 1').className).toContain('bg-warning-soft'); expect(screen.getByText('No address').className).toContain('bg-warning-soft'); }); it('FE-COMP-DUPECARD-007: a side without an address that does not differ shows no address line', () => { setup({ pair: duplicatePair({ a: duplicateSide({ address: null }), b: duplicateSide({ key: 'list:900', name: 'Colosseo', address: null, origins: [listOrigin()] }), differing: ['name'], }), }); expect(screen.queryByText('No address')).not.toBeInTheDocument(); }); it('FE-COMP-DUPECARD-008: "Keep this one" under each side merges with that side leading', async () => { const user = userEvent.setup(); const { props } = setup(); const keeps = screen.getAllByRole('button', { name: /Keep this one/ }); expect(keeps).toHaveLength(2); await user.click(keeps[0]); expect(props.onMerge).toHaveBeenLastCalledWith('a'); await user.click(keeps[1]); expect(props.onMerge).toHaveBeenLastCalledWith('b'); expect(props.onMerge).toHaveBeenCalledTimes(2); expect(screen.queryByText(/can no longer open/)).not.toBeInTheDocument(); }); it('FE-COMP-DUPECARD-009: a pair that cannot be merged hides "Keep this one" and says why', () => { setup({ pair: duplicatePair({ can_merge: false }) }); expect(screen.queryByRole('button', { name: /Keep this one/ })).not.toBeInTheDocument(); expect(screen.getByText(/can no longer open/)).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Not the same place' })).toBeEnabled(); }); it('FE-COMP-DUPECARD-010: "Not the same place" dismisses the pair', async () => { const user = userEvent.setup(); const { props } = setup(); await user.click(screen.getByRole('button', { name: 'Not the same place' })); expect(props.onDismiss).toHaveBeenCalledTimes(1); expect(props.onMerge).not.toHaveBeenCalled(); }); it('FE-COMP-DUPECARD-011: while busy every button is disabled', () => { setup({ busy: true }); for (const button of screen.getAllByRole('button')) expect(button).toBeDisabled(); }); it('FE-COMP-DUPECARD-012: the phone variant renders the same card in its own look', async () => { const user = userEvent.setup(); const { props, container } = setup({ variant: 'sheet' }); const card = within(container).getByRole('article'); expect(card.className).toContain('bg-m-sheet'); expect(screen.getByText('The same name at the same spot')).toBeInTheDocument(); await user.click(screen.getAllByRole('button', { name: /Keep this one/ })[1]); expect(props.onMerge).toHaveBeenCalledWith('b'); await user.click(screen.getByRole('button', { name: 'Not the same place' })); expect(props.onDismiss).toHaveBeenCalledTimes(1); }); it('FE-COMP-DUPECARD-013: a side lists a trip and a saved place as separate chips', () => { setup({ pair: duplicatePair({ a: duplicateSide({ origins: [tripOrigin({ place_id: 1, trip_title: 'Rome 2026' }), listOrigin({ collection_name: 'Wishlist' })], }), }), }); expect(screen.getByText('Wishlist')).toBeInTheDocument(); expect(screen.getAllByText('Rome 2026')).toHaveLength(1); }); });