import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {describe, expect, it, vi} from 'vitest';
import PhotoDialog from './PhotoDialog';
vi.mock('./AddPhotoModal', () => ({
default: ({label, newUrl, onClose}) => (
),
}));
function setup() {
render(
<>
Add photo
Add garden photo
About
>,
);
}
describe('PhotoDialog', () => {
it('draws nothing to start with', () => {
setup();
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});
it('opens from an add photo link', async () => {
setup();
await userEvent.click(screen.getByRole('link', {name: 'Add photo'}));
expect(screen.getByRole('dialog')).toBeInTheDocument();
});
it('names the record from the link', async () => {
setup();
await userEvent.click(screen.getByRole('link', {name: 'Add photo'}));
expect(screen.getByRole('dialog', {name: 'Tomato planting'})).toBeInTheDocument();
});
it('works from the link\'s own href', async () => {
setup();
await userEvent.click(screen.getByRole('link', {name: 'Add photo'}));
expect(screen.getByRole('dialog')).toHaveAttribute('data-url', '/photos/new?type=planting&id=5');
});
it('falls back to its own label', async () => {
setup();
await userEvent.click(screen.getByRole('link', {name: 'Add garden photo'}));
expect(screen.getByRole('dialog', {name: 'Fallback'})).toBeInTheDocument();
});
it('ignores other links', async () => {
setup();
await userEvent.click(screen.getByRole('link', {name: 'About'}));
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});
it('closes', async () => {
setup();
await userEvent.click(screen.getByRole('link', {name: 'Add photo'}));
await userEvent.click(screen.getByRole('button', {name: 'Close'}));
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});
});