|
1 | 1 | require('jsdom-global')()
|
2 | 2 |
|
3 | 3 | const assert = require('assert')
|
4 |
| -const body = document.body |
5 | 4 | const { loadImage, loadImages } = require('./')
|
6 |
| -const pathGen = pathGenerator() |
7 | 5 |
|
8 | 6 |
|
| 7 | +const LOAD_FAILURE_SRC = 'succes.jpg' |
| 8 | +const LOAD_SUCCESS_SRC = 'failed.jpg' |
| 9 | + |
9 | 10 | /* eslint-disable */
|
10 |
| -function * pathGenerator() { |
11 |
| - let i = 1 |
12 |
| - while (i) { |
13 |
| - yield `data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7?${i++}` |
| 11 | +Object.defineProperty(global.Image.prototype, 'src', { |
| 12 | + set(src) { |
| 13 | + if (src === LOAD_FAILURE_SRC) { |
| 14 | + setTimeout(() => this.onerror(new Error('mocked error')), Math.random() * 1000) |
| 15 | + } else if (src === LOAD_SUCCESS_SRC) { |
| 16 | + setTimeout(() => this.onload(), Math.random() * 1000) |
| 17 | + } |
14 | 18 | }
|
15 |
| -} |
| 19 | +}) |
16 | 20 | /* eslint-enable */
|
17 | 21 |
|
18 |
| -describe('Bianco images-loader', function() { |
19 |
| - beforeEach(function() { |
20 |
| - const div = document.createElement('div') |
21 |
| - div.innerHTML = ` |
22 |
| - <img src=''> |
23 |
| - ` |
24 |
| - body.appendChild(div) |
25 |
| - }) |
26 | 22 |
|
| 23 | +describe('Bianco images-loader', function() { |
27 | 24 | it('It can load images in the DOM', function(done) {
|
28 |
| - const img = document.querySelector('img') |
| 25 | + const img = document.createElement('img') |
| 26 | + |
29 | 27 | loadImage(img).then(function(i) {
|
30 |
| - assert.equal(typeof i.src, 'string') |
| 28 | + assert.ok(i) |
31 | 29 | done()
|
32 | 30 | })
|
33 |
| - img.src = pathGen.next().value |
| 31 | + |
| 32 | + img.src = LOAD_SUCCESS_SRC |
34 | 33 | })
|
35 | 34 |
|
36 | 35 | // this test does not work in jsdom somehow
|
37 |
| - /*it('It can throw properly the errors', function(done) { |
38 |
| - const img = document.querySelector('img') |
39 |
| - loadImage(img).catch(function(e) { |
40 |
| - assert.equal(typeof e, Error) |
| 36 | + it('It can throw properly the errors', function(done) { |
| 37 | + const img = document.createElement('img') |
| 38 | + |
| 39 | + loadImage(img).then(() => { |
| 40 | + throw 'This image should be not loaded' |
| 41 | + }).catch(function(e) { |
| 42 | + assert.ok(e instanceof Error) |
41 | 43 | done()
|
42 | 44 | })
|
43 |
| - img.src = 'foo-bar' |
44 |
| - })*/ |
| 45 | + |
| 46 | + img.src = LOAD_FAILURE_SRC |
| 47 | + }) |
45 | 48 |
|
46 | 49 | it('It can load arrays of images urls', function(done) {
|
47 |
| - loadImages([pathGen.next().value, pathGen.next().value]).then(function(imgs) { |
| 50 | + loadImages([LOAD_SUCCESS_SRC, LOAD_SUCCESS_SRC]).then(function(imgs) { |
48 | 51 | assert.equal(imgs.length, 2)
|
49 | 52 | done()
|
50 | 53 | })
|
|
0 commit comments