Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 7a0315a

Browse files
add work dir pool tests
1 parent 11d39d1 commit 7a0315a

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

lib/models/workdir-context-pool.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ export default class WorkdirContextPool {
8383
}
8484
}
8585

86-
remove(directory, options = {}, silenceEmitter = false) {
86+
remove(directory, silenceEmitter = false) {
8787
const existing = this.contexts.get(directory);
8888
this.contexts.delete(directory);
8989

9090
if (existing) {
9191
existing.destroy();
92-
}
9392

94-
if (!silenceEmitter) {
95-
this.emitter.emit('did-change-contexts', {removed: new Set([directory])});
93+
if (!silenceEmitter) {
94+
this.emitter.emit('did-change-contexts', {removed: new Set([directory])});
95+
}
9696
}
9797
}
9898

test/models/workdir-context-pool.test.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,116 @@ describe('WorkdirContextPool', function() {
256256
assert.isFalse(pool.getContext(dir2).isPresent());
257257
});
258258
});
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+
});
259371
});

0 commit comments

Comments
 (0)