import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { vi, test, expect } from 'vitest'; import LinkRules, { expiryText } from './LinkRules.jsx'; import SharedPasscodeForm from './SharedPasscodeForm.jsx'; test('says when a link expires and whether it has a passcode', () => { const now = Date.UTC(2026, 9, 6); expect(expiryText(null, now)).toBe('Never expires'); expect(expiryText(now - 1, now)).toBe('Expired'); expect(expiryText(now + 3 * 86_400_000, now)).toMatch(/3 days left/); render(); expect(screen.getByText(/Needs a passcode/)).toBeInTheDocument(); }); test('only the settings you change are sent', async () => { const onSave = vi.fn(async () => true); const user = userEvent.setup(); render(); const save = screen.getByRole('button', { name: 'Save settings' }); expect(save).toBeDisabled(); await user.selectOptions(screen.getByLabelText('Stop working'), '7'); await user.click(save); expect(onSave).toHaveBeenLastCalledWith({ expiresInDays: 7 }); // no passcode key: the passcode is left alone await user.type(screen.getByLabelText(/Passcode/), ' moses '); await user.click(screen.getByRole('button', { name: 'Save settings' })); expect(onSave).toHaveBeenLastCalledWith({ passcode: 'moses' }); }); test('a passcode can be removed, and "Never" clears the expiry', async () => { const onSave = vi.fn(async () => true); const user = userEvent.setup(); render(); await user.click(screen.getByRole('button', { name: 'Remove passcode' })); expect(onSave).toHaveBeenLastCalledWith({ passcode: null }); await user.selectOptions(screen.getByLabelText('Stop working'), 'Never'); await user.click(screen.getByRole('button', { name: 'Save settings' })); expect(onSave).toHaveBeenLastCalledWith({ expiresInDays: null }); }); test('the viewer\'s passcode form submits what was typed and shows a wrong-passcode message', async () => { const onSubmit = vi.fn(); const user = userEvent.setup(); render(); expect(screen.getByRole('alert')).toHaveTextContent(/not right/); expect(screen.getByRole('button', { name: 'Open' })).toBeDisabled(); await user.type(screen.getByLabelText('Passcode'), 'moses'); await user.click(screen.getByRole('button', { name: 'Open' })); expect(onSubmit).toHaveBeenCalledWith('moses'); });