-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
63 lines (49 loc) · 1.54 KB
/
test.js
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
require('jsdom-global')()
const assert = require('assert')
const imagesLoader = require('./')
const LOAD_FAILURE_SRC = 'failed.jpg'
const LOAD_SUCCESS_SRC = 'success.jpg'
/* eslint-disable */
Object.defineProperty(global.Image.prototype, 'src', {
set(src) {
if (src === LOAD_FAILURE_SRC) {
setTimeout(() => this.dispatchEvent(new Event('error')), Math.random() * 500)
} else if (src === LOAD_SUCCESS_SRC) {
setTimeout(() => this.dispatchEvent(new Event('load')), Math.random() * 1000)
}
}
})
/* eslint-enable */
describe('Bianco images-loader', function() {
it('export default contains all the module methods', function() {
assert.deepEqual(Object.keys(imagesLoader.default), [
'loadImage',
'loadImages'
])
})
it('It can load images in the DOM', function(done) {
const img = document.createElement('img')
imagesLoader.loadImage(img).then(function(i) {
assert.ok(i)
done()
})
img.src = LOAD_SUCCESS_SRC
})
// this test does not work in jsdom somehow
it('It can throw properly the errors', function(done) {
const img = document.createElement('img')
imagesLoader.loadImage(img).then(() => {
throw 'This image should be not loaded'
}).catch(function(e) {
assert.ok(e instanceof Error)
done()
})
img.src = LOAD_FAILURE_SRC
})
it('It can load arrays of images urls', function(done) {
imagesLoader.loadImages([LOAD_SUCCESS_SRC, LOAD_SUCCESS_SRC]).then(function(imgs) {
assert.equal(imgs.length, 2)
done()
})
})
})