@@ -28,9 +28,6 @@ describe(EndpointCache.name, () => {
28
28
Expires : now + CachePeriodInMinutes * 60 * 1000 ,
29
29
} ) ) ;
30
30
31
- const getMaxCachePeriodInMins = ( endpoints : Endpoint [ ] ) =>
32
- Math . max ( ...endpoints . map ( ( endpoint ) => endpoint . CachePeriodInMinutes ) ) ;
33
-
34
31
beforeEach ( ( ) => {
35
32
( ( LRUCache as unknown ) as jest . Mock ) . mockReturnValueOnce ( {
36
33
set,
@@ -60,72 +57,42 @@ describe(EndpointCache.name, () => {
60
57
jest . spyOn ( Date , "now" ) . mockImplementation ( ( ) => now ) ;
61
58
} ) ;
62
59
63
- const verifyHasAndGetCalls = ( ) => {
64
- expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
65
- expect ( has ) . toHaveBeenCalledWith ( key ) ;
66
- expect ( get ) . toHaveBeenCalledTimes ( 1 ) ;
67
- expect ( get ) . toHaveBeenCalledWith ( key ) ;
68
- } ;
69
-
70
- it ( "returns undefined if cache doesn't have key" , ( ) => {
71
- has . mockReturnValueOnce ( false ) ;
72
- expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
73
- expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
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 ) ;
87
- expect ( get ) . not . toHaveBeenCalled ( ) ;
88
- } ) ;
89
-
90
- it ( "returns undefined if cache returns undefined for key" , ( ) => {
91
- get . mockReturnValueOnce ( undefined ) ;
92
- expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
93
- verifyHasAndGetCalls ( ) ;
94
- expect ( set ) . not . toHaveBeenCalled ( ) ;
95
- } ) ;
96
-
97
- it ( "returns undefined if endpoints have expired" , ( ) => {
98
- const maxCachePeriod = getMaxCachePeriodInMins ( mockEndpoints ) ;
99
- jest . spyOn ( Date , "now" ) . mockImplementation ( ( ) => now + ( maxCachePeriod + 1 ) * 60 * 1000 ) ;
100
- expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
101
- verifyHasAndGetCalls ( ) ;
102
- expect ( set ) . toHaveBeenCalledTimes ( 1 ) ;
103
- expect ( set ) . toHaveBeenCalledWith ( key , [ ] ) ;
104
- } ) ;
105
-
106
- describe ( "getEndpoint" , ( ) => {
107
- it ( "returns one of the un-expired endpoints" , ( ) => {
108
- expect ( mockEndpoints . map ( ( endpoint ) => endpoint . Address ) ) . toContain ( endpointCache . getEndpoint ( key ) ) ;
109
- verifyHasAndGetCalls ( ) ;
60
+ describe ( "returns undefined" , ( ) => {
61
+ it ( "if cache doesn't have key" , ( ) => {
62
+ has . mockReturnValueOnce ( false ) ;
63
+ expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
64
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
65
+ expect ( peek ) . not . toHaveBeenCalled ( ) ;
66
+ expect ( get ) . not . toHaveBeenCalled ( ) ;
110
67
expect ( set ) . not . toHaveBeenCalled ( ) ;
111
68
} ) ;
112
69
113
- it ( "returns un-expired endpoint" , ( ) => {
114
- jest . spyOn ( Date , "now" ) . mockImplementation ( ( ) => now + 90 * 1000 ) ;
115
- expect ( endpointCache . getEndpoint ( key ) ) . toEqual ( mockEndpoints [ 1 ] . Address ) ;
116
- verifyHasAndGetCalls ( ) ;
70
+ it ( "if cache has empty array" , ( ) => {
71
+ peek . mockReturnValueOnce ( [ ] ) ;
72
+ expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
73
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
74
+ expect ( peek ) . toHaveBeenCalledWith ( key ) ;
75
+ expect ( get ) . not . toHaveBeenCalled ( ) ;
117
76
expect ( set ) . not . toHaveBeenCalled ( ) ;
118
77
} ) ;
119
78
120
- [ 0 , 1 ] . forEach ( ( index ) => {
121
- it ( `returns un-expired endpoint at index ${ index } ` , ( ) => {
122
- jest . spyOn ( Math , "floor" ) . mockImplementation ( ( ) => index ) ;
123
- expect ( mockEndpoints . map ( ( endpoint ) => endpoint . Address ) ) . toContain ( endpointCache . getEndpoint ( key ) ) ;
124
- verifyHasAndGetCalls ( ) ;
125
- expect ( set ) . not . toHaveBeenCalled ( ) ;
126
- } ) ;
79
+ it ( "if cache returns undefined for key" , ( ) => {
80
+ get . mockReturnValueOnce ( undefined ) ;
81
+ expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
82
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
83
+ expect ( peek ) . toHaveBeenCalledWith ( key ) ;
84
+ expect ( get ) . toHaveBeenCalledWith ( key ) ;
85
+ expect ( set ) . not . toHaveBeenCalled ( ) ;
127
86
} ) ;
128
87
} ) ;
88
+
89
+ it ( "returns endpoints if available" , ( ) => {
90
+ expect ( endpointCache . get ( key ) ) . toEqual ( getEndpointsWithExpiry ( mockEndpoints ) ) ;
91
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
92
+ expect ( peek ) . toHaveBeenCalledWith ( key ) ;
93
+ expect ( get ) . toHaveBeenCalledWith ( key ) ;
94
+ expect ( set ) . not . toHaveBeenCalled ( ) ;
95
+ } ) ;
129
96
} ) ;
130
97
131
98
describe ( "set" , ( ) => {
0 commit comments