1
1
import { retrieveData , generateExport } from '../src/fetch'
2
- import nock from 'nock'
3
2
4
3
jest . setTimeout ( 1000000 )
5
- nock . enableNetConnect ( )
6
4
7
5
describe ( 'fetch' , ( ) => {
8
6
describe ( 'retrieveData' , ( ) => {
9
- afterEach ( nock . cleanAll )
10
- afterAll ( nock . restore )
7
+ afterEach ( ( ) => {
8
+ jest . clearAllMocks ( )
9
+ } )
11
10
12
11
it ( 'should return some data' , async ( ) => {
13
- nock ( 'https://jamesiv.es' ) . get ( '/' ) . reply ( 200 , {
14
- data : '12345'
12
+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
13
+ ok : true ,
14
+ json : jest . fn ( ) . mockResolvedValue ( { data : '12345' } ) ,
15
+ text : jest . fn ( ) . mockResolvedValue ( '{"data":"12345"}' )
15
16
} )
16
17
17
18
const data = await retrieveData ( {
@@ -22,11 +23,11 @@ describe('fetch', () => {
22
23
} )
23
24
24
25
it ( 'should handle the triple bracket replacements' , async ( ) => {
25
- nock ( 'https://jives.dev/' )
26
- . post ( '/' , '{"bestCat":"montezuma"}' )
27
- . reply ( 200 , {
28
- data : ' 12345'
29
- } )
26
+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
27
+ ok : true ,
28
+ json : jest . fn ( ) . mockResolvedValue ( { data : '12345' } ) ,
29
+ text : jest . fn ( ) . mockResolvedValue ( '{"data":" 12345"}' )
30
+ } )
30
31
31
32
const data = await retrieveData ( {
32
33
debug : true ,
@@ -45,8 +46,6 @@ describe('fetch', () => {
45
46
46
47
it ( 'should error if improperly formatted json is passed in' , async ( ) => {
47
48
try {
48
- nock ( 'https://jamesiv.es' ) . get ( '/' ) . reply ( 200 )
49
-
50
49
await retrieveData ( {
51
50
debug : true ,
52
51
endpoint : 'https://example.com' ,
@@ -61,8 +60,10 @@ describe('fetch', () => {
61
60
} )
62
61
63
62
it ( 'should error if the response is not ok' , async ( ) => {
64
- nock ( 'https://jamesiv.es' ) . post ( '/' ) . reply ( 404 , {
65
- a : 1
63
+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
64
+ ok : false ,
65
+ json : jest . fn ( ) . mockResolvedValue ( { a : 1 } ) ,
66
+ text : jest . fn ( ) . mockResolvedValue ( '{"a":1}' )
66
67
} )
67
68
68
69
try {
@@ -83,18 +84,18 @@ describe('fetch', () => {
83
84
}
84
85
} )
85
86
86
- it ( 'should error if the response is not ok after several retrys ' , async ( ) => {
87
+ it ( 'should error if the response is not ok after several retries ' , async ( ) => {
87
88
jest . setTimeout ( 1000000 )
88
-
89
- try {
90
- nock ( 'https://jives.dev' ) . get ( '/' ) . once ( ) . replyWithError ( {
91
- message : 'This is catastrophic'
92
- } )
93
-
94
- nock ( 'https://jives.dev' ) . get ( '/' ) . reply ( 200 , {
95
- data : '12345'
89
+ global . fetch = jest
90
+ . fn ( )
91
+ . mockRejectedValueOnce ( new Error ( 'This is catastrophic' ) )
92
+ . mockResolvedValueOnce ( {
93
+ ok : true ,
94
+ json : jest . fn ( ) . mockResolvedValue ( { data : '12345' } ) ,
95
+ text : jest . fn ( ) . mockResolvedValue ( '{"data":"12345"}' )
96
96
} )
97
97
98
+ try {
98
99
await retrieveData ( {
99
100
debug : true ,
100
101
endpoint : 'https://jives.dev' ,
@@ -117,7 +118,7 @@ describe('fetch', () => {
117
118
expect ( process . env [ 'fetchApiData' ] ) . toBe ( '{"bestCat":"montezuma"}' )
118
119
} )
119
120
120
- it ( 'should save non standard file types' , async ( ) => {
121
+ it ( 'should save non- standard file types' , async ( ) => {
121
122
await generateExport ( {
122
123
data : 'hello' ,
123
124
format : 'txt' ,
0 commit comments