-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
68 lines (63 loc) · 1.82 KB
/
index.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
process.env.NODE_ENV = 'test';
var fetchwCache = require('./index.js'),
chai = require('chai');
describe('Basic tests', function(){
it('should get a json response', function(done){
fetchwCache('https://jsonplaceholder.typicode.com/posts/1', {
headers: { Accept: 'application/json' }
})
.then(function(res){
if (res && res.id === 1) done();
})
.catch(function(err){
done(err);
});
});
it('should get a plain text response', function(done){
fetchwCache('https://jsonplaceholder.typicode.com/posts?userId=1', {
headers: { Accept: 'text/plain' }
})
.then(function(res){
if (res && res.length === 2726) done();
else console.log(res);
})
.catch(function(err){
done(err);
});
});
});
describe('Caching tests', function(){
var startTime;
it('should cache a json response', function(done){
fetchwCache('https://jsonplaceholder.typicode.com/photos', {
headers: { Accept: 'application/json' }
})
.then(function(res){
startTime = new Date().getTime();
if (res && res['10361189088'] ==='Moreno, Victor'){
return fetchwCache('https://jsonplaceholder.typicode.com/photos', {headers: { Accept: 'application/json' }})
}
})
.then(function(res2){
var endTime = new Date().getTime() - startTime;
if (endTime < 10)
done();
else
throw new Error('request appeared to not cache');
})
.catch(function(err){
done(err);
});
});
// it('should get a plain text response', function(done){
// fetchwCache('https://jsonplaceholder.typicode.com/posts/1', {
// headers: { Accept: 'text/plain' }
// })
// .then(function(res){
// if (res && res.length === 2726) done();
// })
// .catch(function(err){
// done(err);
// });
// });
});