diff --git a/src/reducers/brush-tool.js b/src/reducers/brush-tool.js index 0bc422ce..7faca7f6 100644 --- a/src/reducers/brush-tool.js +++ b/src/reducers/brush-tool.js @@ -1,3 +1,5 @@ +import log from '../log/log'; + const CHANGE_BRUSH_SIZE = 'scratch-paint/tools/CHANGE_BRUSH_SIZE'; const initialState = {brushSize: 5}; @@ -5,6 +7,10 @@ const reducer = function (state, action) { if (typeof state === 'undefined') state = initialState; switch (action.type) { case CHANGE_BRUSH_SIZE: + if (isNaN(action.brushSize)) { + log.warn(`Invalid brush size: ${action.brushSize}`); + return state; + } return {brushSize: Math.max(1, action.brushSize)}; default: return state; diff --git a/src/reducers/eraser-tool.js b/src/reducers/eraser-tool.js index a985e666..50000eb4 100644 --- a/src/reducers/eraser-tool.js +++ b/src/reducers/eraser-tool.js @@ -1,3 +1,5 @@ +import log from '../log/log'; + const CHANGE_ERASER_SIZE = 'scratch-paint/tools/CHANGE_ERASER_SIZE'; const initialState = {brushSize: 20}; @@ -5,6 +7,10 @@ const reducer = function (state, action) { if (typeof state === 'undefined') state = initialState; switch (action.type) { case CHANGE_ERASER_SIZE: + if (isNaN(action.brushSize)) { + log.warn(`Invalid brush size: ${action.brushSize}`); + return state; + } return {brushSize: Math.max(1, action.brushSize)}; default: return state; diff --git a/test/unit/blob-tool-reducer.test.js b/test/unit/blob-tool-reducer.test.js new file mode 100644 index 00000000..4ffe37fe --- /dev/null +++ b/test/unit/blob-tool-reducer.test.js @@ -0,0 +1,42 @@ +/* eslint-env jest */ +import brushReducer from '../../src/reducers/brush-tool'; +import eraserReducer from '../../src/reducers/eraser-tool'; + +test('initialState', () => { + let defaultState; + + expect(brushReducer(defaultState /* state */, {type: 'anything'} /* action */)).toBeDefined(); + expect(brushReducer(defaultState /* state */, {type: 'anything'} /* action */).brushSize).toBeGreaterThan(0); + + expect(eraserReducer(defaultState /* state */, {type: 'anything'} /* action */)).toBeTruthy(); + expect(eraserReducer(defaultState /* state */, {type: 'anything'} /* action */).brushSize).toBeGreaterThan(0); +}); + +test('changeBrushSize', () => { + let defaultState; + const newBrushSize = 8078; + + expect(brushReducer(defaultState /* state */, brushReducer.changeBrushSize(newBrushSize) /* action */)) + .toEqual({brushSize: newBrushSize}); + expect(brushReducer(1 /* state */, brushReducer.changeBrushSize(newBrushSize) /* action */)) + .toEqual({brushSize: newBrushSize}); + + expect(eraserReducer(defaultState /* state */, eraserReducer.changeBrushSize(newBrushSize) /* action */)) + .toEqual({brushSize: newBrushSize}); + expect(eraserReducer(1 /* state */, eraserReducer.changeBrushSize(newBrushSize) /* action */)) + .toEqual({brushSize: newBrushSize}); +}); + +test('invalidChangeBrushSize', () => { + const origState = {brushSize: 1}; + + expect(brushReducer(origState /* state */, brushReducer.changeBrushSize('invalid argument') /* action */)) + .toBe(origState); + expect(brushReducer(origState /* state */, brushReducer.changeBrushSize() /* action */)) + .toBe(origState); + + expect(eraserReducer(origState /* state */, eraserReducer.changeBrushSize('invalid argument') /* action */)) + .toBe(origState); + expect(eraserReducer(origState /* state */, eraserReducer.changeBrushSize() /* action */)) + .toBe(origState); +});