diff --git a/apps/src/gamelab/AnimationPicker/animationPickerModule.js b/apps/src/gamelab/AnimationPicker/animationPickerModule.js index 4d9c00c5825e8..ca46f38e53a41 100644 --- a/apps/src/gamelab/AnimationPicker/animationPickerModule.js +++ b/apps/src/gamelab/AnimationPicker/animationPickerModule.js @@ -107,30 +107,33 @@ export function beginUpload(filename) { */ export function handleUploadComplete(result) { return function (dispatch, getState) { - const { goal, uploadFilename } = getState().animationPicker; const key = result.filename.replace(/\.png$/i, ''); const sourceUrl = animationsApi.basePath(key + '.png'); - - loadImageMetadata(sourceUrl, metadata => { - const animation = _.assign({}, metadata, { - name: uploadFilename, - sourceUrl: sourceUrl, - size: result.size, - version: result.versionId - }); - - if (goal === Goal.NEW_ANIMATION) { - dispatch(addAnimation(key, animation)); - } else if (goal === Goal.NEW_FRAME) { - dispatch(appendCustomFrames(animation)); - } - dispatch(hide()); - }, () => { + const onImageMetadataLoaded = buildOnImageMetadataLoaded(key, result, dispatch, getState); + loadImageMetadata(sourceUrl, onImageMetadataLoaded, () => { dispatch(handleUploadError(gamelabMsg.animationPicker_failedToParseImage())); }); }; } +export function buildOnImageMetadataLoaded(key, result, dispatch, getState) { + return (metadata) => { + const { goal, uploadFilename } = getState().animationPicker; + const animation = _.assign({}, metadata, { + name: uploadFilename, + sourceUrl: null, + size: result.size, + version: result.versionId + }); + if (goal === Goal.NEW_ANIMATION) { + dispatch(addAnimation(key, animation)); + } else if (goal === Goal.NEW_FRAME) { + dispatch(appendCustomFrames(animation)); + } + dispatch(hide()); + }; +} + /** * Asynchronously loads an image file as an Image, then derives appropriate * animation metadata from that Image and returns the metadata to a callback. diff --git a/apps/test/unit/gamelab/AnimationPicker/animationPickerModuleTest.js b/apps/test/unit/gamelab/AnimationPicker/animationPickerModuleTest.js index 8a91ac29ce98b..5896c0da61dc2 100644 --- a/apps/test/unit/gamelab/AnimationPicker/animationPickerModuleTest.js +++ b/apps/test/unit/gamelab/AnimationPicker/animationPickerModuleTest.js @@ -1,4 +1,8 @@ +import sinon from 'sinon'; import reducer, * as animationPickerModule from '@cdo/apps/gamelab/AnimationPicker/animationPickerModule'; +import listReducer from '@cdo/apps/gamelab/animationListModule'; +import {combineReducers} from 'redux'; +import {createStore} from '../../../util/redux'; import {expect} from '../../../util/configuredChai'; var Goal = animationPickerModule.Goal; @@ -102,5 +106,46 @@ describe('animationPickerModule', function () { expect(newState.uploadError).to.equal(status); }); }); + + describe('action: handleUploadComplete', function () { + + it('sets sourceUrl to null', function () { + const fakeDispatch = sinon.spy(); + const newState = {animationPicker: { uploadFilename: "filename.jpg", goal: Goal.NEW_ANIMATION }}; + var store = createStore(combineReducers({animationList: listReducer, animationPicker: reducer}), newState); + + var onMetadataLoaded = animationPickerModule.buildOnImageMetadataLoaded( + "filename.jpg", + { + filename: "filename.jpg", + result: 0, + versionId: "string" + }, + fakeDispatch, + store.getState + ); + onMetadataLoaded({}); + expect(fakeDispatch).to.have.been.calledTwice; + + const addAnimation = fakeDispatch.firstCall.args[0]; + expect(addAnimation).to.be.a('function'); + expect(fakeDispatch.secondCall.args[0]).to.deep.equal(animationPickerModule.hide()); + fakeDispatch.reset(); + + addAnimation(fakeDispatch, store.getState); + expect(fakeDispatch.firstCall.args[0]).to.deep.equal({ + type: 'AnimationList/ADD_ANIMATION', + key: 'filename.jpg', + props: + { + name: 'filename.jpg', + sourceUrl: null, + size: undefined, + version: 'string', + looping: true + } + }); + }); + }); }); });