Skip to content

Commit

Permalink
update action creators tests to account for added features
Browse files Browse the repository at this point in the history
  • Loading branch information
colebillys19 committed Jun 27, 2019
1 parent f2b9924 commit b9bdd31
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 42 deletions.
135 changes: 102 additions & 33 deletions src/actions/action-creators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -179,7 +221,6 @@ describe('Action creators', () => {

describe('async action creators', () => {
let store;

beforeEach(() => {
store = mockStore(initialState);
});
Expand All @@ -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,
Expand All @@ -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);
});
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
});
});
});
15 changes: 7 additions & 8 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down
Empty file removed src/setupFiles.js
Empty file.
11 changes: 10 additions & 1 deletion src/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit b9bdd31

Please sign in to comment.