@@ -13,6 +13,7 @@ describe(EndpointCache.name, () => {
13
13
const now = Date . now ( ) ;
14
14
const set = jest . fn ( ) ;
15
15
const get = jest . fn ( ) ;
16
+ const peek = jest . fn ( ) ;
16
17
const has = jest . fn ( ) ;
17
18
const clear = jest . fn ( ) ;
18
19
@@ -34,6 +35,7 @@ describe(EndpointCache.name, () => {
34
35
( ( LRUCache as unknown ) as jest . Mock ) . mockReturnValueOnce ( {
35
36
set,
36
37
get,
38
+ peek,
37
39
has,
38
40
clear,
39
41
} ) ;
@@ -52,7 +54,9 @@ describe(EndpointCache.name, () => {
52
54
describe ( "get" , ( ) => {
53
55
beforeEach ( ( ) => {
54
56
has . mockReturnValue ( true ) ;
55
- get . mockReturnValue ( getEndpointsWithExpiry ( mockEndpoints ) ) ;
57
+ const endpointsWithExpiry = getEndpointsWithExpiry ( mockEndpoints ) ;
58
+ peek . mockReturnValue ( endpointsWithExpiry ) ;
59
+ get . mockReturnValue ( endpointsWithExpiry ) ;
56
60
jest . spyOn ( Date , "now" ) . mockImplementation ( ( ) => now ) ;
57
61
} ) ;
58
62
@@ -68,6 +72,18 @@ describe(EndpointCache.name, () => {
68
72
expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
69
73
expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
70
74
expect ( has ) . toHaveBeenCalledWith ( key ) ;
75
+ expect ( peek ) . not . toHaveBeenCalled ( ) ;
76
+ expect ( get ) . not . toHaveBeenCalled ( ) ;
77
+ } ) ;
78
+
79
+ it ( "returns undefined if cache has empty array" , ( ) => {
80
+ has . mockReturnValueOnce ( true ) ;
81
+ peek . mockReturnValueOnce ( [ ] ) ;
82
+ expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
83
+ expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
84
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
85
+ expect ( peek ) . toHaveBeenCalledTimes ( 1 ) ;
86
+ expect ( peek ) . toHaveBeenCalledWith ( key ) ;
71
87
expect ( get ) . not . toHaveBeenCalled ( ) ;
72
88
} ) ;
73
89
@@ -148,10 +164,39 @@ describe(EndpointCache.name, () => {
148
164
expect ( set ) . toHaveBeenCalledWith ( key , [ ] ) ;
149
165
} ) ;
150
166
151
- it ( "has" , ( ) => {
152
- endpointCache . has ( key ) ;
153
- expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
154
- expect ( has ) . toHaveBeenCalledWith ( key ) ;
167
+ describe ( "has" , ( ) => {
168
+ describe ( "returns false" , ( ) => {
169
+ it ( "when key is not present" , ( ) => {
170
+ has . mockReturnValueOnce ( false ) ;
171
+ expect ( endpointCache . has ( key ) ) . toEqual ( false ) ;
172
+ expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
173
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
174
+ } ) ;
175
+
176
+ it ( "when key is present and value is empty" , ( ) => {
177
+ has . mockReturnValueOnce ( true ) ;
178
+ peek . mockReturnValueOnce ( [ ] ) ;
179
+ expect ( endpointCache . has ( key ) ) . toEqual ( false ) ;
180
+ expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
181
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
182
+ } ) ;
183
+
184
+ it ( "when key is present and value is undefined" , ( ) => {
185
+ has . mockReturnValueOnce ( true ) ;
186
+ peek . mockReturnValueOnce ( undefined ) ;
187
+ expect ( endpointCache . has ( key ) ) . toEqual ( false ) ;
188
+ expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
189
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
190
+ } ) ;
191
+ } ) ;
192
+
193
+ it ( "returns true when key is present and value is non-empty" , ( ) => {
194
+ has . mockReturnValueOnce ( true ) ;
195
+ peek . mockReturnValueOnce ( getEndpointsWithExpiry ( mockEndpoints ) ) ;
196
+ expect ( endpointCache . has ( key ) ) . toEqual ( true ) ;
197
+ expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
198
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
199
+ } ) ;
155
200
} ) ;
156
201
157
202
it ( "clear" , ( ) => {
0 commit comments