|
| 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 | +}); |
0 commit comments