import React from 'react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
vi.mock('@/api/dms', () => ({
shareFile: vi.fn().mockResolvedValue({}),
removeShare: vi.fn(),
}));
vi.mock('@/api/permissions', () => ({
fetchFilePermissions: vi.fn().mockResolvedValue([]),
grantPermission: vi.fn(),
revokePermission: vi.fn(),
createShareLink: vi.fn(),
revokeShareLink: vi.fn(),
}));
vi.mock('@/components/ui/Toast', () => ({
useToast: () => ({ success: vi.fn(), error: vi.fn(), info: vi.fn(), warning: vi.fn() }),
}));
import { ShareDialog } from '@/components/dms/ShareDialog';
import { shareFile } from '@/api/dms';
beforeEach(() => {
vi.clearAllMocks();
});
describe('ShareDialog validation (RHF + Zod)', () => {
it('shows validation error when share ID is empty', async () => {
render(
);
// Find the submit button inside the add-share form
const addShareButtons = screen.getAllByRole('button', { name: /hinzufügen|add share/i });
const submitBtn = addShareButtons.find((b) => b.getAttribute('type') === 'submit');
expect(submitBtn).toBeTruthy();
fireEvent.click(submitBtn!);
await waitFor(() => {
const errors = screen.getAllByText(/erforderlich|required/i);
expect(errors.length).toBeGreaterThan(0);
});
});
it('calls shareFile when form is valid', async () => {
render(
);
// Fill in share ID - find the input inside the share-add-section
const shareSection = screen.getByTestId('share-add-section');
const input = shareSection.querySelector('input');
expect(input).toBeTruthy();
fireEvent.change(input!, { target: { value: 'user-123' } });
fireEvent.submit(input!.closest('form')!);
await waitFor(() => {
expect(shareFile).toHaveBeenCalledWith('f1', expect.objectContaining({ user_id: 'user-123' }));
}, { timeout: 3000 });
});
});