Skip to content

Commit 1720d69

Browse files
author
James Newell
committed
added jquery example, fix issue with jquery, added .mock override with shortcut
1 parent 7677965 commit 1720d69

File tree

6 files changed

+148
-32
lines changed

6 files changed

+148
-32
lines changed

component.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{
22
"name": "xhr-mock",
3-
"scripts": ["index.js", "lib/MockXMLHttpRequest.js"]
3+
"scripts": ["index.js", "lib/MockXMLHttpRequest.js"],
4+
"development": {
5+
"dependencies": {
6+
"component/assert": "*"
7+
}
8+
}
49
}

example/jquery.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>jQuery Example &mdash; xhr-mock</title>
6+
</head>
7+
<body>
8+
9+
<h1>xhr-mock</h1>
10+
<h2>jQuery Example</h2>
11+
12+
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
13+
<script src="../build/build.js"></script>
14+
<script>
15+
16+
var mock = require('xhr-mock');
17+
mock.setup();
18+
19+
mock.get('http://google.com/', function(request) {
20+
request.status = 200;
21+
request.responseText = '<h1>Google</h1>';
22+
});
23+
24+
// ---------
25+
26+
$.get('http://google.com/', function(data, textStatus, jqXHR) {
27+
console.log(arguments);
28+
}).fail(function() {
29+
console.log('ERROR');
30+
});
31+
32+
// ---------
33+
34+
</script>
35+
</body>
36+
</html>

example.html renamed to example/native.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
<html>
33
<head lang="en">
44
<meta charset="UTF-8">
5-
<title></title>
5+
<title>Native Example &mdash; xhr-mock</title>
66
</head>
77
<body>
88

9-
<script src="build/build.js"></script>
9+
<h1>xhr-mock</h1>
10+
<h2>Native Example</h2>
11+
12+
<script src="../build/build.js"></script>
1013
<script>
1114

1215
var mock = require('xhr-mock');

index.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ var MockXMLHttpRequest = require('./lib/MockXMLHttpRequest');
33
var real = window.XMLHttpRequest;
44
var mock = MockXMLHttpRequest;
55

