Files

131 lines
5.1 KiB
TypeScript

import userEvent from '@testing-library/user-event';
import type { ComponentProps } from 'react';
import { describe, expect, it, vi } from 'vitest';
import { pairKey } from '../../../../src/components/Collections/usePlaceDuplicates';
import { useTranslation } from '../../../../src/i18n';
import MDuplicatesSheet from '../../../../src/mobile/screens/collections/MDuplicatesSheet';
import { duplicatePair, duplicateSide } from '../../../helpers/placesLibrary';
import { render, screen } from '../../../helpers/render';
// FE-MOB-DUPESHEET-001 to FE-MOB-DUPESHEET-012
type Props = ComponentProps<typeof MDuplicatesSheet>;
type Dupes = Props['d'];
const PAIR = duplicatePair();
const OTHER = duplicatePair({
a: duplicateSide({ key: 'trip:7', name: 'Pantheon' }),
b: duplicateSide({ key: 'trip:8', name: 'Pantheon Rome' }),
});
function dupes(over: Partial<Dupes> = {}): Dupes {
return {
pairs: [PAIR, OTHER],
hasMore: false,
loading: false,
busy: null,
lastMerge: null,
merge: vi.fn(() => Promise.resolve()),
dismiss: vi.fn(() => Promise.resolve()),
undo: vi.fn(() => Promise.resolve()),
reload: vi.fn(),
...over,
};
}
function Harness(props: Omit<Props, 't'>) {
const { t } = useTranslation();
return <MDuplicatesSheet {...props} t={t} />;
}
function setup(over: Partial<Dupes> = {}, open = true) {
const onClose = vi.fn();
const d = dupes(over);
const view = render(<Harness open={open} d={d} onClose={onClose} />);
return { ...view, d, onClose };
}
describe('MDuplicatesSheet', () => {
it('FE-MOB-DUPESHEET-001: renders nothing while closed', () => {
setup({}, false);
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});
it('FE-MOB-DUPESHEET-002: opens as a dialog with the title and what keeping a side does', () => {
setup();
expect(screen.getByRole('dialog', { name: 'Possible duplicates' })).toBeInTheDocument();
expect(screen.getByText(/Keep one side to group both as one place of yours/)).toBeInTheDocument();
});
it('FE-MOB-DUPESHEET-003: lists a phone-styled card per pair', () => {
setup();
const cards = screen.getAllByRole('article');
expect(cards).toHaveLength(2);
expect(cards[0]).toHaveClass('bg-m-ic');
expect(screen.getByText('Pantheon Rome')).toBeInTheDocument();
});
it('FE-MOB-DUPESHEET-004: with no pairs it says there are none', () => {
setup({ pairs: [] });
expect(screen.getByText('No possible duplicates.')).toBeInTheDocument();
expect(screen.queryByRole('article')).not.toBeInTheDocument();
});
it('FE-MOB-DUPESHEET-005: while loading with nothing yet it shows a spinner, not the empty note', () => {
setup({ pairs: [], loading: true });
expect(screen.queryByText('No possible duplicates.')).not.toBeInTheDocument();
expect(document.querySelector('.animate-spin')).not.toBeNull();
});
it('FE-MOB-DUPESHEET-006: tells that more show up once these are done', () => {
const more = setup({ hasMore: true });
expect(screen.getByText('More show up once you are through these.')).toBeInTheDocument();
more.unmount();
setup({ hasMore: false });
expect(screen.queryByText('More show up once you are through these.')).not.toBeInTheDocument();
});
it('FE-MOB-DUPESHEET-007: keeping a side merges that pair with that side leading', async () => {
const user = userEvent.setup();
const { d } = setup();
await user.click(screen.getAllByRole('button', { name: /Keep this one/ })[3]);
expect(d.merge).toHaveBeenCalledWith(OTHER, 'b');
});
it('FE-MOB-DUPESHEET-008: "Not the same place" dismisses that pair', async () => {
const user = userEvent.setup();
const { d } = setup();
await user.click(screen.getAllByRole('button', { name: 'Not the same place' })[0]);
expect(d.dismiss).toHaveBeenCalledWith(PAIR);
});
it('FE-MOB-DUPESHEET-009: only the pair being worked on is busy', () => {
setup({ busy: pairKey(OTHER) });
const [first, second] = screen.getAllByRole('article');
for (const button of first.querySelectorAll('button')) expect(button).toBeEnabled();
for (const button of second.querySelectorAll('button')) expect(button).toBeDisabled();
});
it('FE-MOB-DUPESHEET-010: the undo button appears only after a merge and takes it back', async () => {
const user = userEvent.setup();
const none = setup();
expect(screen.queryByRole('button', { name: /Separate/ })).not.toBeInTheDocument();
none.unmount();
const { d } = setup({ lastMerge: { id: 4, name: 'Colosseo' } });
await user.click(screen.getByRole('button', { name: 'Separate Colosseo again' }));
expect(d.undo).toHaveBeenCalledTimes(1);
});
it('FE-MOB-DUPESHEET-011: the undo button is disabled while the undo runs', () => {
setup({ lastMerge: { id: 4, name: 'Colosseo' }, busy: 'undo' });
expect(screen.getByRole('button', { name: 'Separate Colosseo again' })).toBeDisabled();
});
it('FE-MOB-DUPESHEET-012: the close button closes the sheet', async () => {
const user = userEvent.setup();
const { onClose } = setup();
await user.click(screen.getByRole('button', { name: 'Close' }));
expect(onClose).toHaveBeenCalledTimes(1);
});
});