-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
182 lines (147 loc) · 3.92 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
describe('loads', function () {
'use strict';
var EventEmitter = require('events').EventEmitter
, eventstub = require('eventstub')
, assume = require('assume')
, loads = require('./')
, xhr
, ee;
beforeEach(function () {
xhr = eventstub('error, readystatechange, timeout, progress, load, abort');
xhr.status = 200;
ee = new EventEmitter();
});
afterEach(function () {
xhr.removeAllListeners();
ee.removeAllListeners();
});
it('is exported as a function', function () {
assume(loads).is.a('function');
});
it('returns the supplied xhr', function () {
assume(loads(xhr, ee)).equals(xhr);
});
it('emits `end` _after_ when an error occures', function (next) {
next = assume.plan(2, next);
ee
.on('end', function (err) {
assume(err).instanceOf(Error);
next();
})
.on('error', function (e) {
assume(e).instanceOf(Error);
});
loads(xhr, ee).emit('error');
});
it('receives the status code in the `end` event', function (next) {
next = assume.plan(2, next);
ee
.on('end', function (err, status) {
assume(err).to.be.a('undefined');
assume(status.code).equals(200);
next();
});
loads(xhr, ee).emit('load');
});
it('emits an `error` before `end` when `load` has an incorrect status', function (next) {
next = assume.plan(3, next);
ee
.on('end', function (err) {
assume(err).instanceOf(Error);
next();
})
.on('error', function (e) {
assume(e).instanceOf(Error);
assume(e.message).contains('request failed');
});
xhr.status = 6000;
loads(xhr, ee).emit('load');
});
it('emits an error when the connection is abort', function (next) {
ee
.on('error', function (e) {
assume(e).instanceOf(Error);
assume(e.message).contains('request failed');
next();
});
loads(xhr, ee).emit('abort');
});
it('emits, timeout, error, end on connection timeout', function (next) {
var flow = '';
ee
.on('timeout', function () {
flow += 'timeout';
})
.on('error', function (err) {
assume(err).is.an('error');
flow += 'error';
})
.on('end', function (err) {
assume(err).is.an('error');
assume(flow).equals('timeouterror');
next();
});
loads(xhr, ee);
xhr.emit('timeout');
xhr.emit('error');
xhr.emit('end');
});
it('calls xhr.abort when we timeout', function (next) {
var start = Date.now();
xhr.timeout = 200;
xhr.abort = function () {
var taken = Date.now() - start;
assume(taken).is.atleast(200);
next();
};
loads(xhr, ee);
});
it('kills assigned event listeners & timers on end', function (next) {
ee
.on('end', next)
/* istanbul ignore next */
.on('error', function (err) {
throw err;
})
/* istanbul ignore next */
.on('timeout', function (err) {
throw err;
});
xhr.timeout = 10;
loads(xhr, ee);
ee.emit('end');
});
it('emits a `stream` event when onload has data', function (next) {
ee.on('stream', function (chunk, status) {
assume(status.code).equals(200);
assume(chunk).equals('foo');
next();
});
xhr.status = 200;
xhr.responseText = 'foo';
loads(xhr, ee).emit('load');
});
it('cannot receive data after an end event', function (next) {
ee
/* istanbul ignore next */
.on('stream', function () {
throw new Error('I should never be called');
})
.on('end', next);
loads(xhr, ee);
xhr.emit('load');
xhr.responseText = 'foo';
xhr.emit('load');
});
it('it should never call the stream event for 204s', function (next) {
ee
/* istanbul ignore next */
.on('stream', function (chunk, status) {
throw new Error('I should never be called');
})
.on('end', next);
xhr.status = 204;
xhr.responseText = 'foo';
loads(xhr, ee).emit('load');
});
});