-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add check to see if assets pre-exist when uploading
- Loading branch information
Showing
14 changed files
with
411 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/components/AssetsUploadConfirm/AssetsUploadConfirm.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import React from 'react'; | ||
import { Button, Modal } from '@edx/paragon'; | ||
|
||
import AssetsUploadConfirm from './index'; | ||
import { mountWithIntl } from '../../utils/i18n/enzymeHelper'; | ||
import mockQuerySelector from '../../utils/mockQuerySelector'; | ||
|
||
const defaultProps = { | ||
files: [], | ||
uploadAssets: () => { }, | ||
clearPreUploadProps: () => {}, | ||
courseDetails: {}, | ||
preUploadError: '', | ||
}; | ||
|
||
const modalIsClosed = (wrapper) => { | ||
expect(wrapper.state('modalOpen')).toEqual(false); | ||
expect(wrapper.find(Modal).prop('open')).toEqual(false); | ||
expect(wrapper.find(AssetsUploadConfirm).prop('preUploadError')).toEqual(''); | ||
}; | ||
|
||
const modalIsOpen = (wrapper) => { | ||
expect(wrapper.state('modalOpen')).toEqual(true); | ||
expect(wrapper.find(Modal).prop('open')).toEqual(true); | ||
expect(wrapper.find(AssetsUploadConfirm).prop('preUploadError')).toBeTruthy(); | ||
}; | ||
|
||
const errorMessageHasCorrectFiles = (wrapper, files) => { | ||
const modal = wrapper.find(AssetsUploadConfirm); | ||
const modalMessage = modal.prop('preUploadError'); | ||
|
||
files.forEach((file) => { | ||
expect(modalMessage).toContain(file); | ||
}); | ||
}; | ||
|
||
let wrapper; | ||
|
||
describe('AssetsUploadConfirm', () => { | ||
beforeEach(() => { | ||
mockQuerySelector.init(); | ||
}); | ||
afterEach(() => { | ||
mockQuerySelector.reset(); | ||
}); | ||
|
||
describe('renders', () => { | ||
beforeEach(() => { | ||
wrapper = mountWithIntl( | ||
<AssetsUploadConfirm | ||
{...defaultProps} | ||
/>, | ||
); | ||
// modal = wrapper.find('[role="dialog"]'); | ||
}); | ||
|
||
it('closed by default', () => { | ||
modalIsClosed(wrapper); | ||
}); | ||
|
||
it('open if there is an error message', () => { | ||
wrapper.setProps({ | ||
preUploadError: 'The following files already exist: asset.jpg', | ||
}); | ||
|
||
modalIsOpen(wrapper); | ||
errorMessageHasCorrectFiles(wrapper, ['asset.jpg']); | ||
}); | ||
}); | ||
describe('behaves', () => { | ||
it('Overwrite calls uploadAssets', () => { | ||
const mockUploadAssets = jest.fn(); | ||
const files = ['file1', 'file2']; | ||
const courseDetails = { | ||
id: 'course-v1:edX+DemoX+Demo_Course', | ||
}; | ||
wrapper.setProps({ | ||
files, | ||
courseDetails, | ||
uploadAssets: mockUploadAssets, | ||
}); | ||
|
||
wrapper.find(Button).filterWhere(button => button.text() === 'Overwrite').simulate('click'); | ||
expect(mockUploadAssets).toBeCalledWith(files, courseDetails); | ||
}); | ||
|
||
it('clicking cancel button closes the status alert', () => { | ||
wrapper.setProps({ | ||
preUploadError: 'The following files already exist: asset.jpg', | ||
clearPreUploadProps: () => { | ||
wrapper.setProps({ | ||
...defaultProps, | ||
}); | ||
}, | ||
}); | ||
|
||
const modal = wrapper.find(Modal); | ||
const cancelModalButton = modal.find('button').filterWhere(button => button.text() === 'Cancel'); | ||
cancelModalButton.simulate('click'); | ||
modalIsClosed(wrapper); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { connect } from 'react-redux'; | ||
|
||
import { uploadAssets, clearPreUploadProps } from '../../data/actions/assets'; | ||
import AssetsUploadConfirm from '.'; | ||
|
||
const mapStateToProps = state => ({ | ||
files: state.metadata.files, | ||
preUploadError: state.metadata.preUploadError, | ||
courseDetails: state.studioDetails.course, | ||
}); | ||
|
||
const mapDispatchToProps = dispatch => ({ | ||
uploadAssets: (assets, courseDetails) => dispatch(uploadAssets(assets, courseDetails)), | ||
clearPreUploadProps: () => dispatch(clearPreUploadProps()), | ||
}); | ||
|
||
const WrappedAssetsUploadConfirm = connect( | ||
mapStateToProps, | ||
mapDispatchToProps, | ||
)(AssetsUploadConfirm); | ||
|
||
export default WrappedAssetsUploadConfirm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { defineMessages } from 'react-intl'; | ||
|
||
const messages = defineMessages({ | ||
assetsUploadConfirmMessage: { | ||
id: 'assetsUploadConfirmMessage', | ||
defaultMessage: '0 files uploaded. {preUploadError}. Continuing to upload will overwrite them. If this is not what you want, retry uploading the files with different names.', | ||
description: 'The message displayed in the confirmation box shown when uploading files with pre-existing names', | ||
}, | ||
assetsUploadConfirmOverwrite: { | ||
id: 'assetsUploadConfirmOverwrite', | ||
defaultMessage: 'Overwrite', | ||
description: 'The message displayed in the button to confirm overwriting the files', | ||
}, | ||
assetsUploadConfirmCancel: { | ||
id: 'assetsUploadConfirmCancel', | ||
defaultMessage: 'Cancel', | ||
description: 'The message displayed in the button to confirm cancelling the upload', | ||
}, | ||
}); | ||
|
||
export default messages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Button, Modal, Variant } from '@edx/paragon'; | ||
|
||
import WrappedMessage from '../../utils/i18n/formattedMessageWrapper'; | ||
import messages from './displayMessages'; | ||
|
||
const defaultState = { | ||
modalOpen: false, | ||
}; | ||
const modalWrapperID = 'modalWrapper'; | ||
|
||
export default class AssetsUploadConfirm extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = defaultState; | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
const { preUploadError } = nextProps; | ||
this.updateAlertOpenState(preUploadError); | ||
} | ||
|
||
updateAlertOpenState = (preUploadError) => { | ||
this.setState({ | ||
modalOpen: !!preUploadError, | ||
}); | ||
}; | ||
|
||
uploadFiles = () => { | ||
this.props.uploadAssets(this.props.files, this.props.courseDetails); | ||
}; | ||
|
||
onClose = () => { | ||
this.setState(defaultState); | ||
this.props.clearPreUploadProps(); | ||
}; | ||
|
||
render() { | ||
const { uploadFiles } = this; | ||
const { modalOpen } = this.state; | ||
const { preUploadError } = this.props; | ||
const content = ( | ||
<WrappedMessage | ||
message={messages.assetsUploadConfirmMessage} | ||
values={{ preUploadError }} | ||
/> | ||
); | ||
const closeText = ( | ||
<WrappedMessage message={messages.assetsUploadConfirmCancel} /> | ||
); | ||
const button = ( | ||
<Button | ||
buttonType="primary" | ||
label={<WrappedMessage message={messages.assetsUploadConfirmOverwrite} />} | ||
onClick={uploadFiles} | ||
/> | ||
); | ||
|
||
return ( | ||
<div id={modalWrapperID}> | ||
<Modal | ||
title="Overwrite Files" | ||
open={modalOpen} | ||
body={content} | ||
buttons={[button]} | ||
onClose={this.onClose} | ||
closeText={closeText} | ||
variant={{ status: Variant.status.WARNING }} | ||
parentSelector={`#${modalWrapperID}`} | ||
/> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
AssetsUploadConfirm.propTypes = { | ||
files: PropTypes.arrayOf(PropTypes.string), | ||
uploadAssets: PropTypes.func.isRequired, | ||
clearPreUploadProps: PropTypes.func.isRequired, | ||
courseDetails: PropTypes.shape({ | ||
lang: PropTypes.string, | ||
url_name: PropTypes.string, | ||
name: PropTypes.string, | ||
display_course_number: PropTypes.string, | ||
num: PropTypes.string, | ||
org: PropTypes.string, | ||
id: PropTypes.string, | ||
revision: PropTypes.string, | ||
}).isRequired, | ||
preUploadError: PropTypes.string, | ||
}; | ||
|
||
AssetsUploadConfirm.defaultProps = { | ||
files: [], | ||
preUploadError: '', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.