6+
/**
7+
* Mock utility
8+
*/
69
module.exports = {
710

11+
XMLHttpRequest: MockXMLHttpRequest,
12+
813
/**
914
* Replace the native XHR with the mocked XHR
1015
* @returns {exports}
1116
*/
1217
setup: function() {
1318
window.XMLHttpRequest = mock;
19+
MockXMLHttpRequest.handlers = [];
1420
return this;
1521
},
1622

@@ -19,8 +25,8 @@ module.exports = {
1925
* @returns {exports}
2026
*/
2127
teardown: function() {
28+
MockXMLHttpRequest.handlers = [];
2229
window.XMLHttpRequest = real;
23-
MockXMLHttpRequest.handlers = [];
2430
return this;
2531
},
2632

@@ -29,27 +35,51 @@ module.exports = {
2935
* @param {Function} fn
3036
* @returns {exports}
3137
*/
32-
mock: function(fn) {
33-
MockXMLHttpRequest.addHandler(fn);
38+
mock: function(method, url, fn) {
39+
var handler = fn;
40+
41+
if (arguments.length === 3) {
42+
handler = function(request) {
43+
if (request.method == method && request.url == url) {
44+
fn(request);
45+
return true;
46+
}
47+
};
48+
}
49+
50+
MockXMLHttpRequest.addHandler(handler);
51+
3452
return this;
3553
},
3654

3755
/**
38-
* Mock a request
56+
* Mock a GET request
3957
* @param {String} url
4058
* @param {Function} fn
4159
* @returns {exports}
4260
*/
4361
get: function(url, fn) {
44-
var method = 'GET';
45-
MockXMLHttpRequest.addHandler(function(request) {
46-
if (request.method == method && request.url == url) {
47-
fn(request);
48-
return true;
49-
}
50-
});
51-
return this;
52-
}
62+
return this.mock('GET', url, fn);
63+
},
64+
65+
/**
66+
* Mock a POST request
67+
* @param {String} url
68+
* @param {Function} fn
69+
* @returns {exports}
70+
*/
71+
post: function(url, fn) {
72+
return this.mock('POST', url, fn);
73+
},
5374

75+
/**
76+
* Mock a PUT request
77+
* @param {String} url
78+
* @param {Function} fn
79+
* @returns {exports}
80+
*/
81+
put: function(url, fn) {
82+
return this.mock('PUT', url, fn);
83+
}
5484

5585
};

lib/MockXMLHttpRequest.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
//https://xhr.spec.whatwg.org/
44
//http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/
55

6-
MockXMLHttpRequest.STATE_UNINITIALIZED = 0;
7-
MockXMLHttpRequest.STATE_OPEN = 1;
8-
MockXMLHttpRequest.STATE_SENT = 2;
9-
MockXMLHttpRequest.STATE_RECEIVING = 3;
10-
MockXMLHttpRequest.STATE_LOADED = 4;
6+
MockXMLHttpRequest.STATE_UNSENT = 0;
7+
MockXMLHttpRequest.STATE_OPENED = 1;
8+
MockXMLHttpRequest.STATE_HEADERS_RECEIVED = 2;
9+
MockXMLHttpRequest.STATE_LOADING = 3;
10+
MockXMLHttpRequest.STATE_DONE = 4;
1111

1212
/**
1313
* The request handlers
@@ -74,13 +74,13 @@ function MockXMLHttpRequest() {
7474
MockXMLHttpRequest.prototype.reset = function() {
7575

7676
this.response = null;
77-
this.responseType = ''
77+
this.responseType = '';
7878
this.responseText = '';
7979

8080
this.status = '';
8181
this.statusText = '';
8282

83-
this.readyState = MockXMLHttpRequest.STATE_UNINITIALIZED;
83+
this.readyState = MockXMLHttpRequest.STATE_UNSENT;
8484
};
8585

8686
/**
@@ -94,19 +94,23 @@ MockXMLHttpRequest.prototype.trigger = function(event) {
9494
this['on'+event]();
9595
}
9696

97+
if (this['onreadystatechange']) {
98+
this['onreadystatechange']();
99+
}
100+
97101
//iterate over the listeners
98102

99103
return this;
100104
};
101105

102106
MockXMLHttpRequest.prototype.open = function(method, url, async, user, password) {
103107
this.reset();
104-
this.method = method,
105-
this.url = url,
106-
this.async = async,
107-
this.user = user,
108-
this.password = password,
109-
this.data = null
108+
this.method = method;
109+
this.url = url;
110+
this.async = async;
111+
this.user = user;
112+
this.password = password;
113+
this.data = null;
110114
};
111115

112116
MockXMLHttpRequest.prototype.setRequestHeader = function(header, value) {
@@ -126,13 +130,13 @@ MockXMLHttpRequest.prototype.send = function(data) {
126130
if (handled) {
127131

128132
//trigger a success event because the request was handled
129-
self.readyState = MockXMLHttpRequest.STATE_LOADED;
133+
self.readyState = MockXMLHttpRequest.STATE_DONE;
130134
self.trigger('load');
131135

132136
} else {
133137

134138
//trigger an error because the request was handled
135-
self.readyState = MockXMLHttpRequest.STATE_LOADED;
139+
self.readyState = MockXMLHttpRequest.STATE_DONE;
136140
self.trigger('error');
137141

138142
}
@@ -144,7 +148,10 @@ MockXMLHttpRequest.prototype.send = function(data) {
144148
MockXMLHttpRequest.prototype.abort = function() {
145149
};
146150

147-
MockXMLHttpRequest.prototype.geAllResponseHeaders = function(header) {
151+
MockXMLHttpRequest.prototype.getAllResponseHeaders = function(header) {
152+
if (this.readyState === MockXMLHttpRequest.STATE_UNSENT) {
153+
return '';
154+
}
148155
};
149156

150157
MockXMLHttpRequest.prototype.geResponseHeader = function(header) {

test/MockXMLHttpRequest.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var assert = require('assert');
2+
var mock = require('xhr-mock');
3+
4+
describe('xhr-mock', function() {
5+
6+
describe('.setup() and teardown()', function() {
7+
8+
it('should setup and teardown the mock XMLHttpRequest class', function() {
9+
10+
var xhr = window.XMLHttpRequest;
11+
mock.setup();
12+
assert.notEqual(window.XMLHttpRequest, xhr);
13+
mock.teardown();
14+
assert.equal(window.XMLHttpRequest, xhr);
15+
16+
});
17+
18+
it('should remove any handlers', function() {
19+
20+
mock.get('http://www.google.com/', function() {});
21+
mock.setup();
22+
assert.equal(mock.XMLHttpRequest.handlers.length, 0);
23+
mock.get('http://www.google.com/', function() {});
24+
mock.teardown();
25+
assert.equal(mock.XMLHttpRequest.handlers.length, 0);
26+
27+
28+
});
29+
30+
});
31+
32+
33+
34+
35+
});

0 commit comments

Comments
 (0)