@@ -79,24 +79,88 @@ describe('httpPollingDatafileManager', () => {
79
79
jest . restoreAllMocks ( )
80
80
} )
81
81
82
- describe ( 'when constructed with sdkKey and datafile' , ( ) => {
82
+ describe ( 'when constructed with sdkKey and datafile and autoUpdate: true, ' , ( ) => {
83
83
beforeEach ( ( ) => {
84
- manager = createTestManager ( { datafile : { foo : 'abcd' } , sdkKey : '123' } )
84
+ manager = createTestManager ( { datafile : { foo : 'abcd' } , sdkKey : '123' , autoUpdate : true } )
85
85
} )
86
86
87
87
it ( 'returns the passed datafile from get' , ( ) => {
88
88
expect ( manager . get ( ) ) . toEqual ( { foo : 'abcd' } )
89
89
} )
90
90
91
- it ( 'after being started, fetches the datafile and resolves onReady' , async ( ) => {
91
+ it ( 'resolves onReady immediately' , async ( ) => {
92
+ manager . start ( )
93
+ await manager . onReady ( )
94
+ expect ( manager . get ( ) ) . toEqual ( { foo : 'abcd' } )
95
+ } )
96
+
97
+ it ( 'after being started, fetches the datafile, updates itself, emits an update event, and updates itself again after a timeout' , async ( ) => {
98
+ manager . queuedResponses . push (
99
+ {
100
+ statusCode : 200 ,
101
+ body : '{"fooz": "barz"}' ,
102
+ headers : { }
103
+ } ,
104
+ {
105
+ statusCode : 200 ,
106
+ body : '{"foo": "bar"}' ,
107
+ headers : { }
108
+ }
109
+ )
110
+ const updateFn = jest . fn ( )
111
+ manager . on ( 'update' , updateFn )
112
+ manager . start ( )
113
+ expect ( manager . responsePromises . length ) . toBe ( 1 )
114
+ await manager . responsePromises [ 0 ]
115
+ expect ( updateFn ) . toBeCalledTimes ( 1 )
116
+ expect ( updateFn ) . toBeCalledWith ( {
117
+ datafile : { foo : 'bar' }
118
+ } )
119
+ expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } )
120
+ updateFn . mockReset ( )
121
+ testTimeoutFactory . timeoutFns [ 0 ] ( )
122
+ expect ( manager . responsePromises . length ) . toBe ( 2 )
123
+ await manager . responsePromises [ 1 ]
124
+ expect ( updateFn ) . toBeCalledTimes ( 1 )
125
+ expect ( updateFn ) . toBeCalledWith ( {
126
+ datafile : { fooz : 'barz' }
127
+ } )
128
+ expect ( manager . get ( ) ) . toEqual ( { fooz : 'barz' } )
129
+ } )
130
+ } )
131
+
132
+ describe ( 'when constructed with sdkKey and datafile and autoUpdate: false,' , ( ) => {
133
+ beforeEach ( ( ) => {
134
+ manager = createTestManager ( { datafile : { foo : 'abcd' } , sdkKey : '123' , autoUpdate : false } )
135
+ } )
136
+
137
+ it ( 'returns the passed datafile from get' , ( ) => {
138
+ expect ( manager . get ( ) ) . toEqual ( { foo : 'abcd' } )
139
+ } )
140
+
141
+ it ( 'after being started, resolves onReady immediately' , async ( ) => {
142
+ manager . start ( )
143
+ await manager . onReady ( )
144
+ expect ( manager . get ( ) ) . toEqual ( { foo : 'abcd' } )
145
+ } )
146
+
147
+ it ( 'after being started, fetches the datafile, updates itself once, and emits an update event, but does not schedule a future update' , async ( ) => {
92
148
manager . queuedResponses . push ( {
93
149
statusCode : 200 ,
94
150
body : '{"foo": "bar"}' ,
95
151
headers : { }
96
152
} )
153
+ const updateFn = jest . fn ( )
154
+ manager . on ( 'update' , updateFn )
97
155
manager . start ( )
98
- await manager . onReady ( )
156
+ expect ( manager . responsePromises . length ) . toBe ( 1 )
157
+ await manager . responsePromises [ 0 ]
158
+ expect ( updateFn ) . toBeCalledTimes ( 1 )
159
+ expect ( updateFn ) . toBeCalledWith ( {
160
+ datafile : { foo : 'bar' }
161
+ } )
99
162
expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } )
163
+ expect ( testTimeoutFactory . timeoutFns . length ) . toBe ( 0 )
100
164
} )
101
165
} )
102
166
0 commit comments