|
| 1 | +/** |
| 2 | + * Tests for ObjectStackAdapter file upload integration |
| 3 | + */ |
| 4 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 5 | +import { ObjectStackAdapter } from './index'; |
| 6 | + |
| 7 | +describe('ObjectStackAdapter File Upload', () => { |
| 8 | + let adapter: ObjectStackAdapter; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + adapter = new ObjectStackAdapter({ |
| 12 | + baseUrl: 'http://localhost:3000', |
| 13 | + autoReconnect: false, |
| 14 | + }); |
| 15 | + vi.clearAllMocks(); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('uploadFile', () => { |
| 19 | + it('should be a method on the adapter', () => { |
| 20 | + expect(typeof adapter.uploadFile).toBe('function'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should call fetch with multipart form data when connected', async () => { |
| 24 | + const mockResponse = { |
| 25 | + ok: true, |
| 26 | + json: vi.fn().mockResolvedValue({ |
| 27 | + id: 'file-1', |
| 28 | + filename: 'test.pdf', |
| 29 | + mimeType: 'application/pdf', |
| 30 | + size: 1024, |
| 31 | + url: 'http://localhost:3000/files/file-1', |
| 32 | + }), |
| 33 | + }; |
| 34 | + |
| 35 | + global.fetch = vi.fn().mockResolvedValue(mockResponse); |
| 36 | + |
| 37 | + // Manually set connected state by accessing private field |
| 38 | + (adapter as any).connected = true; |
| 39 | + (adapter as any).connectionState = 'connected'; |
| 40 | + |
| 41 | + const file = new File(['test content'], 'test.pdf', { type: 'application/pdf' }); |
| 42 | + |
| 43 | + const result = await adapter.uploadFile('documents', file, { |
| 44 | + recordId: 'rec-123', |
| 45 | + fieldName: 'attachment', |
| 46 | + }); |
| 47 | + |
| 48 | + expect(global.fetch).toHaveBeenCalledWith( |
| 49 | + expect.stringContaining('/api/data/documents/upload'), |
| 50 | + expect.objectContaining({ |
| 51 | + method: 'POST', |
| 52 | + body: expect.any(FormData), |
| 53 | + }), |
| 54 | + ); |
| 55 | + |
| 56 | + expect(result.id).toBe('file-1'); |
| 57 | + expect(result.filename).toBe('test.pdf'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should throw on upload failure', async () => { |
| 61 | + const mockResponse = { |
| 62 | + ok: false, |
| 63 | + status: 413, |
| 64 | + statusText: 'Payload Too Large', |
| 65 | + json: vi.fn().mockResolvedValue({ message: 'File too large' }), |
| 66 | + }; |
| 67 | + |
| 68 | + global.fetch = vi.fn().mockResolvedValue(mockResponse); |
| 69 | + |
| 70 | + // Manually set connected state |
| 71 | + (adapter as any).connected = true; |
| 72 | + (adapter as any).connectionState = 'connected'; |
| 73 | + |
| 74 | + const file = new File(['test'], 'large.bin', { type: 'application/octet-stream' }); |
| 75 | + |
| 76 | + await expect(adapter.uploadFile('documents', file)).rejects.toThrow('File too large'); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('uploadFiles', () => { |
| 81 | + it('should be a method on the adapter', () => { |
| 82 | + expect(typeof adapter.uploadFiles).toBe('function'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should upload multiple files', async () => { |
| 86 | + const mockResponse = { |
| 87 | + ok: true, |
| 88 | + json: vi.fn().mockResolvedValue([ |
| 89 | + { id: 'file-1', filename: 'a.pdf', mimeType: 'application/pdf', size: 100, url: '/files/1' }, |
| 90 | + { id: 'file-2', filename: 'b.pdf', mimeType: 'application/pdf', size: 200, url: '/files/2' }, |
| 91 | + ]), |
| 92 | + }; |
| 93 | + |
| 94 | + global.fetch = vi.fn().mockResolvedValue(mockResponse); |
| 95 | + |
| 96 | + // Manually set connected state |
| 97 | + (adapter as any).connected = true; |
| 98 | + (adapter as any).connectionState = 'connected'; |
| 99 | + |
| 100 | + const files = [ |
| 101 | + new File(['content1'], 'a.pdf', { type: 'application/pdf' }), |
| 102 | + new File(['content2'], 'b.pdf', { type: 'application/pdf' }), |
| 103 | + ]; |
| 104 | + |
| 105 | + const results = await adapter.uploadFiles('documents', files); |
| 106 | + |
| 107 | + expect(results).toHaveLength(2); |
| 108 | + expect(results[0].id).toBe('file-1'); |
| 109 | + expect(results[1].id).toBe('file-2'); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments