@@ -18,9 +18,20 @@ const props = [
18
18
19
19
const methods = [ 'on' , 'save' , 'drawImage' ] ;
20
20
21
+ const loadImageAsync = async ( name ) => {
22
+ const image = new Image ( ) ;
23
+ image . src = `${ __dirname } /${ name } ` ;
24
+
25
+ await new Promise ( ( res , rej ) => {
26
+ image . on ( 'load' , ( ) => res ( ) ) ;
27
+ image . on ( 'error' , rej ) ;
28
+ } ) ;
29
+
30
+ return image ;
31
+ } ;
32
+
21
33
22
34
describe ( 'Image' , ( ) => {
23
-
24
35
it ( 'exports an object' , ( ) => {
25
36
assert . strictEqual ( typeof Image , 'function' ) ;
26
37
} ) ;
@@ -46,7 +57,6 @@ describe('Image', () => {
46
57
47
58
48
59
it ( 'emits "load" for the early listener' , async ( ) => {
49
-
50
60
const image = new Image ( ) ;
51
61
52
62
const loaded = await new Promise ( ( res , rej ) => {
@@ -56,33 +66,22 @@ describe('Image', () => {
56
66
} ) ;
57
67
58
68
assert . ok ( loaded ) ;
59
-
60
69
} ) ;
61
70
62
71
63
72
it ( 'emits "load" for the late listener' , async ( ) => {
64
-
65
- const image = new Image ( ) ;
66
- image . src = `${ __dirname } /freeimage.jpg` ;
67
-
68
- // Async barrier
69
- await new Promise ( ( res , rej ) => {
70
- image . on ( 'load' , ( ) => res ( true ) ) ;
71
- image . on ( 'error' , rej ) ;
72
- } ) ;
73
+ const image = await loadImageAsync ( 'freeimage.jpg' ) ;
73
74
74
75
const loaded = await new Promise ( ( res , rej ) => {
75
76
image . on ( 'load' , ( ) => res ( true ) ) ;
76
77
image . on ( 'error' , rej ) ;
77
78
} ) ;
78
79
79
80
assert . ok ( loaded ) ;
80
-
81
81
} ) ;
82
82
83
83
84
84
it ( 'early #addEventListener() calls back with the correct scope' , async ( ) => {
85
-
86
85
const image = new Image ( ) ;
87
86
88
87
const that = await new Promise ( ( res , rej ) => {
@@ -92,27 +91,22 @@ describe('Image', () => {
92
91
} ) ;
93
92
94
93
assert . strictEqual ( that , image ) ;
95
-
96
94
} ) ;
97
95
98
96
99
97
it ( 'late #addEventListener() calls back with the correct scope' , async ( ) => {
100
-
101
- const image = new Image ( ) ;
102
- image . src = `${ __dirname } /freeimage.jpg` ;
98
+ const image = await loadImageAsync ( 'freeimage.jpg' ) ;
103
99
104
100
const that = await new Promise ( ( res , rej ) => {
105
101
image . addEventListener ( 'load' , function ( ) { res ( this ) ; } ) ;
106
102
image . addEventListener ( 'error' , rej ) ;
107
103
} ) ;
108
104
109
105
assert . strictEqual ( that , image ) ;
110
-
111
106
} ) ;
112
107
113
108
114
109
it ( 'early #on() calls back with the correct scope' , async ( ) => {
115
-
116
110
const image = new Image ( ) ;
117
111
118
112
const that = await new Promise ( ( res , rej ) => {
@@ -122,27 +116,22 @@ describe('Image', () => {
122
116
} ) ;
123
117
124
118
assert . strictEqual ( that , image ) ;
125
-
126
119
} ) ;
127
120
128
121
129
122
it ( 'late #on() calls back with the correct scope' , async ( ) => {
130
-
131
- const image = new Image ( ) ;
132
- image . src = `${ __dirname } /freeimage.jpg` ;
123
+ const image = await loadImageAsync ( 'freeimage.jpg' ) ;
133
124
134
125
const that = await new Promise ( ( res , rej ) => {
135
126
image . on ( 'load' , function ( ) { res ( this ) ; } ) ;
136
127
image . on ( 'error' , rej ) ;
137
128
} ) ;
138
129
139
130
assert . strictEqual ( that , image ) ;
140
-
141
131
} ) ;
142
132
143
133
144
134
it ( 'early #once() calls back with the correct scope' , async ( ) => {
145
-
146
135
const image = new Image ( ) ;
147
136
148
137
const that = await new Promise ( ( res , rej ) => {
@@ -152,118 +141,83 @@ describe('Image', () => {
152
141
} ) ;
153
142
154
143
assert . strictEqual ( that , image ) ;
155
-
156
144
} ) ;
157
145
158
146
159
147
it ( 'late #once() calls back with the correct scope' , async ( ) => {
160
-
161
- const image = new Image ( ) ;
162
- image . src = `${ __dirname } /freeimage.jpg` ;
148
+ const image = await loadImageAsync ( 'freeimage.jpg' ) ;
163
149
164
150
const that = await new Promise ( ( res , rej ) => {
165
151
image . once ( 'load' , function ( ) { res ( this ) ; } ) ;
166
152
image . once ( 'error' , rej ) ;
167
153
} ) ;
168
154
169
155
assert . strictEqual ( that , image ) ;
170
-
171
156
} ) ;
172
157
173
-
174
- it ( 'has accessible data' , async ( ) => {
158
+ it ( 'has accessible JPG data' , async ( ) => {
159
+ const image = await loadImageAsync ( 'freeimage.jpg' ) ;
175
160
176
- const image = new Image ( ) ;
177
- image . src = `${ __dirname } /freeimage.jpg` ;
178
-
179
- const data = await new Promise ( ( res , rej ) => {
180
- image . on ( 'load' , ( ) => res ( image . data ) ) ;
181
- image . on ( 'error' , rej ) ;
182
- } ) ;
161
+ assert . ok ( ! ! image . data ) ;
162
+ assert . strictEqual ( image . data . length , TEST_IMAGE_LENGTH ) ;
163
+ } ) ;
164
+
165
+ it ( 'has accessible PNG data' , async ( ) => {
166
+ const image = await loadImageAsync ( 'freeimage.png' ) ;
183
167
184
- assert . ok ( ! ! data ) ;
185
- assert . strictEqual ( data . length , TEST_IMAGE_LENGTH ) ;
168
+ assert . ok ( ! ! image . data ) ;
169
+ assert . strictEqual ( image . data . length , TEST_IMAGE_LENGTH ) ;
170
+ } ) ;
171
+
172
+ it ( 'has accessible GIF data' , async ( ) => {
173
+ const image = await loadImageAsync ( 'freeimage.gif' ) ;
186
174
175
+ assert . ok ( ! ! image . data ) ;
176
+ assert . strictEqual ( image . data . length , TEST_IMAGE_LENGTH ) ;
187
177
} ) ;
188
178
189
179
190
180
it ( 'has correct dimensions' , async ( ) => {
191
-
192
- const image = new Image ( ) ;
193
- image . src = `${ __dirname } /freeimage.jpg` ;
194
-
195
- await new Promise ( ( res , rej ) => {
196
- image . on ( 'load' , res ) ;
197
- image . on ( 'error' , rej ) ;
198
- } ) ;
181
+ const image = await loadImageAsync ( 'freeimage.jpg' ) ;
199
182
200
183
assert . strictEqual ( image . width , TEST_IMAGE_WIDTH ) ;
201
184
assert . strictEqual ( image . naturalWidth , TEST_IMAGE_WIDTH ) ;
202
185
203
186
assert . strictEqual ( image . height , TEST_IMAGE_HEIGHT ) ;
204
187
assert . strictEqual ( image . naturalHeight , TEST_IMAGE_HEIGHT ) ;
205
-
206
188
} ) ;
207
189
208
190
209
191
it ( 'has correct `complete` when empty' , async ( ) => {
210
-
211
192
const image = new Image ( ) ;
212
-
213
193
assert . strictEqual ( image . complete , false ) ;
214
-
215
194
} ) ;
216
195
217
196
218
197
it ( 'has correct `complete` when loaded' , async ( ) => {
219
-
220
- const image = new Image ( ) ;
221
- image . src = `${ __dirname } /freeimage.jpg` ;
222
-
223
- await new Promise ( ( res , rej ) => {
224
- image . on ( 'load' , res ) ;
225
- image . on ( 'error' , rej ) ;
226
- } ) ;
227
-
198
+ const image = await loadImageAsync ( 'freeimage.jpg' ) ;
228
199
assert . ok ( image . complete ) ;
229
-
230
200
} ) ;
231
201
232
- const setSrc = ( image , src ) => { image . src = src ; } ;
202
+
233
203
it ( 'has correct `complete` after dropping `src`' , async ( ) => {
234
-
235
- const image = new Image ( ) ;
236
- setSrc ( image , `${ __dirname } /freeimage.jpg` ) ;
204
+ const image = await loadImageAsync ( 'freeimage.jpg' ) ;
237
205
238
206
let status = '' ;
239
207
image . on ( 'load' , ( ) => status += image . complete ) ;
240
208
241
- await new Promise ( ( res , rej ) => {
242
- image . once ( 'load' , res ) ;
243
- image . once ( 'error' , rej ) ;
244
- } ) ;
245
-
246
209
assert . ok ( image . complete ) ;
247
210
248
- setSrc ( image , '' ) ;
249
-
211
+ image . src = '' ;
250
212
await new Promise ( ( res ) => setTimeout ( res , 10 ) ) ;
251
213
252
- assert . strictEqual ( image . complete , false ) ;
214
+ assert . ok ( ! image . complete ) ;
253
215
assert . strictEqual ( status , 'truefalse' ) ;
254
-
255
216
} ) ;
256
217
257
218
258
219
it ( 'can draw a stretched image' , async ( ) => {
259
-
260
- const src = new Image ( ) ;
261
- src . src = `${ __dirname } /freeimage.jpg` ;
262
-
263
- await new Promise ( ( res , rej ) => {
264
- src . once ( 'load' , res ) ;
265
- src . once ( 'error' , rej ) ;
266
- } ) ;
220
+ const src = await loadImageAsync ( 'freeimage.jpg' ) ;
267
221
268
222
const dest = new Image ( ) ;
269
223
dest . drawImage (
@@ -274,7 +228,5 @@ describe('Image', () => {
274
228
275
229
assert . strictEqual ( dest . width , TEST_STRETCH_WIDTH ) ;
276
230
assert . strictEqual ( dest . height , TEST_STRETCH_HEIGHT ) ;
277
-
278
231
} ) ;
279
-
280
232
} ) ;
0 commit comments