-
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
419 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
100 changes: 100 additions & 0 deletions
100
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,100 @@ | ||
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.prop('preUploadError')).toEqual([]); | ||
expect(wrapper.state('modalOpen')).toEqual(false); | ||
expect(wrapper.find(Modal).prop('open')).toEqual(false); | ||
}; | ||
|
||
const modalIsOpen = (wrapper) => { | ||
expect(wrapper.prop('preUploadError')).toBeTruthy(); | ||
expect(wrapper.state('modalOpen')).toEqual(true); | ||
expect(wrapper.find(Modal).prop('open')).toEqual(true); | ||
}; | ||
|
||
const errorMessageHasCorrectFiles = (wrapper, files) => { | ||
const preUploadError = wrapper.prop('preUploadError'); | ||
files.forEach((file) => { | ||
expect(preUploadError).toContain(file); | ||
}); | ||
}; | ||
|
||
let wrapper; | ||
|
||
describe('AssetsUploadConfirm', () => { | ||
beforeEach(() => { | ||
mockQuerySelector.init(); | ||
}); | ||
afterEach(() => { | ||
mockQuerySelector.reset(); | ||
}); | ||
|
||
describe('renders', () => { | ||
beforeEach(() => { | ||
wrapper = mountWithIntl( | ||
<AssetsUploadConfirm | ||
{...defaultProps} | ||
/>, | ||
); | ||
}); | ||
|
||
it('closed by default', () => { | ||
modalIsClosed(wrapper); | ||
}); | ||
|
||
it('open if there is an error message', () => { | ||
wrapper.setProps({ | ||
preUploadError: ['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: ['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,26 @@ | ||
import { defineMessages } from 'react-intl'; | ||
|
||
const messages = defineMessages({ | ||
assetsUploadConfirmMessage: { | ||
id: 'assetsUploadConfirmMessage', | ||
defaultMessage: 'The following files will be overwritten: {listOfFiles}', | ||
description: 'The message displayed in the modal shown when uploading files with pre-existing names', | ||
}, | ||
assetsUploadConfirmTitle: { | ||
id: 'assetsUploadConfirmTitle', | ||
defaultMessage: 'Overwrite Files', | ||
description: 'The title of the modal to confirm overwriting the files', | ||
}, | ||
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,102 @@ | ||
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.length !== 0, | ||
}); | ||
}; | ||
|
||
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 listOfFiles = ( | ||
<ul> | ||
{ preUploadError.sort().map(item => <li key={item}>{item}</li>) } | ||
</ul> | ||
); | ||
const content = ( | ||
<WrappedMessage | ||
message={messages.assetsUploadConfirmMessage} | ||
values={{ listOfFiles }} | ||
/> | ||
); | ||
const closeText = ( | ||
<WrappedMessage message={messages.assetsUploadConfirmCancel} /> | ||
); | ||
const button = ( | ||
<Button | ||
buttonType="primary" | ||
label={<WrappedMessage message={messages.assetsUploadConfirmOverwrite} />} | ||
onClick={uploadFiles} | ||
/> | ||
); | ||
|
||
return ( | ||
<div id={modalWrapperID}> | ||
<Modal | ||
title={<WrappedMessage message={messages.assetsUploadConfirmTitle} />} | ||
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.arrayOf(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.