-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathimage-paste.test.ts
More file actions
181 lines (142 loc) · 5.17 KB
/
image-paste.test.ts
File metadata and controls
181 lines (142 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Test suite for ImagePasteAddon
*/
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
import { ImagePasteAddon } from './image-paste';
// ============================================================================
// Mock Terminal Implementation
// ============================================================================
class MockTerminal {
public element?: HTMLElement;
public cols = 80;
public rows = 24;
}
// ============================================================================
// Test Suite
// ============================================================================
describe('ImagePasteAddon', () => {
let addon: ImagePasteAddon;
let terminal: MockTerminal;
beforeEach(() => {
addon = new ImagePasteAddon();
terminal = new MockTerminal();
});
afterEach(() => {
addon.dispose();
});
// ==========================================================================
// Activation & Disposal Tests
// ==========================================================================
test('activates successfully', () => {
expect(() => addon.activate(terminal as any)).not.toThrow();
});
test('activates with element and attaches paste listener', () => {
terminal.element = document.createElement('div');
expect(() => addon.activate(terminal as any)).not.toThrow();
});
test('disposes successfully', () => {
addon.activate(terminal as any);
expect(() => addon.dispose()).not.toThrow();
});
test('disposes with element cleans up listener', () => {
terminal.element = document.createElement('div');
addon.activate(terminal as any);
expect(() => addon.dispose()).not.toThrow();
});
test('can activate and dispose multiple times', () => {
addon.activate(terminal as any);
addon.dispose();
addon = new ImagePasteAddon();
addon.activate(terminal as any);
addon.dispose();
});
// ==========================================================================
// Event Tests
// ==========================================================================
test('onImagePaste is a subscribable event', () => {
const disposable = addon.onImagePaste(() => {});
expect(disposable).toBeDefined();
expect(typeof disposable.dispose).toBe('function');
disposable.dispose();
});
test('fires onImagePaste when image is pasted', (done) => {
terminal.element = document.createElement('div');
addon.activate(terminal as any);
addon.onImagePaste((data) => {
expect(data.name).toMatch(/^clipboard_\d+\.png$/);
expect(data.dataBase64).toBe('aW1hZ2VkYXRh');
done();
});
// Create a mock paste event with an image file
const mockFile = new File(['imagedata'], 'test.png', { type: 'image/png' });
// Mock FileReader to return synchronously for testing
const originalFileReader = globalThis.FileReader;
class MockFileReader {
onload: (() => void) | null = null;
result: string | null = null;
readAsDataURL(_file: File) {
this.result = 'data:image/png;base64,aW1hZ2VkYXRh';
if (this.onload) this.onload();
}
}
globalThis.FileReader = MockFileReader as any;
const dataTransfer = new DataTransfer();
dataTransfer.items.add(mockFile);
const pasteEvent = new ClipboardEvent('paste', {
clipboardData: dataTransfer,
bubbles: true,
cancelable: true,
});
terminal.element.dispatchEvent(pasteEvent);
// Restore
globalThis.FileReader = originalFileReader;
});
test('does not fire for non-image pastes', () => {
terminal.element = document.createElement('div');
addon.activate(terminal as any);
let fired = false;
addon.onImagePaste(() => {
fired = true;
});
// Paste event with only text
const dataTransfer = new DataTransfer();
dataTransfer.setData('text/plain', 'hello');
const pasteEvent = new ClipboardEvent('paste', {
clipboardData: dataTransfer,
bubbles: true,
cancelable: true,
});
terminal.element.dispatchEvent(pasteEvent);
expect(fired).toBe(false);
});
test('dispose removes paste listener', () => {
terminal.element = document.createElement('div');
addon.activate(terminal as any);
let fired = false;
addon.onImagePaste(() => {
fired = true;
});
addon.dispose();
// Dispatch after dispose - should not fire
const mockFile = new File(['imagedata'], 'test.png', { type: 'image/png' });
const dataTransfer = new DataTransfer();
dataTransfer.items.add(mockFile);
const pasteEvent = new ClipboardEvent('paste', {
clipboardData: dataTransfer,
bubbles: true,
cancelable: true,
});
terminal.element.dispatchEvent(pasteEvent);
expect(fired).toBe(false);
});
// ==========================================================================
// Integration Tests
// ==========================================================================
test('full workflow: activate → subscribe → dispose', () => {
terminal.element = document.createElement('div');
addon.activate(terminal as any);
const disposable = addon.onImagePaste(() => {});
disposable.dispose();
addon.dispose();
});
});