1
+ import fetchMock from 'fetch-mock' ;
2
+
1
3
import getDefaultNetworkHelpers from '../../../src/internals/network/getDefaultNetworkHelpers' ;
2
4
3
5
const NETWORK_HELPERS = getDefaultNetworkHelpers ( ) ;
6
+ const customGetTokenNetworkHelpers = {
7
+ ...NETWORK_HELPERS ,
8
+ getToken : ( ) => '' ,
9
+ } ;
4
10
5
11
describe ( 'getDefaultNetworkHelpers' , ( ) => {
6
- test ( 'only path ' , ( ) => {
12
+ test ( 'all default ' , ( ) => {
7
13
expect ( NETWORK_HELPERS ) . toMatchSnapshot ( ) ;
8
14
} ) ;
15
+
16
+ test ( 'custom getToken' , ( ) => {
17
+ expect ( customGetTokenNetworkHelpers ) . toMatchSnapshot ( ) ;
18
+ } ) ;
9
19
} ) ;
10
20
11
21
describe ( 'requestGET' , ( ) => {
12
- test ( 'only path ' , async ( ) => {
22
+ test ( 'all default ' , async ( ) => {
13
23
expect ( await NETWORK_HELPERS . requestGET ( ) ) . toMatchSnapshot ( ) ;
14
24
} ) ;
25
+
26
+ test ( 'custom getToken' , async ( ) => {
27
+ expect ( await customGetTokenNetworkHelpers . requestGET ( ) ) . toMatchSnapshot ( ) ;
28
+ } ) ;
15
29
} ) ;
16
30
17
31
describe ( 'requestPATCH' , ( ) => {
18
- test ( 'only path ' , async ( ) => {
32
+ test ( 'all default ' , async ( ) => {
19
33
expect ( await NETWORK_HELPERS . requestPATCH ( ) ) . toMatchSnapshot ( ) ;
20
34
} ) ;
35
+
36
+ test ( 'custom getToken' , async ( ) => {
37
+ expect ( await customGetTokenNetworkHelpers . requestPATCH ( ) ) . toMatchSnapshot ( ) ;
38
+ } ) ;
21
39
} ) ;
22
40
23
41
describe ( 'requestPUT' , ( ) => {
24
- test ( 'only path ' , async ( ) => {
42
+ test ( 'all default ' , async ( ) => {
25
43
expect ( await NETWORK_HELPERS . requestPUT ( ) ) . toMatchSnapshot ( ) ;
26
44
} ) ;
45
+
46
+ test ( 'custom getToken' , async ( ) => {
47
+ expect ( await customGetTokenNetworkHelpers . requestPUT ( ) ) . toMatchSnapshot ( ) ;
48
+ } ) ;
27
49
} ) ;
28
50
29
51
describe ( 'requestPOST' , ( ) => {
30
- test ( 'only path ' , async ( ) => {
52
+ test ( 'all default ' , async ( ) => {
31
53
expect ( await NETWORK_HELPERS . requestPOST ( ) ) . toMatchSnapshot ( ) ;
32
54
} ) ;
55
+
56
+ test ( 'custom getToken' , async ( ) => {
57
+ expect ( await customGetTokenNetworkHelpers . requestPOST ( ) ) . toMatchSnapshot ( ) ;
58
+ } ) ;
33
59
} ) ;
34
60
35
61
describe ( 'requestDELETE' , ( ) => {
36
- test ( 'only path ' , async ( ) => {
62
+ test ( 'all default ' , async ( ) => {
37
63
expect ( await NETWORK_HELPERS . requestDELETE ( ) ) . toMatchSnapshot ( ) ;
38
64
} ) ;
65
+
66
+ test ( 'custom getToken' , async ( ) => {
67
+ expect (
68
+ await customGetTokenNetworkHelpers . requestDELETE ( ) ,
69
+ ) . toMatchSnapshot ( ) ;
70
+ } ) ;
39
71
} ) ;
40
72
41
73
describe ( 'handleStatusCode' , ( ) => {
42
- test ( 'valid response' , ( ) => {
43
- const response = { status : 200 } ;
44
- const response2 = { status : 299 } ;
45
-
46
- expect ( NETWORK_HELPERS . handleStatusCode ( response ) ) . toBe ( response ) ;
47
- expect ( NETWORK_HELPERS . handleStatusCode ( response2 ) ) . toBe ( response2 ) ;
74
+ test ( 'no response' , ( ) => {
75
+ expect ( NETWORK_HELPERS . handleStatusCode ( null ) ) . toBeNull ( ) ;
48
76
} ) ;
49
77
50
78
test ( 'invalid response' , ( ) => {
51
79
const response = { status : 300 } ;
52
80
53
81
expect ( ( ) => NETWORK_HELPERS . handleStatusCode ( response ) ) . toThrow ( ) ;
54
82
} ) ;
83
+
84
+ test ( 'valid response' , ( ) => {
85
+ const response = { status : 200 } ;
86
+ const response2 = { status : 299 } ;
87
+
88
+ expect ( NETWORK_HELPERS . handleStatusCode ( response ) ) . toBe ( response ) ;
89
+ expect ( NETWORK_HELPERS . handleStatusCode ( response2 ) ) . toBe ( response2 ) ;
90
+ } ) ;
55
91
} ) ;
56
92
57
93
describe ( 'handleError' , ( ) => {
@@ -77,4 +113,31 @@ describe('handleError', () => {
77
113
78
114
expect ( consoleErrorMock ) . toHaveBeenCalledTimes ( 1 ) ;
79
115
} ) ;
116
+
117
+ test ( 'wrongly formatted error 2' , async ( ) => {
118
+ const error = { response : 'oh no' } ;
119
+
120
+ await NETWORK_HELPERS . handleError ( error ) ;
121
+
122
+ expect ( consoleErrorMock ) . toHaveBeenCalledTimes ( 1 ) ;
123
+ } ) ;
124
+
125
+ test ( 'valid error' , async ( ) => {
126
+ fetchMock . mock ( 'error' , {
127
+ status : 400 ,
128
+ body : {
129
+ message : 'errorMessage' ,
130
+ } ,
131
+ } ) ;
132
+
133
+ const res1 = await fetch ( 'error' ) ;
134
+ const error = new Error ( res1 . statusText ) ;
135
+ error . response = res1 ;
136
+
137
+ fetchMock . restore ( ) ;
138
+
139
+ await NETWORK_HELPERS . handleError ( error ) ;
140
+
141
+ expect ( consoleErrorMock ) . toHaveBeenCalledTimes ( 1 ) ;
142
+ } ) ;
80
143
} ) ;
0 commit comments