Phase 6.4: DMS-Forms on RHF + Zod
- Dms.tsx folder-create: RHF+Zod (name required) - ShareDialog add-share: RHF+Zod (shareId required, shareType/permission kept as useState) - Error display under each field - Preserved all existing functionality: folder tree, file grid, share links - Added 2 validation tests for ShareDialog (empty shareId, valid submit)
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
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(
|
||||
<ShareDialog
|
||||
open
|
||||
file={{ id: 'f1', name: 'test.pdf', folder_id: null, mime_type: 'application/pdf', size_bytes: 100, created_at: '', updated_at: '', created_by: '' } as any}
|
||||
onClose={vi.fn()}
|
||||
onShared={vi.fn()}
|
||||
/>
|
||||
);
|
||||
// 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(
|
||||
<ShareDialog
|
||||
open
|
||||
file={{ id: 'f1', name: 'test.pdf', folder_id: null, mime_type: 'application/pdf', size_bytes: 100, created_at: '', updated_at: '', created_by: '' } as any}
|
||||
onClose={vi.fn()}
|
||||
onShared={vi.fn()}
|
||||
/>
|
||||
);
|
||||
// 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 });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user