-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-http-alert.spec.js
68 lines (48 loc) · 2.17 KB
/
angular-http-alert.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
describe('http-alert', function() {
var interceptor, interceptorProvider, alerter, responseParser, filter, ngFilter, q, rejectReturnValue;
beforeEach(function() {
module('http-alert');
module(function(httpAlertInterceptorProvider, $provide) {
interceptorProvider = httpAlertInterceptorProvider;
alerter = {};
alerter.error = jasmine.createSpy();
filter = function(response) {
return response.data == "foobar";
};
responseParser = jasmine.createSpy().and.returnValue("Error!");
httpAlertInterceptorProvider.setAlerter(alerter);
httpAlertInterceptorProvider.setResponseParser(responseParser);
httpAlertInterceptorProvider.setFilter(filter);
$provide.service('$filter', function () {
return jasmine.createSpy('$filter').and.returnValue(jasmine.createSpy('$filter.func'));
});
$provide.service('$q', function () {
rejectReturnValue = true;
this.reject = jasmine.createSpy('$q.reject').and.returnValue(rejectReturnValue);
});
});
inject(function(httpAlertInterceptor, $filter, $q) {
interceptor = httpAlertInterceptor;
ngFilter = $filter;
q = $q;
});
});
it('should have a response error handler', function() {
var configMock = {data: 'foobar'};
var result = interceptor.responseError(configMock);
expect(responseParser).toHaveBeenCalled();
expect(alerter.error).toHaveBeenCalledWith("Error!");
expect(q.reject).toHaveBeenCalledWith(configMock);
expect(result).toBe(rejectReturnValue);
});
it('should not alert error when filter returns false', function() {
var configMock = {data: 'error!!'};
expect(alerter.error).not.toHaveBeenCalledWith();
});
it('should set translation parser', function() {
var configMock = {data: 'foobar'};
interceptorProvider.setUseTranslate(true);
interceptor.responseError(configMock);
expect(ngFilter).toHaveBeenCalledWith('translate');
});
});