1 import observeApiError from '../lib/observeApiError';
3 const metricObserver = jest.fn();
5 describe('observeApiError', () => {
6 it('ignores reporting if error is undefined', () => {
7 observeApiError(undefined, metricObserver);
9 expect(metricObserver).toHaveBeenCalledTimes(0);
12 it('ignores reporting if error is null', () => {
13 observeApiError(null, metricObserver);
15 expect(metricObserver).toHaveBeenCalledTimes(0);
18 it('ignores reporting if error is not an object', () => {
19 observeApiError('string', metricObserver);
21 expect(metricObserver).toHaveBeenCalledTimes(0);
24 it('ignores reporting if status is -1 (offline)', () => {
25 observeApiError({ status: -1 }, metricObserver);
27 expect(metricObserver).toHaveBeenCalledTimes(0);
30 it('ignores reporting if status is 0', () => {
31 observeApiError({ status: 0 }, metricObserver);
33 expect(metricObserver).toHaveBeenCalledTimes(0);
36 it('ignores reporting if error does not contain a status property', () => {
37 observeApiError('', metricObserver);
39 expect(metricObserver).toHaveBeenCalledTimes(0);
42 it('ignores reporting if it is an abort error', () => {
43 observeApiError({ name: 'AbortError ' }, metricObserver);
45 expect(metricObserver).toHaveBeenCalledTimes(0);
48 it('ignores reporting if it is an HV error', () => {
49 observeApiError({ status: 422, data: { Error: 'HV required', Code: 9001 } }, metricObserver);
51 expect(metricObserver).toHaveBeenCalledTimes(0);
54 it('returns 5xx if error.status is 500', () => {
55 observeApiError({ status: 500 }, metricObserver);
57 expect(metricObserver).toHaveBeenCalledTimes(1);
58 expect(metricObserver).toHaveBeenCalledWith('5xx');
61 it('returns 5xx if error.status is larger than 500', () => {
62 observeApiError({ status: 501 }, metricObserver);
64 expect(metricObserver).toHaveBeenCalledTimes(1);
65 expect(metricObserver).toHaveBeenCalledWith('5xx');
68 it('returns 4xx if error.status is 400', () => {
69 observeApiError({ status: 400 }, metricObserver);
71 expect(metricObserver).toHaveBeenCalledTimes(1);
72 expect(metricObserver).toHaveBeenCalledWith('4xx');
75 it('returns 4xx if error.status is larger than 400', () => {
76 observeApiError({ status: 401 }, metricObserver);
78 expect(metricObserver).toHaveBeenCalledTimes(1);
79 expect(metricObserver).toHaveBeenCalledWith('4xx');
82 it('returns failure if error.status is not in the correct range', () => {
83 observeApiError({ status: 200 }, metricObserver);
85 expect(metricObserver).toHaveBeenCalledTimes(1);
86 expect(metricObserver).toHaveBeenCalledWith('failure');