From 85c73cee49e731bba331fdda6ed4fb50e7c09d28 Mon Sep 17 00:00:00 2001 From: Matt Hinchliffe Date: Fri, 7 Dec 2018 14:31:22 +0000 Subject: [PATCH] =?UTF-8?q?Add=20a=20spec=20for=20evented=20queue=20module?= =?UTF-8?q?=20=20=F0=9F=90=BF=20v2.10.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/event-queue.js | 10 +++--- src/run-parallel.js | 2 +- test/src/event-queue.spec.js | 63 ++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 test/src/event-queue.spec.js diff --git a/src/event-queue.js b/src/event-queue.js index fe40e62..28c900e 100644 --- a/src/event-queue.js +++ b/src/event-queue.js @@ -8,27 +8,27 @@ class EventQueue extends EventEmitter { add(item) { this.queue.add(item); - this.emit('queue:add', item); + this.emit('add', item); } delete(item) { this.queue.delete(item); - this.emit('queue:delete', item); + this.emit('delete', item); } // Resolves when the queue contains none of the given items - done(items = []) { + waitFor(items = []) { return new Promise((resolve) => { const callback = () => { const noItemsRunning = items.every((item) => !this.queue.has(item)); if (noItemsRunning) { - this.off('queue:delete', callback); + this.off('delete', callback); resolve(); } }; - this.on('queue:delete', callback); + this.on('delete', callback); callback(); }); } diff --git a/src/run-parallel.js b/src/run-parallel.js index bd95d15..eb27a38 100644 --- a/src/run-parallel.js +++ b/src/run-parallel.js @@ -16,7 +16,7 @@ module.exports = (tasks = [], concurrency = 1) => { // Queue the package now to maintain running order... queue.add(pkg.name); // ...but wait for any dependencies in the queue to finish - return queue.done(pkg.allDependencies); + return queue.waitFor(pkg.allDependencies); }) .then(() => { return apply(); diff --git a/test/src/event-queue.spec.js b/test/src/event-queue.spec.js new file mode 100644 index 0000000..bed29cb --- /dev/null +++ b/test/src/event-queue.spec.js @@ -0,0 +1,63 @@ +const Subject = require('../../src/event-queue'); + +describe('src/event-queue', () => { + let instance; + + beforeEach(() => { + instance = new Subject(); + }); + + describe('#add', () => { + it('adds the given item to the queue', () => { + instance.add('foo'); + expect(instance.queue.size).toEqual(1); + }); + + it('emits an event when items are added to the queue', (done) => { + instance.on('add', (item) => { + expect(item).toEqual('foo'); + done(); + }); + + instance.add('foo'); + }); + }); + + describe('#delete', () => { + it('removes the given item to the queue', () => { + instance.add('foo'); + expect(instance.queue.size).toEqual(1); + + instance.delete('foo'); + expect(instance.queue.size).toEqual(0); + }); + + it('emits an event when items are removed from the queue', (done) => { + instance.add('foo'); + + instance.on('delete', (item) => { + expect(item).toEqual('foo'); + done(); + }); + + instance.delete('foo'); + }); + }); + + describe('#waitFor', () => { + it('resolves when the queue does not contain any of the given items', (done) => { + instance.add('foo'); + instance.add('bar'); + instance.add('baz'); + + instance.waitFor(['foo', 'bar', 'baz']).then(() => { + expect(instance.queue.size).toEqual(0); + done(); + }); + + instance.delete('foo'); + instance.delete('bar'); + instance.delete('baz'); + }); + }); +});