This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
response-delay.spec.js
122 lines (96 loc) · 4 KB
/
response-delay.spec.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
'use strict';
var assert = require('assert');
var ResponseDelay = require('../../lib/services/response-delay');
describe('Response delay', function() {
var responseDelay;
beforeEach(function() {
responseDelay = new ResponseDelay();
});
describe('No delay', function() {
it('should have no delay by default', function() {
assert.equal(responseDelay.delayTimeInMs(), 0);
});
it('should have no delay when given null parameter', function() {
assert.equal(responseDelay.delayTimeInMs(null), 0);
});
it('should have no delay when given a parameter that coerce to false', function() {
assert.equal(responseDelay.delayTimeInMs(false), 0);
assert.equal(responseDelay.delayTimeInMs(0), 0);
assert.equal(responseDelay.delayTimeInMs(''), 0);
assert.equal(responseDelay.delayTimeInMs(NaN), 0);
});
});
describe('Simple delays', function() {
it('should have a simple delay when given a number', function() {
assert.equal(responseDelay.delayTimeInMs(42), 42);
});
it('should have a simple delay when given a string that coerce to a number', function() {
assert.equal(responseDelay.delayTimeInMs('42'), 42);
});
});
describe('Ranged delays', function() {
describe('Lower range', function() {
beforeEach(function() {
var fakeRandomNumberGenerator = function() {
return 0;
};
responseDelay = new ResponseDelay(fakeRandomNumberGenerator);
});
it('should have minimum delay of 500ms when using auto parameter', function() {
assert.equal(responseDelay.delayTimeInMs('auto'), 500);
});
it('should have minimum delay of 150ms when using fast parameter', function() {
assert.equal(responseDelay.delayTimeInMs('fast'), 150);
});
it('should have minimum delay of 1500ms when using slow parameter', function() {
assert.equal(responseDelay.delayTimeInMs('slow'), 1500);
});
it('should have minimum delay of 1ms when given an invalid parameter', function() {
assert.equal(responseDelay.delayTimeInMs('invalid parameter'), 1);
});
});
describe('Upper range', function() {
beforeEach(function() {
var fakeRandomNumberGenerator = function() {
return 1;
};
responseDelay = new ResponseDelay(fakeRandomNumberGenerator);
});
it('should have maximum delay of 1750ms when using auto parameter', function() {
assert.equal(responseDelay.delayTimeInMs('auto'), 1750);
});
it('should have maximum delay of 1000ms when using fast parameter', function() {
assert.equal(responseDelay.delayTimeInMs('fast'), 1000);
});
it('should have maximum delay of 3000ms when using slow parameter', function() {
assert.equal(responseDelay.delayTimeInMs('slow'), 3000);
});
it('should have maximum delay of 1ms when given an invalid parameter', function() {
assert.equal(responseDelay.delayTimeInMs('invalid parameter'), 1);
});
});
describe('Custom ranges', function() {
it('should use the given upper bound to calculate the delay', function() {
var fakeRandomNumberGenerator = function() {
return 1;
};
responseDelay = new ResponseDelay(fakeRandomNumberGenerator);
assert.equal(responseDelay.delayTimeInMs({lowerBound: 50, upperBound: 100}), 100);
});
it('should use the given lower bound to calculate the delay', function() {
var fakeRandomNumberGenerator = function() {
return 0;
};
responseDelay = new ResponseDelay(fakeRandomNumberGenerator);
assert.equal(responseDelay.delayTimeInMs({lowerBound: 50, upperBound: 100}), 50);
});
});
it('should round delays to the millisecond', function() {
var fakeRandomNumberGenerator = function() {
return 0.001;
};
responseDelay = new ResponseDelay(fakeRandomNumberGenerator);
assert.equal(responseDelay.delayTimeInMs('fast'), 150);
});
});
});