@@ -18,9 +18,20 @@ const props = [
1818
1919const methods = [ 'on' , 'save' , 'drawImage' ] ;
2020
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+
2133
2234describe ( 'Image' , ( ) => {
23-
2435 it ( 'exports an object' , ( ) => {
2536 assert . strictEqual ( typeof Image , 'function' ) ;
2637 } ) ;
@@ -46,7 +57,6 @@ describe('Image', () => {
4657
4758
4859 it ( 'emits "load" for the early listener' , async ( ) => {
49-
5060 const image = new Image ( ) ;
5161
5262 const loaded = await new Promise ( ( res , rej ) => {
@@ -56,33 +66,22 @@ describe('Image', () => {
5666 } ) ;
5767
5868 assert . ok ( loaded ) ;
59-
6069 } ) ;
6170
6271
6372 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' ) ;
7374
7475 const loaded = await new Promise ( ( res , rej ) => {
7576 image . on ( 'load' , ( ) => res ( true ) ) ;
7677 image . on ( 'error' , rej ) ;
7778 } ) ;
7879
7980 assert . ok ( loaded ) ;
80-
8181 } ) ;
8282
8383
8484 it ( 'early #addEventListener() calls back with the correct scope' , async ( ) => {
85-
8685 const image = new Image ( ) ;
8786
8887 const that = await new Promise ( ( res , rej ) => {
@@ -92,27 +91,22 @@ describe('Image', () => {
9291 } ) ;
9392
9493 assert . strictEqual ( that , image ) ;
95-
9694 } ) ;
9795
9896
9997 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' ) ;
10399
104100 const that = await new Promise ( ( res , rej ) => {
105101 image . addEventListener ( 'load' , function ( ) { res ( this ) ; } ) ;
106102 image . addEventListener ( 'error' , rej ) ;
107103 } ) ;
108104
109105 assert . strictEqual ( that , image ) ;
110-
111106 } ) ;
112107
113108
114109 it ( 'early #on() calls back with the correct scope' , async ( ) => {
115-
116110 const image = new Image ( ) ;
117111
118112 const that = await new Promise ( ( res , rej ) => {
@@ -122,27 +116,22 @@ describe('Image', () => {
122116 } ) ;
123117
124118 assert . strictEqual ( that , image ) ;
125-
126119 } ) ;
127120
128121
129122 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' ) ;
133124
134125 const that = await new Promise ( ( res , rej ) => {
135126 image . on ( 'load' , function ( ) { res ( this ) ; } ) ;
136127 image . on ( 'error' , rej ) ;
137128 } ) ;
138129
139130 assert . strictEqual ( that , image ) ;
140-
141131 } ) ;
142132
143133
144134 it ( 'early #once() calls back with the correct scope' , async ( ) => {
145-
146135 const image = new Image ( ) ;
147136
148137 const that = await new Promise ( ( res , rej ) => {
@@ -152,118 +141,83 @@ describe('Image', () => {
152141 } ) ;
153142
154143 assert . strictEqual ( that , image ) ;
155-
156144 } ) ;
157145
158146
159147 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' ) ;
163149
164150 const that = await new Promise ( ( res , rej ) => {
165151 image . once ( 'load' , function ( ) { res ( this ) ; } ) ;
166152 image . once ( 'error' , rej ) ;
167153 } ) ;
168154
169155 assert . strictEqual ( that , image ) ;
170-
171156 } ) ;
172157
173-
174- it ( 'has accessible data' , async ( ) => {
158+ it ( 'has accessible JPG data' , async ( ) => {
159+ const image = await loadImageAsync ( 'freeimage.jpg' ) ;
175160
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' ) ;
183167
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' ) ;
186174
175+ assert . ok ( ! ! image . data ) ;
176+ assert . strictEqual ( image . data . length , TEST_IMAGE_LENGTH ) ;
187177 } ) ;
188178
189179
190180 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' ) ;
199182
200183 assert . strictEqual ( image . width , TEST_IMAGE_WIDTH ) ;
201184 assert . strictEqual ( image . naturalWidth , TEST_IMAGE_WIDTH ) ;
202185
203186 assert . strictEqual ( image . height , TEST_IMAGE_HEIGHT ) ;
204187 assert . strictEqual ( image . naturalHeight , TEST_IMAGE_HEIGHT ) ;
205-
206188 } ) ;
207189
208190
209191 it ( 'has correct `complete` when empty' , async ( ) => {
210-
211192 const image = new Image ( ) ;
212-
213193 assert . strictEqual ( image . complete , false ) ;
214-
215194 } ) ;
216195
217196
218197 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' ) ;
228199 assert . ok ( image . complete ) ;
229-
230200 } ) ;
231201
232- const setSrc = ( image , src ) => { image . src = src ; } ;
202+
233203 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' ) ;
237205
238206 let status = '' ;
239207 image . on ( 'load' , ( ) => status += image . complete ) ;
240208
241- await new Promise ( ( res , rej ) => {
242- image . once ( 'load' , res ) ;
243- image . once ( 'error' , rej ) ;
244- } ) ;
245-
246209 assert . ok ( image . complete ) ;
247210
248- setSrc ( image , '' ) ;
249-
211+ image . src = '' ;
250212 await new Promise ( ( res ) => setTimeout ( res , 10 ) ) ;
251213
252- assert . strictEqual ( image . complete , false ) ;
214+ assert . ok ( ! image . complete ) ;
253215 assert . strictEqual ( status , 'truefalse' ) ;
254-
255216 } ) ;
256217
257218
258219 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' ) ;
267221
268222 const dest = new Image ( ) ;
269223 dest . drawImage (
@@ -274,7 +228,5 @@ describe('Image', () => {
274228
275229 assert . strictEqual ( dest . width , TEST_STRETCH_WIDTH ) ;
276230 assert . strictEqual ( dest . height , TEST_STRETCH_HEIGHT ) ;
277-
278231 } ) ;
279-
280232} ) ;
0 commit comments