@@ -256,4 +256,116 @@ describe('WorkdirContextPool', function() {
256
256
assert . isFalse ( pool . getContext ( dir2 ) . isPresent ( ) ) ;
257
257
} ) ;
258
258
} ) ;
259
+
260
+ describe ( 'emitter' , function ( ) {
261
+ describe ( 'did-change-contexts' , function ( ) {
262
+ let dir0 , dir1 , dir2 , emitterSpy ;
263
+
264
+ beforeEach ( async function ( ) {
265
+ [ dir0 , dir1 , dir2 ] = await Promise . all ( [
266
+ cloneRepository ( 'three-files' ) ,
267
+ cloneRepository ( 'three-files' ) ,
268
+ cloneRepository ( 'three-files' ) ,
269
+ ] ) ;
270
+
271
+ pool . add ( dir0 ) ;
272
+
273
+ emitterSpy = sinon . spy ( ) ;
274
+
275
+ pool . onDidChangePoolContexts ( emitterSpy ) ;
276
+ } ) ;
277
+
278
+ it ( 'emits only once for set' , function ( ) {
279
+ pool . set ( new Set ( [ dir1 , dir2 ] ) ) ;
280
+ assert . isTrue ( emitterSpy . calledOnce ) ;
281
+ assert . deepEqual (
282
+ emitterSpy . getCall ( 0 ) . args [ 0 ] ,
283
+ { added : new Set ( [ dir1 , dir2 ] ) , removed : new Set ( [ dir0 ] ) }
284
+ ) ;
285
+ } ) ;
286
+
287
+ it ( 'does not emit for set when no changes are made' , function ( ) {
288
+ pool . set ( new Set ( [ dir0 ] ) ) ;
289
+ assert . isFalse ( emitterSpy . called ) ;
290
+ } ) ;
291
+
292
+ it ( 'emits only once for clear' , function ( ) {
293
+ pool . clear ( ) ;
294
+ assert . isTrue ( emitterSpy . calledOnce ) ;
295
+ assert . deepEqual (
296
+ emitterSpy . getCall ( 0 ) . args [ 0 ] ,
297
+ { removed : new Set ( [ dir0 ] ) }
298
+ ) ;
299
+ } ) ;
300
+
301
+ it ( 'does not emit for clear when no changes are made' , function ( ) {
302
+ pool . clear ( ) ;
303
+ emitterSpy . resetHistory ( ) ;
304
+ pool . clear ( ) ; // Should not emit
305
+ assert . isFalse ( emitterSpy . called ) ;
306
+ } ) ;
307
+
308
+ it ( 'emits only once for replace' , function ( ) {
309
+ pool . replace ( dir0 ) ;
310
+ assert . isTrue ( emitterSpy . calledOnce ) ;
311
+ assert . deepEqual (
312
+ emitterSpy . getCall ( 0 ) . args [ 0 ] ,
313
+ { altered : new Set ( [ dir0 ] ) }
314
+ ) ;
315
+ } ) ;
316
+
317
+ it ( 'emits for every new add' , function ( ) {
318
+ pool . remove ( dir0 ) ;
319
+ emitterSpy . resetHistory ( ) ;
320
+ pool . add ( dir0 ) ;
321
+ pool . add ( dir1 ) ;
322
+ pool . add ( dir2 ) ;
323
+ assert . isTrue ( emitterSpy . calledThrice ) ;
324
+ assert . deepEqual (
325
+ emitterSpy . getCall ( 0 ) . args [ 0 ] ,
326
+ { added : new Set ( [ dir0 ] ) }
327
+ ) ;
328
+ assert . deepEqual (
329
+ emitterSpy . getCall ( 1 ) . args [ 0 ] ,
330
+ { added : new Set ( [ dir1 ] ) }
331
+ ) ;
332
+ assert . deepEqual (
333
+ emitterSpy . getCall ( 2 ) . args [ 0 ] ,
334
+ { added : new Set ( [ dir2 ] ) }
335
+ ) ;
336
+ } ) ;
337
+
338
+ it ( 'does not emit for add when a context already exists' , function ( ) {
339
+ pool . add ( dir0 ) ;
340
+ assert . isFalse ( emitterSpy . called ) ;
341
+ } ) ;
342
+
343
+ it ( 'emits for every remove' , function ( ) {
344
+ pool . add ( dir1 ) ;
345
+ pool . add ( dir2 ) ;
346
+ emitterSpy . resetHistory ( ) ;
347
+ pool . remove ( dir0 ) ;
348
+ pool . remove ( dir1 ) ;
349
+ pool . remove ( dir2 ) ;
350
+ assert . isTrue ( emitterSpy . calledThrice ) ;
351
+ assert . deepEqual (
352
+ emitterSpy . getCall ( 0 ) . args [ 0 ] ,
353
+ { removed : new Set ( [ dir0 ] ) }
354
+ ) ;
355
+ assert . deepEqual (
356
+ emitterSpy . getCall ( 1 ) . args [ 0 ] ,
357
+ { removed : new Set ( [ dir1 ] ) }
358
+ ) ;
359
+ assert . deepEqual (
360
+ emitterSpy . getCall ( 2 ) . args [ 0 ] ,
361
+ { removed : new Set ( [ dir2 ] ) }
362
+ ) ;
363
+ } ) ;
364
+
365
+ it ( 'does not emit for remove when a context did not exist' , function ( ) {
366
+ pool . remove ( dir1 ) ;
367
+ assert . isFalse ( emitterSpy . called ) ;
368
+ } ) ;
369
+ } )
370
+ } ) ;
259
371
} ) ;
0 commit comments