Add format reducer

This commit is contained in:
DD 2018-04-05 18:06:37 -04:00
parent e6b151c41f
commit 2b8c291765
4 changed files with 66 additions and 0 deletions

8
src/lib/format.js Normal file
View file

@ -0,0 +1,8 @@
import keyMirror from 'keymirror';
const Formats = keyMirror({
BITMAP: null,
VECTOR: null
});
export default Formats;

32
src/reducers/format.js Normal file
View file

@ -0,0 +1,32 @@
import Formats from '../lib/format';
import log from '../log/log';
const CHANGE_FORMAT = 'scratch-paint/formats/CHANGE_FORMAT';
const initialState = Formats.VECTOR;
const reducer = function (state, action) {
if (typeof state === 'undefined') state = initialState;
switch (action.type) {
case CHANGE_FORMAT:
if (action.format in Formats) {
return action.format;
}
log.warn(`Format does not exist: ${action.format}`);
/* falls through */
default:
return state;
}
};
// Action creators ==================================
const changeFormat = function (format) {
return {
type: CHANGE_FORMAT,
format: format
};
};
export {
reducer as default,
changeFormat
};

View file

@ -4,6 +4,7 @@ import brushModeReducer from './brush-mode';
import eraserModeReducer from './eraser-mode';
import colorReducer from './color';
import clipboardReducer from './clipboard';
import formatReducer from './format';
import hoverReducer from './hover';
import modalsReducer from './modals';
import selectedItemReducer from './selected-items';
@ -17,6 +18,7 @@ export default combineReducers({
color: colorReducer,
clipboard: clipboardReducer,
eraserMode: eraserModeReducer,
format: formatReducer,
hoveredItemId: hoverReducer,
modals: modalsReducer,
selectedItems: selectedItemReducer,

View file

@ -0,0 +1,24 @@
/* eslint-env jest */
import Formats from '../../src/lib/format';
import reducer from '../../src/reducers/format';
import {changeFormat} from '../../src/reducers/format';
test('initialState', () => {
let defaultState;
expect(reducer(defaultState /* state */, {type: 'anything'} /* action */) in Formats).toBeTruthy();
});
test('changeFormat', () => {
let defaultState;
expect(reducer(defaultState /* state */, changeFormat(Formats.BITMAP) /* action */)).toBe(Formats.BITMAP);
expect(reducer(Formats.BITMAP /* state */, changeFormat(Formats.BITMAP) /* action */))
.toBe(Formats.BITMAP);
expect(reducer(Formats.BITMAP /* state */, changeFormat(Formats.VECTOR) /* action */))
.toBe(Formats.VECTOR);
});
test('invalidChangeMode', () => {
expect(reducer(Formats.BITMAP /* state */, changeFormat('non-existant mode') /* action */))
.toBe(Formats.BITMAP);
expect(reducer(Formats.BITMAP /* state */, changeFormat() /* action */)).toBe(Formats.BITMAP);
});