Skip to content

Commit 146770e

Browse files
tommysitubenjih
authored andcommitted
Add unit test to http error handler
1 parent e0af638 commit 146770e

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
import { NgRedux } from '@angular-redux/store';
3+
import { AppState } from '../../app.state';
4+
import { API_ERRORS, notifyError } from './error-handling';
5+
import { mockErrorResponse } from '../testing/http-helper';
6+
import { HOVERFLY_ACTIONS } from '../services/hoverfly.service';
7+
describe('Http error handler', () => {
8+
let ngRedux: NgRedux<AppState>;
9+
10+
beforeEach(() => {
11+
ngRedux = new NgRedux<AppState>(null);
12+
spyOn(ngRedux, 'dispatch');
13+
});
14+
15+
it('should dispatch SERVICE_UNAVAILABLE error on status code 0', () => {
16+
notifyError(mockErrorResponse(0), ngRedux);
17+
18+
expect(ngRedux.dispatch).toHaveBeenCalledWith({
19+
type: HOVERFLY_ACTIONS.NOTIFY_ERROR,
20+
payload: API_ERRORS.SERVICE_UNAVAILABLE
21+
})
22+
});
23+
24+
it('should dispatch UNAUTHORIZED error on status code 401', () => {
25+
notifyError(mockErrorResponse(401), ngRedux);
26+
27+
expect(ngRedux.dispatch).toHaveBeenCalledWith({
28+
type: HOVERFLY_ACTIONS.NOTIFY_ERROR,
29+
payload: API_ERRORS.UNAUTHORIZED
30+
})
31+
});
32+
33+
it('should dispatch TOO_MANY_REQUESTS error on status code 429', () => {
34+
notifyError(mockErrorResponse(429), ngRedux);
35+
36+
expect(ngRedux.dispatch).toHaveBeenCalledWith({
37+
type: HOVERFLY_ACTIONS.NOTIFY_ERROR,
38+
payload: API_ERRORS.TOO_MANY_REQUESTS
39+
})
40+
});
41+
42+
it('should dispatch DEFAULT error on other status code', () => {
43+
notifyError(mockErrorResponse(500), ngRedux);
44+
45+
expect(ngRedux.dispatch).toHaveBeenCalledWith({
46+
type: HOVERFLY_ACTIONS.NOTIFY_ERROR,
47+
payload: API_ERRORS.DEFAULT
48+
})
49+
});
50+
51+
});

src/app/shared/services/auth.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('Service: Auth', () => {
2424
let lastConnection: MockConnection;
2525
let service: AuthService;
2626
let router: MockRouter;
27-
let ngRedux: NgRedux<AppState>
27+
let ngRedux: NgRedux<AppState>;
2828

2929
beforeEach(() => {
3030
ngRedux = new NgRedux<AppState>(null);

src/app/views/login/login.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ export class LoginComponent implements OnInit {
2323
if (error === API_ERRORS.UNAUTHORIZED) {
2424
this.loginError = 'Please try again.';
2525
} else if (error === API_ERRORS.TOO_MANY_REQUESTS) {
26-
this.loginError = 'Too many unsuccessful login attempts. Please wait 10 minutes before trying again. ' +
27-
'(Alternatively, you can restart Hoverfly)'
26+
this.loginError = 'Too many unsuccessful login attempts. Please wait 10 minutes before trying again.';
2827
}
2928
// other error is ignored?
3029
})

0 commit comments

Comments
 (0)