diff --git a/package.json b/package.json index 1bbb94a..e39c39f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ha-store", - "version": "1.7.0", + "version": "1.7.1", "description": "Efficient data fetching", "main": "src/index.js", "scripts": { diff --git a/src/breaker.js b/src/breaker.js index 00b4ac1..136b6b6 100644 --- a/src/breaker.js +++ b/src/breaker.js @@ -9,6 +9,7 @@ const {tween} = require('./utils.js'); /* Methods -------------------------------------------------------------------*/ function breaker(config, emitter) { + config = config || {}; let active = false; const circuitError = new Error('Service unavailable (circuit-breaker)'); let timer = null; diff --git a/src/index.js b/src/index.js index e9be15d..480e73b 100644 --- a/src/index.js +++ b/src/index.js @@ -33,7 +33,7 @@ class HaStore extends EventEmitter { this.setMaxListeners(Infinity); } - this.breaker = breaker(this.config.breaker, this); + this.breaker = breaker(this.config, this); this.queue = queue( this.config, diff --git a/tests/unit/breaker.spec.js b/tests/unit/breaker.spec.js index 031bbc4..292a11c 100644 --- a/tests/unit/breaker.spec.js +++ b/tests/unit/breaker.spec.js @@ -23,6 +23,11 @@ const config = { }; /* Tests ---------------------------------------------------------------------*/ +const varToString = (value) => { + return typeof value === 'object' + ? JSON.stringify(value) + : `${value}`; +} describe('breaker', () => { let testCircuit; @@ -30,25 +35,32 @@ describe('breaker', () => { const assertIsInactive = (status) => expect(status).to.be.deep.equal({active: false, step: 0, ttl: undefined}); - it('should be a noop if no configuration is supplied', () => { - const stubbedEmitter = new EventEmitter(); - sinon.spy(stubbedEmitter, 'emit'); + [ + {breaker: null}, + undefined, + null, + {}, + ].forEach((config)=>( + it(`should be a noop if no configuration (${varToString(config)}) is supplied`, () => { + const stubbedEmitter = new EventEmitter(); + sinon.spy(stubbedEmitter, 'emit'); - const testBreaker = breaker({breaker: null}, stubbedEmitter); - assertIsInactive(testBreaker.status()); + const testBreaker = breaker(config, stubbedEmitter); + assertIsInactive(testBreaker.status()); - testBreaker.openCircuit(); - assertIsInactive(testBreaker.status()); + testBreaker.openCircuit(); + assertIsInactive(testBreaker.status()); - testBreaker.restoreCircuit(); - assertIsInactive(testBreaker.status()); + testBreaker.restoreCircuit(); + assertIsInactive(testBreaker.status()); - testBreaker.closeCircuit(); - assertIsInactive(testBreaker.status()); + testBreaker.closeCircuit(); + assertIsInactive(testBreaker.status()); - sinon.assert.notCalled(stubbedEmitter.emit); + sinon.assert.notCalled(stubbedEmitter.emit); + }) + )); - }); describe('#closeCircuit', () => { beforeEach(() => {