diff --git a/src/actions/action-creators.test.js b/src/actions/action-creators.test.js index 1725629..4ca1a55 100644 --- a/src/actions/action-creators.test.js +++ b/src/actions/action-creators.test.js @@ -4,6 +4,8 @@ import thunkMiddleware from 'redux-thunk'; import configureMockStore from 'redux-mock-store'; import panes from '../constants/pane-types'; import { initializeCalculator } from '../lib/calculator'; +import * as calcHelpers from '../lib/calc-helpers'; +import * as inputHelpers from '../lib/input-helpers'; const middlewares = [thunkMiddleware]; const mockStore = configureMockStore(middlewares); @@ -41,21 +43,6 @@ const initialState = { } }; -const desmosMock = { - GraphingCalculator: jest.fn(() => { - return { - asyncScreenshot: (opts, cb) => cb(''), - getExpressions: () => [ - { id: 1, type: 'expression', latex: 'x = 3' }, - { id: 2, latex: '' } - ], - setExpression: () => null - }; - }) -}; - -const calcContainerMock = { current: undefined }; - // Tests describe('Action creators', () => { @@ -72,6 +59,18 @@ describe('Action creators', () => { expect(actions.addFrame(imageData)).toEqual(expected); }); + it('return an action to add frame from storage', () => { + const imageData = 'URI'; + const expected = { + type: types.ADD_FRAME, + payload: { + id: 0, + imageData + } + }; + expect(actions.addSavedFrame(imageData, 0)).toEqual(expected); + }); + it('return an action to update GIF creation progress', () => { const progress = 0.5; const expected = { @@ -81,6 +80,45 @@ describe('Action creators', () => { expect(actions.updateGIFProgress(progress)).toEqual(expected); }); + it('return an action to update GIF file name', () => { + const gifFileName = 'name'; + const expected = { + type: types.UPDATE_GIF_FILENAME, + payload: { gifFileName } + }; + expect(actions.updateGIFFileName('name')).toEqual(expected); + }); + + it('return an action to update GIF text', () => { + const text = 'text'; + const expected = { + type: types.UPDATE_TEXT, + payload: { text } + }; + expect(actions.updateText('text')).toEqual(expected); + }); + + it('return an action to update GIF text color', () => { + const fontColor = '#FFFFFF'; + const expected = { + type: types.UPDATE_TEXT_COLOR, + payload: { fontColor } + }; + expect(actions.updateTextColor('#FFFFFF')).toEqual(expected); + }); + + it('return an action to update GIF text position', () => { + const textOpts = { + textAlign: 'left', + textBaseline: 'top' + }; + const expected = { + type: types.UPDATE_TEXT_POSITION, + payload: { textAlign: 'left', textBaseline: 'top' } + }; + expect(actions.updateTextPosition(textOpts)).toEqual(expected); + }); + it('return an action to add processed GIF data', () => { const imageData = 'URI'; const expected = { @@ -90,6 +128,14 @@ describe('Action creators', () => { expect(actions.addGIF(imageData)).toEqual(expected); }); + it('return an action to undo a captured burst', () => { + const expected = { + type: types.UNDO_BURST, + payload: { frames: { 0: 'zero' }, frameIDs: [0] } + }; + expect(actions.undoBurst({ 0: 'zero' }, [0])).toEqual(expected); + }); + it('return an action to toggle the visible pane', () => { const pane = panes.PREVIEW; const expected = { @@ -121,12 +167,8 @@ describe('Action creators', () => { top: 20, bottom: -20 }; - const strategy = 'stretch'; let key; - let expected; - let setting, val; - for (key in imageSettings) { expect(actions.updateSetting(key, imageSettings[key])).toEqual({ type: types.UPDATE_IMAGE_SETTING, @@ -179,7 +221,6 @@ describe('Action creators', () => { describe('async action creators', () => { let store; - beforeEach(() => { store = mockStore(initialState); }); @@ -196,12 +237,6 @@ describe('Action creators', () => { }); it('return an action to request a frame from the calculator', () => { - const expectedActions = [ - { - type: types.ADD_FRAME, - payload: { id: 2, imageData: expect.any(String) } - } - ]; const opts = { height: 300, targetPixelRatio: 1, @@ -214,7 +249,13 @@ describe('Action creators', () => { top: 10 } }; - initializeCalculator(desmosMock, calcContainerMock); + const expectedActions = [ + { + type: types.ADD_FRAME, + payload: { id: 2, imageData: expect.any(String) } + } + ]; + initializeCalculator(global.Desmos, { current: undefined }); return store.dispatch(actions.requestFrame(opts)).then(() => { expect(store.getActions()).toEqual(expectedActions); }); @@ -236,7 +277,7 @@ describe('Action creators', () => { bottom: -10, top: 10 }; - initializeCalculator(desmosMock, calcContainerMock); + initializeCalculator(global.Desmos, { current: undefined }); return store.dispatch(actions.requestBurst(opts)).then(() => { // slide with min/max of -3/3 should dispatch addFrame 7 times expect(store.getActions().length).toEqual(7); @@ -252,11 +293,7 @@ describe('Action creators', () => { expect(store.getActions()).toEqual(expectedActions); }); - it('return action to generate a GIF via frames in state', () => { - const expectedActions = [ - { type: types.UPDATE_GIF_PROGRESS, payload: { progress: 100 } }, - { type: types.ADD_GIF, payload: { imageData: expect.any(String) } } - ]; + it('return actions to generate a GIF via frames in state', () => { const opts = { gifHeight: 300, gifWidth: 300, @@ -274,8 +311,40 @@ describe('Action creators', () => { // download mock const download = () => null; + const expectedActions = [ + { type: types.UPDATE_GIF_PROGRESS, payload: { progress: 100 } }, + { type: types.ADD_GIF, payload: { imageData: expect.any(String) } } + ]; store.dispatch(actions.generateGIF([], opts, gifshot, download)); expect(store.getActions()).toEqual(expectedActions); }); + + it('return actions to load saved frames from local storage', () => { + calcHelpers.loadSavedGraph = jest.fn(() => ({ + frames: { 0: 'zero' }, + frameIDs: [0] + })); + const expectedActions = [ + { type: types.RESET }, + { type: types.ADD_FRAME, payload: { id: 0, imageData: 'zero' } } + ]; + store.dispatch(actions.loadFramesFromLocal('date')); + expect(store.getActions()).toEqual(expectedActions); + }); + + it('return an action to flash error on bad name input when saving graph', () => { + jest.useFakeTimers(); + inputHelpers.getSaveGraphErrors = jest.fn(() => ({ name: 'name' })); + const expectedActions = [ + { + type: types.SET_ERROR, + payload: { message: 'Invalid name input for saving graph: name' } + }, + { type: types.CLEAR_ERROR } + ]; + store.dispatch(actions.saveGraph()); + jest.runAllTimers(); + expect(store.getActions()).toEqual(expectedActions); + }); }); }); diff --git a/src/actions/index.js b/src/actions/index.js index 4c87290..f2c7c90 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -78,6 +78,13 @@ export const updateGIFProgress = progress => ({ payload: { progress } }); +export const updateGIFFileName = name => { + return { + type: types.UPDATE_GIF_FILENAME, + payload: { gifFileName: name } + }; +}; + export const updateText = text => ({ type: types.UPDATE_TEXT, payload: { text } @@ -88,16 +95,8 @@ export const updateTextColor = fontColor => ({ payload: { fontColor } }); -export const updateGIFFileName = name => { - return { - type: types.UPDATE_GIF_FILENAME, - payload: { gifFileName: name } - }; -}; - export const updateTextPosition = textOpts => { let { textAlign, textBaseline } = textOpts; - return { type: types.UPDATE_TEXT_POSITION, payload: { textAlign, textBaseline } diff --git a/src/setupFiles.js b/src/setupFiles.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/setupTests.js b/src/setupTests.js index 1816586..cfae3e4 100644 --- a/src/setupTests.js +++ b/src/setupTests.js @@ -10,7 +10,16 @@ global.console = { }; global.Desmos = { - GraphingCalculator: jest.fn() + GraphingCalculator: jest.fn(() => { + return { + asyncScreenshot: (opts, cb) => cb(''), + getExpressions: () => [ + { id: 1, type: 'expression', latex: 'x = 3' }, + { id: 2, latex: '' } + ], + setExpression: () => null + }; + }) }; global.renderWithRedux = (