add tests

This commit is contained in:
DD Liu 2017-07-27 22:58:31 -04:00
parent 27f7102b06
commit 975bfdc464
3 changed files with 54 additions and 0 deletions

View file

@ -1,3 +1,5 @@
import log from '../log/log';
const CHANGE_BRUSH_SIZE = 'scratch-paint/tools/CHANGE_BRUSH_SIZE'; const CHANGE_BRUSH_SIZE = 'scratch-paint/tools/CHANGE_BRUSH_SIZE';
const initialState = {brushSize: 5}; const initialState = {brushSize: 5};
@ -5,6 +7,10 @@ const reducer = function (state, action) {
if (typeof state === 'undefined') state = initialState; if (typeof state === 'undefined') state = initialState;
switch (action.type) { switch (action.type) {
case CHANGE_BRUSH_SIZE: case CHANGE_BRUSH_SIZE:
if (isNaN(action.brushSize)) {
log.warn(`Invalid brush size: ${action.brushSize}`);
return state;
}
return {brushSize: Math.max(1, action.brushSize)}; return {brushSize: Math.max(1, action.brushSize)};
default: default:
return state; return state;

View file

@ -1,3 +1,5 @@
import log from '../log/log';
const CHANGE_ERASER_SIZE = 'scratch-paint/tools/CHANGE_ERASER_SIZE'; const CHANGE_ERASER_SIZE = 'scratch-paint/tools/CHANGE_ERASER_SIZE';
const initialState = {brushSize: 20}; const initialState = {brushSize: 20};
@ -5,6 +7,10 @@ const reducer = function (state, action) {
if (typeof state === 'undefined') state = initialState; if (typeof state === 'undefined') state = initialState;
switch (action.type) { switch (action.type) {
case CHANGE_ERASER_SIZE: case CHANGE_ERASER_SIZE:
if (isNaN(action.brushSize)) {
log.warn(`Invalid brush size: ${action.brushSize}`);
return state;
}
return {brushSize: Math.max(1, action.brushSize)}; return {brushSize: Math.max(1, action.brushSize)};
default: default:
return state; return state;

View file

@ -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);
});