|
| 1 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { useState } from 'react'; |
| 5 | + |
| 6 | +import FileSidebar from './FileSidebar'; |
| 7 | +import { useNotesManager } from '../hooks/useNotesManager'; |
| 8 | + |
| 9 | +const { dbGetMock, dbSetMock, fileImportMock, fileExportMock } = vi.hoisted(() => ({ |
| 10 | + dbGetMock: vi.fn(), |
| 11 | + dbSetMock: vi.fn(), |
| 12 | + fileImportMock: vi.fn(), |
| 13 | + fileExportMock: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('idb-keyval', () => ({ |
| 17 | + get: dbGetMock, |
| 18 | + set: dbSetMock, |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('../services/fileService', () => ({ |
| 22 | + fileService: { |
| 23 | + importFile: fileImportMock, |
| 24 | + exportFile: fileExportMock, |
| 25 | + }, |
| 26 | +})); |
| 27 | + |
| 28 | +function FileSidebarExportHarness() { |
| 29 | + const [snackbar, setSnackbar] = useState<{ |
| 30 | + open: boolean; |
| 31 | + message: string; |
| 32 | + severity: 'success' | 'error'; |
| 33 | + }>({ |
| 34 | + open: false, |
| 35 | + message: '', |
| 36 | + severity: 'success', |
| 37 | + }); |
| 38 | + |
| 39 | + const { |
| 40 | + notes, |
| 41 | + activeNoteId, |
| 42 | + setActiveNoteId, |
| 43 | + isLoaded, |
| 44 | + handleCreateNote, |
| 45 | + handleDeleteNote, |
| 46 | + handleRenameNote, |
| 47 | + handleImportNote, |
| 48 | + handleExportNote, |
| 49 | + } = useNotesManager(setSnackbar); |
| 50 | + |
| 51 | + if (!isLoaded) { |
| 52 | + return <div>Caricamento note...</div>; |
| 53 | + } |
| 54 | + |
| 55 | + const activeNote = notes.find((note) => note.id === activeNoteId); |
| 56 | + |
| 57 | + return ( |
| 58 | + <> |
| 59 | + <FileSidebar |
| 60 | + notes={notes} |
| 61 | + activeId={activeNoteId} |
| 62 | + onSelect={setActiveNoteId} |
| 63 | + onCreate={handleCreateNote} |
| 64 | + onDelete={handleDeleteNote} |
| 65 | + onRename={handleRenameNote} |
| 66 | + onImport={handleImportNote} |
| 67 | + onExport={handleExportNote} |
| 68 | + /> |
| 69 | + <h2>{activeNote?.title}</h2> |
| 70 | + <p>{snackbar.open ? snackbar.message : ''}</p> |
| 71 | + </> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +describe('FileSidebar integration (export flow)', () => { |
| 76 | + beforeEach(() => { |
| 77 | + vi.clearAllMocks(); |
| 78 | + dbSetMock.mockResolvedValue(undefined); |
| 79 | + fileImportMock.mockResolvedValue(null); |
| 80 | + fileExportMock.mockResolvedValue(undefined); |
| 81 | + }); |
| 82 | + |
| 83 | + it('esporta la nota attiva quando clicco il bottone salva su disco', async () => { |
| 84 | + const user = userEvent.setup(); |
| 85 | + dbGetMock.mockResolvedValue([ |
| 86 | + { id: 'note-1', title: 'Nota Uno', content: 'Contenuto uno', createdAt: 1 }, |
| 87 | + ]); |
| 88 | + |
| 89 | + render(<FileSidebarExportHarness />); |
| 90 | + |
| 91 | + await screen.findByRole('heading', { name: 'Nota Uno' }); |
| 92 | + await user.click(screen.getByTitle(/salva nota su disco/i)); |
| 93 | + |
| 94 | + await waitFor(() => |
| 95 | + expect(fileExportMock).toHaveBeenCalledWith('Nota Uno', 'Contenuto uno'), |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('se cambio nota attiva dalla lista, esporta la nuova nota selezionata', async () => { |
| 100 | + const user = userEvent.setup(); |
| 101 | + dbGetMock.mockResolvedValue([ |
| 102 | + { id: 'note-1', title: 'Nota Uno', content: 'Contenuto uno', createdAt: 1 }, |
| 103 | + { id: 'note-2', title: 'Nota Due', content: 'Contenuto due', createdAt: 2 }, |
| 104 | + ]); |
| 105 | + |
| 106 | + render(<FileSidebarExportHarness />); |
| 107 | + |
| 108 | + await screen.findByRole('heading', { name: 'Nota Uno' }); |
| 109 | + await user.click(screen.getByText('Nota Due')); |
| 110 | + await screen.findByRole('heading', { name: 'Nota Due' }); |
| 111 | + |
| 112 | + await user.click(screen.getByTitle(/salva nota su disco/i)); |
| 113 | + await waitFor(() => |
| 114 | + expect(fileExportMock).toHaveBeenCalledWith('Nota Due', 'Contenuto due'), |
| 115 | + ); |
| 116 | + }); |
| 117 | +}); |
0 commit comments