diff --git a/lib/gui/models/store.js b/lib/gui/models/store.js index 7c6bdf93..8a8f62aa 100644 --- a/lib/gui/models/store.js +++ b/lib/gui/models/store.js @@ -107,6 +107,34 @@ const storeReducer = (state, action) => { throw new Error('Can\'t set the flashing state when not flashing'); } + if (!action.data.type) { + throw new Error('Missing state type'); + } + + if (!_.isString(action.data.type)) { + throw new Error(`Invalid state type: ${action.data.type}`); + } + + if (!action.data.progress) { + throw new Error('Missing state progress'); + } + + if (!_.isNumber(action.data.progress)) { + throw new Error(`Invalid state progress: ${action.data.progress}`); + } + + if (!action.data.eta) { + throw new Error('Missing state eta'); + } + + if (!_.isNumber(action.data.eta)) { + throw new Error(`Invalid state eta: ${action.data.eta}`); + } + + if (!action.data.speed) { + throw new Error('Missing state speed'); + } + return state.set('flashState', Immutable.fromJS(action.data)); } diff --git a/tests/gui/modules/image-writer.spec.js b/tests/gui/modules/image-writer.spec.js index d4faffde..48e3196f 100644 --- a/tests/gui/modules/image-writer.spec.js +++ b/tests/gui/modules/image-writer.spec.js @@ -81,6 +81,86 @@ describe('Browser: ImageWriter', function() { }).to.throw('Can\'t set the flashing state when not flashing'); }); + it('should throw if type is missing', function() { + ImageWriterService.setFlashing(true); + m.chai.expect(function() { + ImageWriterService.setProgressState({ + percentage: 50, + eta: 15, + speed: 100000000000 + }); + }).to.throw('Missing state type'); + }); + + it('should throw if type is not a string', function() { + ImageWriterService.setFlashing(true); + m.chai.expect(function() { + ImageWriterService.setProgressState({ + type: 1234, + percentage: 50, + eta: 15, + speed: 100000000000 + }); + }).to.throw('Invalid state type: 1234'); + }); + + it('should throw if progress is missing', function() { + ImageWriterService.setFlashing(true); + m.chai.expect(function() { + ImageWriterService.setProgressState({ + type: 'write', + eta: 15, + speed: 100000000000 + }); + }).to.throw('Missing state progress'); + }); + + it('should throw if progress is not a number', function() { + ImageWriterService.setFlashing(true); + m.chai.expect(function() { + ImageWriterService.setProgressState({ + type: 'write', + percentage: '50', + eta: 15, + speed: 100000000000 + }); + }).to.throw('Invalid state progress: 50'); + }); + + it('should throw if eta is missing', function() { + ImageWriterService.setFlashing(true); + m.chai.expect(function() { + ImageWriterService.setProgressState({ + type: 'write', + percentage: 50, + speed: 100000000000 + }); + }).to.throw('Missing state eta'); + }); + + it('should throw if eta is not a number', function() { + ImageWriterService.setFlashing(true); + m.chai.expect(function() { + ImageWriterService.setProgressState({ + type: 'write', + percentage: 50, + eta: '15', + speed: 100000000000 + }); + }).to.throw('Invalid state eta: 15'); + }); + + it('should throw if speed is missing', function() { + ImageWriterService.setFlashing(true); + m.chai.expect(function() { + ImageWriterService.setProgressState({ + type: 'write', + percentage: 50, + eta: 15 + }); + }).to.throw('Missing state speed'); + }); + }); describe('.setFlashing()', function() {