mirror of
https://github.com/scratchfoundation/scratch-paint.git
synced 2024-12-22 21:42:30 -05:00
add tests
This commit is contained in:
parent
27f7102b06
commit
975bfdc464
3 changed files with 54 additions and 0 deletions
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
42
test/unit/blob-tool-reducer.test.js
Normal file
42
test/unit/blob-tool-reducer.test.js
Normal 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);
|
||||||
|
});
|
Loading…
Reference in a new issue