Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions client/modules/IDE/components/FileNode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { useSelector } from 'react-redux';

import * as IDEActions from '../actions/ide';
import * as FileActions from '../actions/files';
import { showToast as showToastAction } from '../actions/toast';
import { CREATE_FILE_REGEX } from '../../../../server/utils/fileUtils';
import DownArrowIcon from '../../../images/down-filled-triangle.svg';
import FolderRightIcon from '../../../images/triangle-arrow-right.svg';
import FolderDownIcon from '../../../images/triangle-arrow-down.svg';
Expand Down Expand Up @@ -82,7 +84,8 @@ const FileNode = ({
canEdit,
openUploadFileModal,
authenticated,
onClickFile
onClickFile,
showToast
}) => {
const [isOptionsOpen, setIsOptionsOpen] = useState(false);
const [isEditingName, setIsEditingName] = useState(false);
Expand Down Expand Up @@ -195,6 +198,13 @@ const FileNode = ({
const hasEmptyFilename = updatedName.trim() === '';
const hasOnlyExtension =
newFileExtension && updatedName.trim() === newFileExtension[0];

if (fileType === 'file' && !updatedName.match(CREATE_FILE_REGEX)) {
showToast(t('NewFileModal.InvalidType'));
setUpdatedName(currentName);
return;
}

if (
hasEmptyFilename ||
hasNoExtension ||
Expand Down Expand Up @@ -412,7 +422,8 @@ FileNode.propTypes = {
canEdit: PropTypes.bool.isRequired,
openUploadFileModal: PropTypes.func.isRequired,
authenticated: PropTypes.bool.isRequired,
onClickFile: PropTypes.func
onClickFile: PropTypes.func,
showToast: PropTypes.func.isRequired
};

FileNode.defaultProps = {
Expand All @@ -433,7 +444,11 @@ function mapStateToProps(state, ownProps) {
});
}

const mapDispatchToProps = { ...FileActions, ...IDEActions };
const mapDispatchToProps = {
...FileActions,
...IDEActions,
showToast: showToastAction
};

const ConnectedFileNode = connect(
mapStateToProps,
Expand Down
24 changes: 18 additions & 6 deletions client/modules/IDE/components/FileNode.unit.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('<FileNode />', () => {
const props = {
...extraProps,
id: '0',
name: fileType === 'folder' ? 'afolder' : 'test.jsx',
name: fileType === 'folder' ? 'afolder' : 'test.js',
fileType,
canEdit: true,
children: [],
Expand All @@ -48,7 +48,8 @@ describe('<FileNode />', () => {
showFolderChildren: jest.fn(),
hideFolderChildren: jest.fn(),
openUploadFileModal: jest.fn(),
setProjectName: jest.fn()
setProjectName: jest.fn(),
showToast: jest.fn()
};

const mockFiles = [
Expand Down Expand Up @@ -100,7 +101,7 @@ describe('<FileNode />', () => {
});

it('can change to a valid filename', async () => {
const newName = 'newname.jsx';
const newName = 'newname.js';
const props = renderFileNode('file');

changeName(newName);
Expand All @@ -125,7 +126,7 @@ describe('<FileNode />', () => {
const mockConfirm = jest.fn(() => true);
window.confirm = mockConfirm;

const newName = 'newname.gif';
const newName = 'newname.json';
const props = renderFileNode('file');

changeName(newName);
Expand All @@ -137,8 +138,19 @@ describe('<FileNode />', () => {
await expectFileNameToBe(props.name);
});

it('cannot change to an invalid extension', async () => {
const newName = 'newname.obj';
const props = renderFileNode('file');

changeName(newName);

await waitFor(() => expect(props.updateFileName).not.toHaveBeenCalled());
expect(props.showToast).toHaveBeenCalled();
await expectFileNameToBe(props.name);
});

it('cannot be just an extension', async () => {
const newName = '.jsx';
const newName = '.js';
const props = renderFileNode('file');

changeName(newName);
Expand Down Expand Up @@ -171,7 +183,7 @@ describe('<FileNode />', () => {
});

it('cannot have a file extension', async () => {
const newName = 'foldername.jsx';
const newName = 'foldername.js';
const props = renderFileNode('folder');

changeName(newName);
Expand Down