1
- import { act , renderHook , waitFor } from " @testing-library/react" ;
2
- import { render } from " @testing-library/react" ;
3
- import useRequest , { clearCache } from " ../index" ;
4
- import { request } from " ../../utils/testingHelpers" ;
5
- import React , { useState } from " react" ;
6
- import " jest-localstorage-mock" ;
7
-
8
- describe ( " useCachePlugin" , ( ) => {
1
+ import { act , renderHook , waitFor } from ' @testing-library/react' ;
2
+ import { render } from ' @testing-library/react' ;
3
+ import useRequest , { clearCache } from ' ../index' ;
4
+ import { request } from ' ../../utils/testingHelpers' ;
5
+ import React , { useState } from ' react' ;
6
+ import ' jest-localstorage-mock' ;
7
+
8
+ describe ( ' useCachePlugin' , ( ) => {
9
9
jest . useFakeTimers ( ) ;
10
10
11
- const setup = ( service , options ) =>
12
- renderHook ( ( ) => useRequest ( service , options ) ) ;
11
+ const setup = ( service , options ) => renderHook ( ( ) => useRequest ( service , options ) ) ;
13
12
14
13
const testCacheKey = async ( options : any ) => {
15
14
const hook = setup ( request , options ) ;
@@ -18,79 +17,79 @@ describe("useCachePlugin", () => {
18
17
jest . advanceTimersByTime ( 1000 ) ;
19
18
} ) ;
20
19
expect ( hook . result . current . loading ) . toBe ( false ) ;
21
- expect ( hook . result . current . data ) . toBe ( " success" ) ;
20
+ expect ( hook . result . current . data ) . toBe ( ' success' ) ;
22
21
hook . unmount ( ) ;
23
22
} ;
24
23
25
- it ( " useRequest cacheKey should work" , async ( ) => {
24
+ it ( ' useRequest cacheKey should work' , async ( ) => {
26
25
await testCacheKey ( {
27
- cacheKey : " testCacheKey" ,
26
+ cacheKey : ' testCacheKey' ,
28
27
} ) ;
29
28
30
29
jest . advanceTimersByTime ( 100 ) ;
31
30
32
31
const hook2 = setup ( request , {
33
- cacheKey : " testCacheKey" ,
32
+ cacheKey : ' testCacheKey' ,
34
33
} ) ;
35
34
expect ( hook2 . result . current . loading ) . toBe ( true ) ;
36
- expect ( hook2 . result . current . data ) . toBe ( " success" ) ;
35
+ expect ( hook2 . result . current . data ) . toBe ( ' success' ) ;
37
36
await act ( async ( ) => {
38
37
jest . advanceTimersByTime ( 1000 ) ;
39
38
} ) ;
40
39
expect ( hook2 . result . current . loading ) . toBe ( false ) ;
41
40
} ) ;
42
41
43
- it ( " useRequest staleTime should work" , async ( ) => {
42
+ it ( ' useRequest staleTime should work' , async ( ) => {
44
43
await testCacheKey ( {
45
- cacheKey : " testStaleTime" ,
44
+ cacheKey : ' testStaleTime' ,
46
45
staleTime : 3000 ,
47
46
} ) ;
48
47
49
48
jest . advanceTimersByTime ( 1000 ) ;
50
49
51
50
const hook2 = setup ( request , {
52
- cacheKey : " testStaleTime" ,
51
+ cacheKey : ' testStaleTime' ,
53
52
staleTime : 3000 ,
54
53
} ) ;
55
54
expect ( hook2 . result . current . loading ) . toBe ( false ) ;
56
- expect ( hook2 . result . current . data ) . toBe ( " success" ) ;
55
+ expect ( hook2 . result . current . data ) . toBe ( ' success' ) ;
57
56
hook2 . unmount ( ) ;
58
57
59
58
jest . advanceTimersByTime ( 3001 ) ;
60
59
61
60
const hook3 = setup ( request , {
62
- cacheKey : " testStaleTime" ,
61
+ cacheKey : ' testStaleTime' ,
63
62
staleTime : 3000 ,
64
63
} ) ;
65
64
expect ( hook3 . result . current . loading ) . toBe ( true ) ;
66
- expect ( hook3 . result . current . data ) . toBe ( " success" ) ;
65
+ expect ( hook3 . result . current . data ) . toBe ( ' success' ) ;
67
66
68
67
await act ( async ( ) => {
69
68
jest . advanceTimersByTime ( 1000 ) ;
70
69
} ) ;
71
70
expect ( hook3 . result . current . loading ) . toBe ( false ) ;
72
71
} ) ;
73
72
74
- it ( " useRequest cacheTime should work" , async ( ) => {
73
+ it ( ' useRequest cacheTime should work' , async ( ) => {
75
74
await testCacheKey ( {
76
- cacheKey : " testCacheTime" ,
75
+ cacheKey : ' testCacheTime' ,
77
76
cacheTime : 5000 ,
78
77
} ) ;
79
78
80
79
jest . advanceTimersByTime ( 1000 ) ;
81
80
82
81
const hook2 = setup ( request , {
83
- cacheKey : " testCacheTime" ,
82
+ cacheKey : ' testCacheTime' ,
84
83
cacheTime : 5000 ,
85
84
} ) ;
86
85
expect ( hook2 . result . current . loading ) . toBe ( true ) ;
87
- expect ( hook2 . result . current . data ) . toBe ( " success" ) ;
86
+ expect ( hook2 . result . current . data ) . toBe ( ' success' ) ;
88
87
hook2 . unmount ( ) ;
89
88
90
89
jest . advanceTimersByTime ( 5001 ) ;
91
90
92
91
const hook3 = setup ( request , {
93
- cacheKey : " testCacheTime" ,
92
+ cacheKey : ' testCacheTime' ,
94
93
cacheTime : 5000 ,
95
94
} ) ;
96
95
expect ( hook3 . result . current . loading ) . toBe ( true ) ;
@@ -100,46 +99,46 @@ describe("useCachePlugin", () => {
100
99
jest . advanceTimersByTime ( 1000 ) ;
101
100
} ) ;
102
101
expect ( hook3 . result . current . loading ) . toBe ( false ) ;
103
- expect ( hook3 . result . current . data ) . toBe ( " success" ) ;
102
+ expect ( hook3 . result . current . data ) . toBe ( ' success' ) ;
104
103
} ) ;
105
104
106
- it ( " clearCache should work" , async ( ) => {
107
- await testCacheKey ( " testClearCache" ) ;
105
+ it ( ' clearCache should work' , async ( ) => {
106
+ await testCacheKey ( ' testClearCache' ) ;
108
107
109
- clearCache ( " testClearCache" ) ;
108
+ clearCache ( ' testClearCache' ) ;
110
109
const hook2 = setup ( request , {
111
- cacheKey : " testClearCache" ,
110
+ cacheKey : ' testClearCache' ,
112
111
} ) ;
113
112
expect ( hook2 . result . current . loading ) . toBe ( true ) ;
114
113
expect ( hook2 . result . current . data ) . toBeUndefined ( ) ;
115
114
} ) ;
116
115
117
- it ( " setCache/getCache should work" , async ( ) => {
116
+ it ( ' setCache/getCache should work' , async ( ) => {
118
117
const cacheKey = `setCacheKey` ;
119
118
await testCacheKey ( {
120
119
cacheKey,
121
120
setCache : ( data ) => localStorage . setItem ( cacheKey , JSON . stringify ( data ) ) ,
122
- getCache : ( ) => JSON . parse ( localStorage . getItem ( cacheKey ) || "{}" ) ,
121
+ getCache : ( ) => JSON . parse ( localStorage . getItem ( cacheKey ) || '{}' ) ,
123
122
} ) ;
124
123
125
124
jest . advanceTimersByTime ( 1000 ) ;
126
125
const hook2 = setup ( request , {
127
126
cacheKey,
128
127
setCache : ( data ) => localStorage . setItem ( cacheKey , JSON . stringify ( data ) ) ,
129
- getCache : ( ) => JSON . parse ( localStorage . getItem ( cacheKey ) || "{}" ) ,
128
+ getCache : ( ) => JSON . parse ( localStorage . getItem ( cacheKey ) || '{}' ) ,
130
129
} ) ;
131
130
expect ( hook2 . result . current . loading ) . toBe ( true ) ;
132
- expect ( hook2 . result . current . data ) . toBe ( " success" ) ;
131
+ expect ( hook2 . result . current . data ) . toBe ( ' success' ) ;
133
132
134
133
await act ( async ( ) => {
135
134
jest . advanceTimersByTime ( 1000 ) ;
136
135
} ) ;
137
136
expect ( hook2 . result . current . loading ) . toBe ( false ) ;
138
137
} ) ;
139
138
140
- it ( " cache should work when change data immediately" , async ( ) => {
139
+ it ( ' cache should work when change data immediately' , async ( ) => {
141
140
const { result } = setup ( request , {
142
- cacheKey : " mutateCacheKey" ,
141
+ cacheKey : ' mutateCacheKey' ,
143
142
} ) ;
144
143
act ( ( ) => {
145
144
result . current . mutate ( 1 ) ;
@@ -149,12 +148,12 @@ describe("useCachePlugin", () => {
149
148
jest . advanceTimersByTime ( 1000 ) ;
150
149
} ) ;
151
150
expect ( result . current . loading ) . toBe ( false ) ;
152
- expect ( result . current . data ) . toBe ( " success" ) ;
151
+ expect ( result . current . data ) . toBe ( ' success' ) ;
153
152
} ) ;
154
153
155
154
//github.com/alibaba/hooks/issues/1859
156
- it ( " error should reset with activeKey" , async ( ) => {
157
- const errSpy = jest . spyOn ( console , " error" ) . mockImplementation ( ( ) => { } ) ;
155
+ it ( ' error should reset with activeKey' , async ( ) => {
156
+ const errSpy = jest . spyOn ( console , ' error' ) . mockImplementation ( ( ) => { } ) ;
158
157
159
158
let res = { } as any ;
160
159
const TestComponent = ( ) => {
0 commit comments