Skip to content

Commit ee8e981

Browse files
Di PengIgorMinar
Di Peng
authored andcommitted
doc(xhr): add e2e test for JSONP error handling
- add e2e tests - refactor the example by removing clear button and simplifying the code
1 parent 05e2c31 commit ee8e981

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

src/service/xhr.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,21 @@
115115
var self = this;
116116
117117
this.fetch = function() {
118-
self.clear();
118+
self.code = null;
119+
self.response = null;
120+
119121
$xhr(self.method, self.url, function(code, response) {
120122
self.code = code;
121123
self.response = response;
124+
}, function(code, response) {
125+
self.code = code;
126+
self.response = response || "Request failed";
122127
});
123128
};
124129
125-
this.clear = function() {
126-
self.code = null;
127-
self.response = null;
130+
this.updateModel = function(method, url) {
131+
self.method = method;
132+
self.url = url;
128133
};
129134
}
130135
FetchCntl.$inject = ['$xhr'];
@@ -134,15 +139,38 @@
134139
<option>GET</option>
135140
<option>JSON</option>
136141
</select>
137-
<input type="text" name="url" value="index.html" size="80"/><br/>
138-
<button ng:click="fetch()">fetch</button>
139-
<button ng:click="clear()">clear</button>
140-
<a href="" ng:click="method='GET'; url='index.html'">sample</a>
141-
<a href="" ng:click="method='JSON'; url='https://www.googleapis.com/buzz/v1/activities/googlebuzz/@self?alt=json&callback=JSON_CALLBACK'">buzz</a>
142+
<input type="text" name="url" value="index.html" size="80"/>
143+
<button ng:click="fetch()">fetch</button><br>
144+
<button ng:click="updateModel('GET', 'index.html')">Sample GET</button>
145+
<button ng:click="updateModel('JSON', 'https://www.googleapis.com/buzz/v1/activities/googlebuzz/@self?alt=json&callback=JSON_CALLBACK')">Sample JSONP (Buzz API)</button>
146+
<button ng:click="updateModel('JSON', 'https://www.invalid_JSONP_request.com&callback=JSON_CALLBACK')">Invalid JSONP</button>
142147
<pre>code={{code}}</pre>
143148
<pre>response={{response}}</pre>
144149
</div>
145150
</doc:source>
151+
<doc:scenario>
152+
it('should make xhr GET request', function() {
153+
element(':button:contains("Sample GET")').click();
154+
element(':button:contains("fetch")').click();
155+
expect(binding('code')).toBe('code=200');
156+
expect(binding('response')).toMatch(/angularjs.org/);
157+
});
158+
159+
it('should make JSONP request to the Buzz API', function() {
160+
element(':button:contains("Buzz API")').click();
161+
element(':button:contains("fetch")').click();
162+
expect(binding('code')).toBe('code=200');
163+
expect(binding('response')).toMatch(/buzz-feed/);
164+
});
165+
166+
it('should make JSONP request to invalid URL and invoke the error handler',
167+
function() {
168+
element(':button:contains("Invalid JSONP")').click();
169+
element(':button:contains("fetch")').click();
170+
expect(binding('code')).toBe('code=');
171+
expect(binding('response')).toBe('response=Request failed');
172+
});
173+
</doc:scenario>
146174
</doc:example>
147175
*/
148176
angularServiceInject('$xhr', function($browser, $error, $log, $updateView){

0 commit comments

Comments
 (0)