/
batch-tests.js
119 lines (93 loc) · 3.78 KB
/
batch-tests.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
YUI.add('batch-tests', function (Y) {
var Assert = Y.Assert,
ArrayAssert = Y.Test.ArrayAssert,
Promise = Y.Promise;
function wait(ms) {
return new Promise(function (fulfill) {
setTimeout(function () {
fulfill(ms);
}, ms);
});
}
function rejectedAfter(ms) {
return new Promise(function (fulfill, reject) {
setTimeout(function () {
reject(new Error(ms));
}, ms);
});
}
// -- Suite --------------------------------------------------------------------
var suite = new Y.Test.Suite({
name: 'Promise: Y.batch'
});
// -- Lifecycle ----------------------------------------------------------------
suite.add(new Y.Test.Case({
name: 'Y.batch basic tests',
'batch should return a promise': function () {
var somePromise = new Promise(function () {});
Assert.isInstanceOf(Promise, Y.batch(5), 'when passed a value, batch should return a promise');
Assert.isInstanceOf(Promise, Y.batch(new Promise(function () {})), 'when passed a promise, batch should return a promise');
Assert.isInstanceOf(Promise, Y.batch(), 'with no parameters batch should still return a promise');
Assert.areNotSame(somePromise, Y.batch(somePromise), 'when passed a promise, batch should return a new promise');
},
'empty parameter list should return an empty array': function () {
var test = this;
Y.batch().then(function (result) {
test.resume(function () {
Assert.areSame(0, result.length, 'with no parameters, batch should result in an empty array');
});
});
test.wait(100);
},
'order of promises should be preserved': function () {
var test = this;
Y.batch(wait(20), wait(10), wait(15)).then(function (result) {
test.resume(function () {
ArrayAssert.itemsAreSame([20, 10, 15], result, 'order of returned values should be the same as the parameter list');
});
});
test.wait();
},
'values should be wrapped in a promise': function () {
var test = this,
obj = {
hello: 'world'
};
Y.batch('foo', 5, obj).then(function (result) {
test.resume(function () {
ArrayAssert.itemsAreSame(['foo', 5, obj], result, 'values passed to batch should be wrapped in promises, not ignored');
});
});
test.wait(100);
},
'correct handling of function parameters': function () {
var test = this;
function testFn() {}
Y.batch(testFn).then(function (values) {
test.resume(function () {
Assert.isFunction(values[0], 'promise value should be a function');
Assert.areSame(testFn, values[0], 'promise value should be the passed function');
});
});
test.wait(100);
}
}));
suite.add(new Y.Test.Case({
name: 'rejection tests',
'batch should fail as fast as possible': function () {
var test = this;
Y.batch(rejectedAfter(20), rejectedAfter(10), rejectedAfter(15)).then(null, function (reason) {
test.resume(function () {
Assert.areEqual('10', reason.message, 'reason should be the one from the first promise to be rejected');
});
});
test.wait(100);
}
}));
Y.Test.Runner.add(suite);
}, '@VERSION@', {
requires: [
'promise',
'test'
]
});