-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
141 lines (114 loc) · 2.94 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
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
import test from 'ava';
import getMediaSize from '.';
function HTMLVideoElement(ready) {
this.tagName = 'VIDEO';
if (ready) {
this.videoWidth = 1280;
this.videoHeight = 720;
return;
}
this.videoWidth = 0;
this.videoHeight = 0;
this.addEventListener = (type, listener) => {
this.videoWidth = 1280;
this.videoHeight = 720;
setTimeout(listener, 500);
};
}
function HTMLImageElement(ready) {
if (ready) {
this.naturalWidth = 1280;
this.naturalHeight = 720;
return;
}
this.naturalWidth = 0;
this.naturalHeight = 0;
setTimeout(() => {
this.naturalWidth = 1280;
this.naturalHeight = 720;
}, 350);
}
const successfulSize = {
width: 1280,
height: 720
};
const successfulHalfSize = {
width: 640,
height: 360
};
const failedSize = {
width: 0,
height: 0
};
const readyImageStub = new HTMLImageElement(true);
const nonReadyImageStub = {
naturalWidth: 0,
naturalHeight: 0
};
const readyVideoStub = new HTMLVideoElement(true);
const nonReadyVideoStub = {
videoWidth: 0,
videoHeight: 0
};
const canvasStub = {
getContext() {},
width: 1280,
height: 720
};
const contextStub = {
canvas: canvasStub
};
/**
* SYNC TESTS
*/
test('sync: ready image success', t => {
t.deepEqual(getMediaSize.sync(readyImageStub), successfulSize);
});
test('sync: non-ready image fail', t => {
t.deepEqual(getMediaSize.sync(nonReadyImageStub), failedSize);
});
test('sync: ready video success', t => {
t.deepEqual(getMediaSize.sync(readyVideoStub), successfulSize);
});
test('sync: non-ready video fail', t => {
t.deepEqual(getMediaSize.sync(nonReadyVideoStub), failedSize);
});
test('sync: canvas success', t => {
t.deepEqual(getMediaSize.sync(canvasStub), successfulSize);
});
test('sync: context success', t => {
t.deepEqual(getMediaSize.sync(contextStub), successfulSize);
});
test('sync: no argument fail', t => {
t.deepEqual(getMediaSize.sync(), failedSize);
});
test('sync: image scale', t => {
t.deepEqual(getMediaSize.sync(readyImageStub, 2), successfulHalfSize);
});
/**
* ASYNC TESTS
*/
test('async: ready image success', async t => {
t.deepEqual(await getMediaSize(new HTMLImageElement(true)), successfulSize);
});
test('async: non-ready image success', async t => {
t.deepEqual(await getMediaSize(new HTMLImageElement()), successfulSize);
});
test('async: ready video success', async t => {
t.deepEqual(await getMediaSize(new HTMLVideoElement(true)), successfulSize);
});
test('async: non-ready video success', async t => {
t.deepEqual(await getMediaSize(new HTMLVideoElement()), successfulSize);
});
test('async: canvas success', async t => {
t.deepEqual(await getMediaSize(canvasStub), successfulSize);
});
test('async: context success', async t => {
t.deepEqual(await getMediaSize(contextStub), successfulSize);
});
// test('async: no argument fail', async t => {
// t.throws(getMediaSize(), failedSize);
// });
test('async: image scale', async t => {
t.deepEqual(await getMediaSize(new HTMLImageElement(), 2), successfulHalfSize);
});