summaryrefslogtreecommitdiff
path: root/app/services/api/api-problem.test.ts
blob: 2b37aa950ce6d1fb9b423c1fc3df8215d0d2fc6c (about) (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { getGeneralApiProblem } from './api-problem'
import { ApiErrorResponse } from 'apisauce'

test('handles connection errors', () => {
  expect(
    getGeneralApiProblem({
      problem: 'CONNECTION_ERROR',
    } as ApiErrorResponse<null>),
  ).toEqual({
    kind: 'cannot-connect',
    temporary: true,
  })
})

test('handles network errors', () => {
  expect(
    getGeneralApiProblem({
      problem: 'NETWORK_ERROR',
    } as ApiErrorResponse<null>),
  ).toEqual({
    kind: 'cannot-connect',
    temporary: true,
  })
})

test('handles timeouts', () => {
  expect(
    getGeneralApiProblem({
      problem: 'TIMEOUT_ERROR',
    } as ApiErrorResponse<null>),
  ).toEqual({
    kind: 'timeout',
    temporary: true,
  })
})

test('handles server errors', () => {
  expect(
    getGeneralApiProblem({ problem: 'SERVER_ERROR' } as ApiErrorResponse<null>),
  ).toEqual({
    kind: 'server',
  })
})

test('handles unknown errors', () => {
  expect(
    getGeneralApiProblem({
      problem: 'UNKNOWN_ERROR',
    } as ApiErrorResponse<null>),
  ).toEqual({
    kind: 'unknown',
    temporary: true,
  })
})

test('handles unauthorized errors', () => {
  expect(
    getGeneralApiProblem({
      problem: 'CLIENT_ERROR',
      status: 401,
    } as ApiErrorResponse<null>),
  ).toEqual({
    kind: 'unauthorized',
  })
})

test('handles forbidden errors', () => {
  expect(
    getGeneralApiProblem({
      problem: 'CLIENT_ERROR',
      status: 403,
    } as ApiErrorResponse<null>),
  ).toEqual({
    kind: 'forbidden',
  })
})

test('handles not-found errors', () => {
  expect(
    getGeneralApiProblem({
      problem: 'CLIENT_ERROR',
      status: 404,
    } as ApiErrorResponse<null>),
  ).toEqual({
    kind: 'not-found',
  })
})

test('handles other client errors', () => {
  expect(
    getGeneralApiProblem({
      problem: 'CLIENT_ERROR',
      status: 418,
    } as ApiErrorResponse<null>),
  ).toEqual({
    kind: 'rejected',
  })
})

test('handles cancellation errors', () => {
  expect(
    getGeneralApiProblem({ problem: 'CANCEL_ERROR' } as ApiErrorResponse<null>),
  ).toBeNull()
})