|
1 | 1 | import { describe, it, expect, beforeEach, vi, Mock } from 'vitest'; |
2 | 2 | import { |
| 3 | + appendAttachment, |
3 | 4 | BugSplat, |
4 | 5 | createStandardizedCallStack, |
5 | 6 | tryParseResponseJson, |
@@ -369,3 +370,65 @@ describe('BugSplat', function () { |
369 | 370 | expect(result.error!.message).toEqual('BugSplat Error: Invalid response received'); |
370 | 371 | }); |
371 | 372 | }); |
| 373 | + |
| 374 | +describe('appendAttachment', () => { |
| 375 | + let append: Mock; |
| 376 | + let body: FormData; |
| 377 | + |
| 378 | + beforeEach(() => { |
| 379 | + append = vi.fn(); |
| 380 | + body = { append } as unknown as FormData; |
| 381 | + }); |
| 382 | + |
| 383 | + it('wraps a Uint8Array in a Blob before appending', () => { |
| 384 | + const bytes = new Uint8Array([0x42, 0x53]); |
| 385 | + appendAttachment(body, { filename: 'data.bin', data: bytes }); |
| 386 | + expect(append).toHaveBeenCalledTimes(1); |
| 387 | + const [filename, value, postedName] = append.mock.calls[0]; |
| 388 | + expect(filename).toBe('data.bin'); |
| 389 | + expect(value).toBeInstanceOf(Blob); |
| 390 | + expect(postedName).toBe('data.bin'); |
| 391 | + }); |
| 392 | + |
| 393 | + it('uploads only the bytes covered by a Uint8Array view, not the whole backing buffer', async () => { |
| 394 | + // Build a Uint8Array that is a subarray view into a larger buffer. |
| 395 | + const underlying = new Uint8Array([0x00, 0x00, 0x42, 0x53, 0x00, 0x00]); |
| 396 | + const view = underlying.subarray(2, 4); // just [0x42, 0x53] |
| 397 | + appendAttachment(body, { filename: 'data.bin', data: view }); |
| 398 | + const [, value] = append.mock.calls[0]; |
| 399 | + expect(value).toBeInstanceOf(Blob); |
| 400 | + expect((value as Blob).size).toBe(2); |
| 401 | + const bytes = new Uint8Array(await (value as Blob).arrayBuffer()); |
| 402 | + expect(Array.from(bytes)).toEqual([0x42, 0x53]); |
| 403 | + }); |
| 404 | + |
| 405 | + it('passes a Blob through with filename', () => { |
| 406 | + const blob = new Blob(['hello']); |
| 407 | + appendAttachment(body, { filename: 'hello.txt', data: blob }); |
| 408 | + expect(append).toHaveBeenCalledWith('hello.txt', blob, 'hello.txt'); |
| 409 | + }); |
| 410 | + |
| 411 | + it('appends a React Native file ref in {uri, name, type} shape', () => { |
| 412 | + appendAttachment(body, { |
| 413 | + filename: 'screenshot.png', |
| 414 | + data: { uri: 'file:///tmp/shot.png', type: 'image/png' }, |
| 415 | + }); |
| 416 | + expect(append).toHaveBeenCalledWith( |
| 417 | + 'screenshot.png', |
| 418 | + { uri: 'file:///tmp/shot.png', type: 'image/png', name: 'screenshot.png' }, |
| 419 | + 'screenshot.png' |
| 420 | + ); |
| 421 | + }); |
| 422 | + |
| 423 | + it('handles a file ref with no type', () => { |
| 424 | + appendAttachment(body, { |
| 425 | + filename: 'log.txt', |
| 426 | + data: { uri: 'file:///tmp/log.txt' }, |
| 427 | + }); |
| 428 | + expect(append).toHaveBeenCalledWith( |
| 429 | + 'log.txt', |
| 430 | + { uri: 'file:///tmp/log.txt', type: undefined, name: 'log.txt' }, |
| 431 | + 'log.txt' |
| 432 | + ); |
| 433 | + }); |
| 434 | +}); |
0 commit comments