make modes independent of tool. Fix tests

This commit is contained in:
DD Liu 2017-08-16 15:34:33 -04:00
parent 087a6264a0
commit 33a01c1396
9 changed files with 50 additions and 48 deletions

View file

@ -41,9 +41,7 @@ class BrushMode extends React.Component {
activateTool () { activateTool () {
// TODO: This is temporary until a component that provides the brush size is hooked up // TODO: This is temporary until a component that provides the brush size is hooked up
this.props.canvas.addEventListener('mousewheel', this.onScroll); this.props.canvas.addEventListener('mousewheel', this.onScroll);
this.blob.activateTool(false /* isEraser */, this.props.brushModeState);
this.tool = new paper.Tool();
this.blob.activateTool(false /* isEraser */, this.tool, this.props.brushModeState);
// TODO Make sure a fill color is set on the brush // TODO Make sure a fill color is set on the brush
// if(!pg.stylebar.getFillColor()) { // if(!pg.stylebar.getFillColor()) {
@ -53,8 +51,6 @@ class BrushMode extends React.Component {
// TODO setup floating tool options panel in the editor // TODO setup floating tool options panel in the editor
// pg.toolOptionPanel.setup(options, components, function() {}); // pg.toolOptionPanel.setup(options, components, function() {});
this.tool.activate();
} }
deactivateTool () { deactivateTool () {
this.props.canvas.removeEventListener('mousewheel', this.onScroll); this.props.canvas.removeEventListener('mousewheel', this.onScroll);

View file

@ -40,9 +40,7 @@ class EraserMode extends React.Component {
activateTool () { activateTool () {
this.props.canvas.addEventListener('mousewheel', this.onScroll); this.props.canvas.addEventListener('mousewheel', this.onScroll);
this.tool = new paper.Tool(); this.blob.activateTool(true /* isEraser */, this.props.eraserModeState);
this.blob.activateTool(true /* isEraser */, this.tool, this.props.eraserModeState);
this.tool.activate();
} }
deactivateTool () { deactivateTool () {
this.props.canvas.removeEventListener('mousewheel', this.onScroll); this.props.canvas.removeEventListener('mousewheel', this.onScroll);

View file

@ -31,8 +31,8 @@ class Blobbiness {
} }
} }
activateTool (isEraser, tool, options) { activateTool (isEraser, options) {
this.tool = tool; this.tool = new paper.Tool();
tool.cursorPreviewLastPoint = new paper.Point(-10000, -10000); tool.cursorPreviewLastPoint = new paper.Point(-10000, -10000);
tool.resizeCursorIfNeeded = function (point) { tool.resizeCursorIfNeeded = function (point) {

View file

@ -77,7 +77,6 @@ const segmentBrushHelper = function (tool) {
// Smooth the path. // Smooth the path.
finalPath.simplify(2); finalPath.simplify(2);
// console.log(finalPath.segments);
return finalPath; return finalPath;
}; };
}; };

View file

@ -18,11 +18,14 @@ const reducer = function (state, action) {
}; };
// Action creators ================================== // Action creators ==================================
reducer.changeBrushSize = function (brushSize) { const changeBrushSize = function (brushSize) {
return { return {
type: CHANGE_ERASER_SIZE, type: CHANGE_ERASER_SIZE,
brushSize: brushSize brushSize: brushSize
}; };
}; };
export default reducer; export {
reducer as default,
changeBrushSize
};

View file

@ -19,11 +19,14 @@ const reducer = function (state, action) {
}; };
// Action creators ================================== // Action creators ==================================
reducer.changeMode = function (mode) { const changeMode = function (mode) {
return { return {
type: CHANGE_MODE, type: CHANGE_MODE,
mode: mode mode: mode
}; };
}; };
export default reducer; export {
reducer as default,
changeMode
};

View file

@ -1,6 +1,8 @@
/* eslint-env jest */ /* eslint-env jest */
import brushReducer from '../../src/reducers/brush-tool'; import brushReducer from '../../src/reducers/brush-mode';
import eraserReducer from '../../src/reducers/eraser-tool'; import {changeBrushSize} from '../../src/reducers/brush-mode';
import eraserReducer from '../../src/reducers/eraser-mode';
import {changeBrushSize as changeEraserSize} from '../../src/reducers/eraser-mode';
test('initialState', () => { test('initialState', () => {
let defaultState; let defaultState;
@ -16,27 +18,27 @@ test('changeBrushSize', () => {
let defaultState; let defaultState;
const newBrushSize = 8078; const newBrushSize = 8078;
expect(brushReducer(defaultState /* state */, brushReducer.changeBrushSize(newBrushSize) /* action */)) expect(brushReducer(defaultState /* state */, changeBrushSize(newBrushSize) /* action */))
.toEqual({brushSize: newBrushSize}); .toEqual({brushSize: newBrushSize});
expect(brushReducer(1 /* state */, brushReducer.changeBrushSize(newBrushSize) /* action */)) expect(brushReducer(1 /* state */, changeBrushSize(newBrushSize) /* action */))
.toEqual({brushSize: newBrushSize}); .toEqual({brushSize: newBrushSize});
expect(eraserReducer(defaultState /* state */, eraserReducer.changeBrushSize(newBrushSize) /* action */)) expect(eraserReducer(defaultState /* state */, changeEraserSize(newBrushSize) /* action */))
.toEqual({brushSize: newBrushSize}); .toEqual({brushSize: newBrushSize});
expect(eraserReducer(1 /* state */, eraserReducer.changeBrushSize(newBrushSize) /* action */)) expect(eraserReducer(1 /* state */, changeEraserSize(newBrushSize) /* action */))
.toEqual({brushSize: newBrushSize}); .toEqual({brushSize: newBrushSize});
}); });
test('invalidChangeBrushSize', () => { test('invalidChangeBrushSize', () => {
const origState = {brushSize: 1}; const origState = {brushSize: 1};
expect(brushReducer(origState /* state */, brushReducer.changeBrushSize('invalid argument') /* action */)) expect(brushReducer(origState /* state */, changeBrushSize('invalid argument') /* action */))
.toBe(origState); .toBe(origState);
expect(brushReducer(origState /* state */, brushReducer.changeBrushSize() /* action */)) expect(brushReducer(origState /* state */, changeBrushSize() /* action */))
.toBe(origState); .toBe(origState);
expect(eraserReducer(origState /* state */, eraserReducer.changeBrushSize('invalid argument') /* action */)) expect(eraserReducer(origState /* state */, changeEraserSize('invalid argument') /* action */))
.toBe(origState); .toBe(origState);
expect(eraserReducer(origState /* state */, eraserReducer.changeBrushSize() /* action */)) expect(eraserReducer(origState /* state */, changeEraserSize() /* action */))
.toBe(origState); .toBe(origState);
}); });

View file

@ -0,0 +1,24 @@
/* eslint-env jest */
import Modes from '../../src/modes/modes';
import reducer from '../../src/reducers/modes';
import {changeMode} from '../../src/reducers/modes';
test('initialState', () => {
let defaultState;
expect(reducer(defaultState /* state */, {type: 'anything'} /* action */) in Modes).toBeTruthy();
});
test('changeMode', () => {
let defaultState;
expect(reducer(defaultState /* state */, changeMode(Modes.ERASER) /* action */)).toBe(Modes.ERASER);
expect(reducer(Modes.ERASER /* state */, changeMode(Modes.ERASER) /* action */))
.toBe(Modes.ERASER);
expect(reducer(Modes.BRUSH /* state */, changeMode(Modes.ERASER) /* action */))
.toBe(Modes.ERASER);
});
test('invalidChangeMode', () => {
expect(reducer(Modes.BRUSH /* state */, changeMode('non-existant mode') /* action */))
.toBe(Modes.BRUSH);
expect(reducer(Modes.BRUSH /* state */, changeMode() /* action */)).toBe(Modes.BRUSH);
});

View file

@ -1,23 +0,0 @@
/* eslint-env jest */
import ToolTypes from '../../src/tools/tool-types';
import reducer from '../../src/reducers/tools';
test('initialState', () => {
let defaultState;
expect(reducer(defaultState /* state */, {type: 'anything'} /* action */) in ToolTypes).toBeTruthy();
});
test('changeTool', () => {
let defaultState;
expect(reducer(defaultState /* state */, reducer.changeTool(ToolTypes.ERASER) /* action */)).toBe(ToolTypes.ERASER);
expect(reducer(ToolTypes.ERASER /* state */, reducer.changeTool(ToolTypes.ERASER) /* action */))
.toBe(ToolTypes.ERASER);
expect(reducer(ToolTypes.BRUSH /* state */, reducer.changeTool(ToolTypes.ERASER) /* action */))
.toBe(ToolTypes.ERASER);
});
test('invalidChangeTool', () => {
expect(reducer(ToolTypes.BRUSH /* state */, reducer.changeTool('non-existant tool') /* action */))
.toBe(ToolTypes.BRUSH);
expect(reducer(ToolTypes.BRUSH /* state */, reducer.changeTool() /* action */)).toBe(ToolTypes.BRUSH);
});