Skip to content

Commit 9d185bd

Browse files
authored
Merge pull request scratchfoundation#4753 from paulkaplan/project-save-trigger
Allow GUI consumer to receive a function for triggering saves.
2 parents 7c26245 + a0745e4 commit 9d185bd

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/lib/project-saver-hoc.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ const ProjectSaverHOC = function (WrappedComponent) {
5959
// but then it'd be hard to turn this listening off in our tests
6060
window.onbeforeunload = e => this.leavePageConfirm(e);
6161
}
62+
63+
// Allow the GUI consumer to pass in a function to receive a trigger
64+
// for triggering thumbnail or whole project saves.
65+
// These functions are called with null on unmount to prevent stale references.
6266
this.props.onSetProjectThumbnailer(this.getProjectThumbnail);
67+
this.props.onSetProjectSaver(this.tryToAutoSave);
6368
}
6469
componentDidUpdate (prevProps) {
6570
if (!this.props.isAnyCreatingNewState && prevProps.isAnyCreatingNewState) {
@@ -116,6 +121,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
116121
// window.onbeforeunload = undefined; // eslint-disable-line no-undefined
117122
// Remove project thumbnailer function since the components are unmounting
118123
this.props.onSetProjectThumbnailer(null);
124+
this.props.onSetProjectSaver(null);
119125
}
120126
leavePageConfirm (e) {
121127
if (this.props.projectChanged) {
@@ -310,6 +316,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
310316
onRemixing,
311317
onSetProjectUnchanged,
312318
onSetProjectThumbnailer,
319+
onSetProjectSaver,
313320
onShowAlert,
314321
onShowCopySuccessAlert,
315322
onShowRemixSuccessAlert,
@@ -376,6 +383,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
376383
autoSaveIntervalSecs: 120,
377384
onRemixing: () => {},
378385
onSetProjectThumbnailer: () => {},
386+
onSetProjectSaver: () => {},
379387
onUpdateProjectData: saveProjectToServer
380388
};
381389
const mapStateToProps = (state, ownProps) => {

test/unit/util/project-saver-hoc.test.jsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,27 @@ describe('projectSaverHOC', () => {
465465
expect(setThumb).toHaveBeenCalledTimes(2);
466466
expect(setThumb.mock.calls[1][0]).toBe(null);
467467
});
468+
469+
test('uses onSetProjectSaver on mount/unmount', () => {
470+
const Component = () => <div />;
471+
const WrappedComponent = projectSaverHOC(Component);
472+
const setSaver = jest.fn();
473+
const mounted = mount(
474+
<WrappedComponent
475+
store={store}
476+
vm={vm}
477+
onSetProjectSaver={setSaver}
478+
/>
479+
);
480+
// Set project saver should be called on mount
481+
expect(setSaver).toHaveBeenCalledTimes(1);
482+
483+
// And it should not pass that function on to wrapped element
484+
expect(mounted.find(Component).props().onSetProjectSaver).toBeUndefined();
485+
486+
// Unmounting should call it again with null
487+
mounted.unmount();
488+
expect(setSaver).toHaveBeenCalledTimes(2);
489+
expect(setSaver.mock.calls[1][0]).toBe(null);
490+
});
468491
});

0 commit comments

Comments
 (0)