import { describe, it, expect, vi, beforeEach } from 'vitest' import { useEntityDocuments } from '~/composables/useEntityDocuments' // --------------------------------------------------------------------------- // Mocks // --------------------------------------------------------------------------- const mockLoadDocumentsByPiece = vi.fn() const mockLoadDocumentsByComponent = vi.fn() vi.mock('~/composables/useDocuments', () => ({ useDocuments: () => ({ loadDocumentsByPiece: mockLoadDocumentsByPiece, loadDocumentsByComponent: mockLoadDocumentsByComponent, uploadDocuments: vi.fn(), deleteDocument: vi.fn(), updateDocument: vi.fn(), }), })) vi.mock('~/utils/documentPreview', () => ({ canPreviewDocument: () => true, })) beforeEach(() => { vi.clearAllMocks() }) // --------------------------------------------------------------------------- // refreshDocuments — pending / orphan entities // --------------------------------------------------------------------------- describe('refreshDocuments', () => { it('does NOT load documents for a pending piece node (orphan link id is not a piece id)', async () => { // A category-only / pending piece node: its `id` is the machinePieceLink id, // there is no real piece behind it (pieceId is null). const pendingNode = { id: 'cl48179803369dd93b4a90b784', // machinePieceLink id, NOT a piece id pieceId: null, pendingEntity: true, documents: [], } const { refreshDocuments } = useEntityDocuments({ entity: () => pendingNode, entityType: 'piece', }) await refreshDocuments() expect(mockLoadDocumentsByPiece).not.toHaveBeenCalled() }) it('loads documents for a real piece node using its piece id', async () => { mockLoadDocumentsByPiece.mockResolvedValue({ success: true, data: [] }) const realNode = { id: 'clrealpieceid000000000000', pieceId: 'clrealpieceid000000000000', pendingEntity: false, documents: [], } const { refreshDocuments } = useEntityDocuments({ entity: () => realNode, entityType: 'piece', }) await refreshDocuments() expect(mockLoadDocumentsByPiece).toHaveBeenCalledWith('clrealpieceid000000000000', { updateStore: false }) }) })