Merge pull request #4753 from paulkaplan/project-save-trigger

Allow GUI consumer to receive a function for triggering saves.
This commit is contained in:
Paul Kaplan authored and GitHub committed 2019-04-23 08:32:36 -04:00
commit 9d185bd6c8
2 files changed
+31

No files matched your search

+8
View File
@@ -59,7 +59,12 @@ const ProjectSaverHOC = function (WrappedComponent) {
// but then it'd be hard to turn this listening off in our tests
window.onbeforeunload = e => this.leavePageConfirm(e);
}
// Allow the GUI consumer to pass in a function to receive a trigger
// for triggering thumbnail or whole project saves.
// These functions are called with null on unmount to prevent stale references.
this.props.onSetProjectThumbnailer(this.getProjectThumbnail);
this.props.onSetProjectSaver(this.tryToAutoSave);
}
componentDidUpdate (prevProps) {
if (!this.props.isAnyCreatingNewState && prevProps.isAnyCreatingNewState) {
@@ -116,6 +121,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
// window.onbeforeunload = undefined; // eslint-disable-line no-undefined
// Remove project thumbnailer function since the components are unmounting
this.props.onSetProjectThumbnailer(null);
this.props.onSetProjectSaver(null);
}
leavePageConfirm (e) {
if (this.props.projectChanged) {
@@ -310,6 +316,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
onRemixing,
onSetProjectUnchanged,
onSetProjectThumbnailer,
onSetProjectSaver,
onShowAlert,
onShowCopySuccessAlert,
onShowRemixSuccessAlert,
@@ -376,6 +383,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
autoSaveIntervalSecs: 120,
onRemixing: () => {},
onSetProjectThumbnailer: () => {},
onSetProjectSaver: () => {},
onUpdateProjectData: saveProjectToServer
};
const mapStateToProps = (state, ownProps) => {
+23
View File
@@ -465,4 +465,27 @@ describe('projectSaverHOC', () => {
expect(setThumb).toHaveBeenCalledTimes(2);
expect(setThumb.mock.calls[1][0]).toBe(null);
});
test('uses onSetProjectSaver on mount/unmount', () => {
const Component = () => <div />;
const WrappedComponent = projectSaverHOC(Component);
const setSaver = jest.fn();
const mounted = mount(
<WrappedComponent
store={store}
vm={vm}
onSetProjectSaver={setSaver}
/>
);
// Set project saver should be called on mount
expect(setSaver).toHaveBeenCalledTimes(1);
// And it should not pass that function on to wrapped element
expect(mounted.find(Component).props().onSetProjectSaver).toBeUndefined();
// Unmounting should call it again with null
mounted.unmount();
expect(setSaver).toHaveBeenCalledTimes(2);
expect(setSaver.mock.calls[1][0]).toBe(null);
});
});