|
1 | 1 | import { extractFileFromDragEvent } from '@/utils/eventUtils' |
2 | 2 | import { describe, expect, it } from 'vitest' |
3 | 3 |
|
4 | | -function makeDragOptions(options: Partial<DragEventInit> = {}): DragEventInit { |
5 | | - return { |
6 | | - bubbles: true, |
7 | | - cancelable: true, |
8 | | - ...options |
9 | | - } |
10 | | -} |
11 | 4 | describe('eventUtils', () => { |
12 | 5 | describe('extractFileFromDragEvent', () => { |
13 | 6 | it('should handle drops with no data', async () => { |
14 | | - const actual = await extractFileFromDragEvent(new DragEvent('drop')) |
| 7 | + const actual = await extractFileFromDragEvent(new FakeDragEvent('drop')) |
15 | 8 | expect(actual).toBe(undefined) |
16 | 9 | }) |
17 | 10 |
|
18 | 11 | it('should handle drops with dataTransfer but no files', async () => { |
19 | 12 | const actual = await extractFileFromDragEvent( |
20 | | - new DragEvent( |
21 | | - 'drop', |
22 | | - makeDragOptions({ dataTransfer: new DataTransfer() }) |
23 | | - ) |
| 13 | + new FakeDragEvent('drop', { dataTransfer: new DataTransfer() }) |
24 | 14 | ) |
25 | 15 | expect(actual).toBe(undefined) |
26 | 16 | }) |
27 | 17 |
|
28 | | - it('should handle drops with no data', async () => { |
29 | | - const actual = await extractFileFromDragEvent(new DragEvent('drop')) |
30 | | - expect(actual).toBe(undefined) |
| 18 | + it('should handle drops with dataTransfer with files', async () => { |
| 19 | + const fileWithWorkflowMaybeWhoKnows = new File( |
| 20 | + [new Uint8Array()], |
| 21 | + 'fake_workflow.json', |
| 22 | + { |
| 23 | + type: 'application/json' |
| 24 | + } |
| 25 | + ) |
| 26 | + |
| 27 | + const dataTransfer = new DataTransfer() |
| 28 | + dataTransfer.items.add(fileWithWorkflowMaybeWhoKnows) |
| 29 | + |
| 30 | + const event = new FakeDragEvent('drop', { dataTransfer }) |
| 31 | + |
| 32 | + const actual = await extractFileFromDragEvent(event) |
| 33 | + expect(actual).toBe(fileWithWorkflowMaybeWhoKnows) |
| 34 | + }) |
| 35 | + |
| 36 | + // Skip until we can setup MSW |
| 37 | + it.skip('should handle drops with URLs', async () => { |
| 38 | + const urlWithWorkflow = 'https://fakewebsite.notreal/fake_workflow.json' |
| 39 | + |
| 40 | + const dataTransfer = new DataTransfer() |
| 41 | + |
| 42 | + dataTransfer.setData('text/uri-list', urlWithWorkflow) |
| 43 | + dataTransfer.setData('text/x-moz-url', urlWithWorkflow) |
| 44 | + |
| 45 | + const event = new FakeDragEvent('drop', { dataTransfer }) |
| 46 | + |
| 47 | + const actual = await extractFileFromDragEvent(event) |
| 48 | + expect(actual).toBeInstanceOf(File) |
31 | 49 | }) |
32 | 50 | }) |
33 | 51 | }) |
| 52 | + |
| 53 | +// Needed to keep the dataTransfer defined |
| 54 | +class FakeDragEvent extends DragEvent { |
| 55 | + override dataTransfer: DataTransfer | null |
| 56 | + override clientX: number |
| 57 | + override clientY: number |
| 58 | + |
| 59 | + constructor( |
| 60 | + type: string, |
| 61 | + { dataTransfer, clientX, clientY }: DragEventInit = {} |
| 62 | + ) { |
| 63 | + super(type) |
| 64 | + this.dataTransfer = dataTransfer ?? null |
| 65 | + this.clientX = clientX ?? 0 |
| 66 | + this.clientY = clientY ?? 0 |
| 67 | + } |
| 68 | +} |
0 commit comments