Files
TREK/client/src/components/Collections/DuplicatePairCard.test.tsx
T
Maurice bacc6a29a7 feat(places): review possible duplicates, and repair the client gates
All places offers the pairs that look like one place: both sides side by side
with what differs marked, "keep this one" to group them as one place of the
user's own (undo right after), and "not the same place" to set a pair aside for
good. Desktop dialog and phone sheet share usePlaceDuplicates; every locale has
the new strings.

What the last dev run flagged on the client:
- Tailwind's Prettier plugin had trimmed the leading space of conditional class
  names (col-filter-btn, col-lrow sel, col-split map-full, col-detail docked),
  so the filter row and the open row lost their styling; those classes are now
  joined so the formatter cannot glue them.
- The dock position lives in one helper for the lists and All places, which
  moves the measured-position markers there instead of adding new ones.
- usePlacesLibrary is split into filters, pages, detail and cover hooks that
  derive their loading state instead of setting it inside effects; the big
  sheets and views are cut into parts under the complexity limit, dynamic icons
  go through createElement, originName moves out of a component file.
- formatRange is guarded like in the packing export, the cover lookup skips
  places without a position.
- Tests follow the rename to Places, the category icon, the default list icon
  and the new trip collections store; react-hooks/refs is lowered to the count
  the run measured.
- The rating on a place is a round badge again, in the list and on the map.
2026-11-29 16:28:22 +01:00

145 lines
6.2 KiB
TypeScript

// 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<React.ComponentProps<typeof DuplicatePairCard>, 't'>;
function Harness(props: Props): React.ReactElement {
const { t } = useTranslation();
return <DuplicatePairCard {...props} t={t} />;
}
function setup(over: Partial<Props> = {}) {
const props: Props = {
pair: duplicatePair(),
busy: false,
onMerge: vi.fn(),
onDismiss: vi.fn(),
variant: 'dialog',
...over,
};
const view = render(<Harness {...props} />);
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);
});
});