diff --git a/src/components/menu-bar/menu-bar.jsx b/src/components/menu-bar/menu-bar.jsx index 5cf6cb6bbfa062753d4e449ebf2f54505e3f5438..825f6096f9f16c357076201fde8c9b537ecb61f8 100644 --- a/src/components/menu-bar/menu-bar.jsx +++ b/src/components/menu-bar/menu-bar.jsx @@ -157,10 +157,18 @@ class MenuBar extends React.Component { ]); } handleClickNew () { - // if canSave===true and canCreateNew===true, it's safe to replace current project, - // since we will auto-save first. Else, confirm first. - const readyToReplaceProject = (this.props.canSave && this.props.canCreateNew) || - confirm(this.props.intl.formatMessage(messages.confirmNav)); // eslint-disable-line no-alert + let readyToReplaceProject = true; + // if the project is dirty, and user owns the project, we will autosave. + // but if they are not logged in and can't save, user should consider + // downloading or logging in first. + // Note that if user is logged in and editing someone else's project, + // they'll lose their work. + if (this.props.projectChanged && !this.props.canCreateNew) { + readyToReplaceProject = confirm( // eslint-disable-line no-alert + this.props.intl.formatMessage(messages.confirmNav) + ); + } + this.props.onRequestCloseFile(); if (readyToReplaceProject) { this.props.onClickNew(this.props.canSave && this.props.canCreateNew); } @@ -734,6 +742,7 @@ MenuBar.propTypes = { onShare: PropTypes.func, onToggleLoginOpen: PropTypes.func, onUpdateProjectTitle: PropTypes.func, + projectChanged: PropTypes.bool, projectTitle: PropTypes.string, renderLogin: PropTypes.func, sessionExists: PropTypes.bool, @@ -757,6 +766,7 @@ const mapStateToProps = state => { isShowingProject: getIsShowingProject(loadingState), languageMenuOpen: languageMenuOpen(state), loginMenuOpen: loginMenuOpen(state), + projectChanged: state.scratchGui.projectChanged, projectTitle: state.scratchGui.projectTitle, sessionExists: state.session && typeof state.session.session !== 'undefined', username: user ? user.username : null diff --git a/test/integration/project-loading.test.js b/test/integration/project-loading.test.js index a3b4923343439b1c4a469008e5db7997b9f5c660..115c29576c14d227ab9125e755809b91256082cc 100644 --- a/test/integration/project-loading.test.js +++ b/test/integration/project-loading.test.js @@ -115,9 +115,9 @@ describe('Loading scratch gui', () => { test('Creating new project resets active tab to Code tab', async () => { await loadUri(uri); await clickText('View 2.0 Project'); - const el = await findByXpath("//input[@placeholder='scratch.mit.edu/projects/123456789']"); + const inputElement = await findByXpath("//input[@placeholder='scratch.mit.edu/projects/123456789']"); const projectId = '96708228'; - await el.sendKeys(`scratch.mit.edu/projects/${projectId}`); + await inputElement.sendKeys(`scratch.mit.edu/projects/${projectId}`); await clickXpath('//button[@title="View Project"]'); await new Promise(resolve => setTimeout(resolve, 2000)); await findByXpath('//*[span[text()="Costumes"]]'); @@ -127,6 +127,41 @@ describe('Loading scratch gui', () => { 'contains(@class, "menu-bar_hoverable")][span[text()="File"]]' ); await clickXpath('//li[span[text()="New"]]'); + await findByXpath('//*[div[@class="scratchCategoryMenu"]]'); + await clickText('Operators', scope.blocksTab); + }); + + test('Not logged in->made no changes to project->create new project should not show alert', async () => { + await loadUri(uri); + await clickText('View 2.0 Project'); + const inputElement = await findByXpath("//input[@placeholder='scratch.mit.edu/projects/123456789']"); + const projectId = '96708228'; + await inputElement.sendKeys(`scratch.mit.edu/projects/${projectId}`); + await clickXpath('//button[@title="View Project"]'); + await new Promise(resolve => setTimeout(resolve, 2000)); + await clickXpath( + '//div[contains(@class, "menu-bar_menu-bar-item") and ' + + 'contains(@class, "menu-bar_hoverable")][span[text()="File"]]' + ); + await clickXpath('//li[span[text()="New"]]'); + await findByXpath('//*[div[@class="scratchCategoryMenu"]]'); + await clickText('Operators', scope.blocksTab); + }); + + test('Not logged in->made a change to project->create new project should show alert', async () => { + await loadUri(uri); + await clickText('View 2.0 Project'); + const inputElement = await findByXpath("//input[@placeholder='scratch.mit.edu/projects/123456789']"); + const projectId = '96708228'; + await inputElement.sendKeys(`scratch.mit.edu/projects/${projectId}`); + await clickXpath('//button[@title="View Project"]'); + await new Promise(resolve => setTimeout(resolve, 2000)); + await clickText('move'); + await clickXpath( + '//div[contains(@class, "menu-bar_menu-bar-item") and ' + + 'contains(@class, "menu-bar_hoverable")][span[text()="File"]]' + ); + await clickXpath('//li[span[text()="New"]]'); driver.switchTo() .alert() .accept();