resource-request-test.js
1.5 KB
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
var tap = require("tap");
var ResourceRequest = require("../lib/ResourceRequest");
tap.test("can be created", function(t) {
var create = function() {
var request = new ResourceRequest(undefined, Promise); // eslint-disable-line no-unused-vars
};
t.doesNotThrow(create);
t.end();
});
tap.test("times out when created with a ttl", function(t) {
var reject = function(err) {
t.match(err, /ResourceRequest timed out/);
t.end();
};
var resolve = function(r) {
t.fail("should not resolve");
};
var request = new ResourceRequest(10, Promise); // eslint-disable-line no-unused-vars
request.promise.then(resolve, reject);
});
tap.test("calls resolve when resolved", function(t) {
var resource = {};
var resolve = function(r) {
t.equal(r, resource);
t.end();
};
var reject = function(err) {
t.error(err);
};
var request = new ResourceRequest(undefined, Promise);
request.promise.then(resolve, reject);
request.resolve(resource);
});
tap.test("removeTimeout removes the timeout", function(t) {
var reject = function(err) {
t.error(err);
};
var request = new ResourceRequest(10, Promise);
request.promise.then(undefined, reject);
request.removeTimeout();
setTimeout(function() {
t.end();
}, 20);
});
tap.test("does nothing if resolved more than once", function(t) {
var request = new ResourceRequest(undefined, Promise);
t.doesNotThrow(function() {
request.resolve({});
});
t.doesNotThrow(function() {
request.resolve({});
});
t.end();
});