@@ -183,4 +183,67 @@ describe('RevisionCache', () => {
183
183
] ) ,
184
184
) ;
185
185
} ) ;
186
+
187
+ it ( 'should not mutate previous feature-updated events when new events with the same feature name are added' , ( ) => {
188
+ const baseEvent : DeltaHydrationEvent = {
189
+ eventId : 1 ,
190
+ features : [
191
+ {
192
+ name : 'base-flag' ,
193
+ type : 'release' ,
194
+ enabled : true ,
195
+ project : 'streaming-deltas' ,
196
+ stale : false ,
197
+ strategies : [ ] ,
198
+ variants : [ ] ,
199
+ description : null ,
200
+ impressionData : false ,
201
+ } ,
202
+ ] ,
203
+ type : 'hydration' ,
204
+ segments : [ ] ,
205
+ } ;
206
+
207
+ const deltaCache = new DeltaCache ( baseEvent , 10 ) ;
208
+
209
+ const initialFeatureEvent : DeltaEvent = {
210
+ eventId : 129 ,
211
+ type : DELTA_EVENT_TYPES . FEATURE_UPDATED ,
212
+ feature : {
213
+ impressionData : false ,
214
+ enabled : false ,
215
+ name : 'streaming-test' ,
216
+ description : null ,
217
+ project : 'streaming-deltas' ,
218
+ stale : false ,
219
+ type : 'release' ,
220
+ variants : [ ] ,
221
+ strategies : [ ] ,
222
+ } ,
223
+ } ;
224
+ // This tests is to verify that the initialFeatureEvent is not mutated when a new event with the same feature name is added
225
+ // the following dirty way to clone this object is to avoid mutation on the comparison object. Because the object is passed by reference
226
+ // we would be comparing the same object with itself which would cause the expect check to always pass because the comparison would
227
+ // also change to match the object being compared.
228
+ deltaCache . addEvents ( [ JSON . parse ( JSON . stringify ( initialFeatureEvent ) ) ] ) ;
229
+
230
+ const updatedFeatureEvent : DeltaEvent = {
231
+ eventId : 130 ,
232
+ type : DELTA_EVENT_TYPES . FEATURE_UPDATED ,
233
+ feature : {
234
+ impressionData : false ,
235
+ enabled : true ,
236
+ name : 'streaming-test' ,
237
+ description : null ,
238
+ project : 'streaming-deltas' ,
239
+ stale : false ,
240
+ type : 'release' ,
241
+ variants : [ ] ,
242
+ strategies : [ { name : 'new-strategy' , parameters : { } } ] ,
243
+ } ,
244
+ } ;
245
+ deltaCache . addEvents ( [ updatedFeatureEvent ] ) ;
246
+ // @ts -ignore
247
+ expect ( deltaCache . events [ 1 ] ) . toStrictEqual ( initialFeatureEvent ) ;
248
+ } ) ;
186
249
} ) ;
0 commit comments