-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
57 lines (52 loc) · 1.27 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
'use strict'
var assert = require('assert')
var test = require('testit')
var Promise = require('promise')
var gorge = require('./')
function delay(timeout) {
return new Promise(function (resolve) {
setTimeout(resolve, timeout)
})
}
test('it returns a function which gets run', function () {
var runs = []
var fn = gorge(function () {
var self = this
var args = arguments
return new Promise(function (resolve, reject) {
runs.push({
self: self,
args: args,
resolve: resolve,
reject: reject
})
})
})
function alwaysPending(v) {
return v.done(function () {
throw new Error('This promise should have always been pending')
})
}
alwaysPending(fn(1))
alwaysPending(fn(2))
alwaysPending(fn(3))
var last = fn(4)
assert(runs.length === 1)
assert(runs[0].args[0] === 1)
runs[0].resolve(null)
return delay(50).then(function () {
assert(runs.length === 2)
assert(runs[1].args[0] === 4)
runs[1].resolve('result')
return last
}).then(function (result) {
assert(result === 'result')
var next = fn(5)
assert(runs.length === 3)
assert(runs[2].args[0] === 5)
runs[2].resolve('result2')
return next
}).then(function (result) {
assert(result === 'result2')
})
})