diff --git a/src/lib/format.js b/src/lib/format.js new file mode 100644 index 00000000..6748d0da --- /dev/null +++ b/src/lib/format.js @@ -0,0 +1,8 @@ +import keyMirror from 'keymirror'; + +const Formats = keyMirror({ + BITMAP: null, + VECTOR: null +}); + +export default Formats; diff --git a/src/reducers/format.js b/src/reducers/format.js new file mode 100644 index 00000000..a8a2f7c4 --- /dev/null +++ b/src/reducers/format.js @@ -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 +}; diff --git a/src/reducers/scratch-paint-reducer.js b/src/reducers/scratch-paint-reducer.js index 258dea9e..224dcad7 100644 --- a/src/reducers/scratch-paint-reducer.js +++ b/src/reducers/scratch-paint-reducer.js @@ -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, diff --git a/test/unit/format-reducer.test.js b/test/unit/format-reducer.test.js new file mode 100644 index 00000000..5b12ca43 --- /dev/null +++ b/test/unit/format-reducer.test.js @@ -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); +});