|
| 1 | +import { TodoistApi } from './TodoistApi' |
| 2 | +import { setupRestClientMock } from './testUtils/mocks' |
| 3 | +import { getSyncBaseUri } from './consts/endpoints' |
| 4 | +import axios from 'axios' |
| 5 | +import * as fs from 'fs' |
| 6 | +import { Readable } from 'stream' |
| 7 | + |
| 8 | +// Mock axios |
| 9 | +jest.mock('axios') |
| 10 | +const mockedAxios = axios as jest.Mocked<typeof axios> |
| 11 | + |
| 12 | +// Mock fs |
| 13 | +jest.mock('fs') |
| 14 | +const mockedFs = fs as jest.Mocked<typeof fs> |
| 15 | + |
| 16 | +describe('TodoistApi uploads', () => { |
| 17 | + describe('uploadFile', () => { |
| 18 | + const mockUploadResult = { |
| 19 | + fileUrl: 'https://cdn.todoist.com/uploads/test-file.pdf', |
| 20 | + fileName: 'test-file.pdf', |
| 21 | + fileSize: 1024, |
| 22 | + fileType: 'application/pdf', |
| 23 | + resourceType: 'file', |
| 24 | + uploadState: 'completed' as const, |
| 25 | + image: null, |
| 26 | + imageWidth: null, |
| 27 | + imageHeight: null, |
| 28 | + } |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + jest.clearAllMocks() |
| 32 | + mockedAxios.post.mockResolvedValue({ |
| 33 | + data: mockUploadResult, |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + test('uploads file from Buffer with fileName', async () => { |
| 38 | + const api = new TodoistApi('token') |
| 39 | + const buffer = Buffer.from('test file content') |
| 40 | + |
| 41 | + const result = await api.uploadFile({ |
| 42 | + file: buffer, |
| 43 | + fileName: 'test-file.pdf', |
| 44 | + projectId: '12345', |
| 45 | + }) |
| 46 | + |
| 47 | + expect(mockedAxios.post).toHaveBeenCalledTimes(1) |
| 48 | + const [url, , config] = mockedAxios.post.mock.calls[0] |
| 49 | + |
| 50 | + expect(url).toBe(`${getSyncBaseUri()}uploads`) |
| 51 | + expect(config?.headers?.Authorization).toBe('Bearer token') |
| 52 | + expect(result).toEqual(mockUploadResult) |
| 53 | + }) |
| 54 | + |
| 55 | + test('uploads file from file path', async () => { |
| 56 | + const mockStream = new Readable() |
| 57 | + mockedFs.createReadStream.mockReturnValue(mockStream as fs.ReadStream) |
| 58 | + |
| 59 | + const api = new TodoistApi('token') |
| 60 | + |
| 61 | + const result = await api.uploadFile({ |
| 62 | + file: '/path/to/document.pdf', |
| 63 | + projectId: '12345', |
| 64 | + }) |
| 65 | + |
| 66 | + expect(mockedFs.createReadStream).toHaveBeenCalledWith('/path/to/document.pdf') |
| 67 | + expect(mockedAxios.post).toHaveBeenCalledTimes(1) |
| 68 | + expect(result).toEqual(mockUploadResult) |
| 69 | + }) |
| 70 | + |
| 71 | + test('uploads file from file path with custom fileName', async () => { |
| 72 | + const mockStream = new Readable() |
| 73 | + mockedFs.createReadStream.mockReturnValue(mockStream as fs.ReadStream) |
| 74 | + |
| 75 | + const api = new TodoistApi('token') |
| 76 | + |
| 77 | + await api.uploadFile({ |
| 78 | + file: '/path/to/document.pdf', |
| 79 | + fileName: 'custom-name.pdf', |
| 80 | + }) |
| 81 | + |
| 82 | + expect(mockedFs.createReadStream).toHaveBeenCalledWith('/path/to/document.pdf') |
| 83 | + expect(mockedAxios.post).toHaveBeenCalledTimes(1) |
| 84 | + }) |
| 85 | + |
| 86 | + test('uploads file from stream with fileName', async () => { |
| 87 | + const mockStream = new Readable() |
| 88 | + const api = new TodoistApi('token') |
| 89 | + |
| 90 | + await api.uploadFile({ |
| 91 | + file: mockStream, |
| 92 | + fileName: 'stream-file.pdf', |
| 93 | + }) |
| 94 | + |
| 95 | + expect(mockedAxios.post).toHaveBeenCalledTimes(1) |
| 96 | + }) |
| 97 | + |
| 98 | + test.each([ |
| 99 | + { |
| 100 | + description: 'Buffer', |
| 101 | + file: Buffer.from('test file content'), |
| 102 | + expectedError: 'fileName is required when uploading from a Buffer', |
| 103 | + }, |
| 104 | + { |
| 105 | + description: 'stream', |
| 106 | + file: new Readable(), |
| 107 | + expectedError: 'fileName is required when uploading from a stream', |
| 108 | + }, |
| 109 | + ])( |
| 110 | + 'throws error when $description provided without fileName', |
| 111 | + async ({ file, expectedError }) => { |
| 112 | + const api = new TodoistApi('token') |
| 113 | + |
| 114 | + await expect( |
| 115 | + api.uploadFile({ |
| 116 | + file, |
| 117 | + }), |
| 118 | + ).rejects.toThrow(expectedError) |
| 119 | + }, |
| 120 | + ) |
| 121 | + |
| 122 | + test('uploads file with requestId', async () => { |
| 123 | + const api = new TodoistApi('token') |
| 124 | + const buffer = Buffer.from('test file content') |
| 125 | + const requestId = 'test-request-id' |
| 126 | + |
| 127 | + await api.uploadFile( |
| 128 | + { |
| 129 | + file: buffer, |
| 130 | + fileName: 'test.pdf', |
| 131 | + }, |
| 132 | + requestId, |
| 133 | + ) |
| 134 | + |
| 135 | + expect(mockedAxios.post).toHaveBeenCalledTimes(1) |
| 136 | + const [, , config] = mockedAxios.post.mock.calls[0] |
| 137 | + expect(config?.headers?.['X-Request-Id']).toBe(requestId) |
| 138 | + }) |
| 139 | + |
| 140 | + test('uploads file without projectId', async () => { |
| 141 | + const api = new TodoistApi('token') |
| 142 | + const buffer = Buffer.from('test file content') |
| 143 | + |
| 144 | + await api.uploadFile({ |
| 145 | + file: buffer, |
| 146 | + fileName: 'test.pdf', |
| 147 | + }) |
| 148 | + |
| 149 | + expect(mockedAxios.post).toHaveBeenCalledTimes(1) |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + describe('deleteUpload', () => { |
| 154 | + test('deletes upload successfully', async () => { |
| 155 | + const requestMock = setupRestClientMock('ok', 200) |
| 156 | + const api = new TodoistApi('token') |
| 157 | + |
| 158 | + const result = await api.deleteUpload({ |
| 159 | + fileUrl: 'https://cdn.todoist.com/uploads/test-file.pdf', |
| 160 | + }) |
| 161 | + |
| 162 | + expect(requestMock).toHaveBeenCalledTimes(1) |
| 163 | + expect(requestMock).toHaveBeenCalledWith( |
| 164 | + 'DELETE', |
| 165 | + getSyncBaseUri(), |
| 166 | + 'uploads', |
| 167 | + 'token', |
| 168 | + { |
| 169 | + fileUrl: 'https://cdn.todoist.com/uploads/test-file.pdf', |
| 170 | + }, |
| 171 | + undefined, |
| 172 | + ) |
| 173 | + expect(result).toBe(true) |
| 174 | + }) |
| 175 | + |
| 176 | + test('deletes upload with requestId', async () => { |
| 177 | + const requestMock = setupRestClientMock('ok', 200) |
| 178 | + const api = new TodoistApi('token') |
| 179 | + const requestId = 'test-request-id' |
| 180 | + |
| 181 | + const result = await api.deleteUpload( |
| 182 | + { |
| 183 | + fileUrl: 'https://cdn.todoist.com/uploads/test-file.pdf', |
| 184 | + }, |
| 185 | + requestId, |
| 186 | + ) |
| 187 | + |
| 188 | + expect(requestMock).toHaveBeenCalledTimes(1) |
| 189 | + expect(requestMock).toHaveBeenCalledWith( |
| 190 | + 'DELETE', |
| 191 | + getSyncBaseUri(), |
| 192 | + 'uploads', |
| 193 | + 'token', |
| 194 | + { |
| 195 | + fileUrl: 'https://cdn.todoist.com/uploads/test-file.pdf', |
| 196 | + }, |
| 197 | + requestId, |
| 198 | + ) |
| 199 | + expect(result).toBe(true) |
| 200 | + }) |
| 201 | + }) |
| 202 | +}) |
0 commit comments