Skip to content

Commit 33db5fb

Browse files
authored
Merge pull request #1372 from ghalestrilo/fix/rename-file-set-unsaved
Fixes #681 - File name update triggers editor change detection
2 parents 8114a88 + 2c9fd98 commit 33db5fb

File tree

5 files changed

+299
-197
lines changed

5 files changed

+299
-197
lines changed

Diff for: client/components/__test__/FileNode.test.jsx

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React from 'react';
2+
import { shallow } from 'enzyme';
3+
import { FileNode } from '../../modules/IDE/components/FileNode';
4+
5+
beforeAll(() => {});
6+
describe('<FileNode />', () => {
7+
let component;
8+
let props = {};
9+
10+
describe('with valid props', () => {
11+
beforeEach(() => {
12+
props = {
13+
...props,
14+
id: '0',
15+
children: [],
16+
name: 'test.jsx',
17+
fileType: 'dunno',
18+
setSelectedFile: jest.fn(),
19+
deleteFile: jest.fn(),
20+
updateFileName: jest.fn(),
21+
resetSelectedFile: jest.fn(),
22+
newFile: jest.fn(),
23+
newFolder: jest.fn(),
24+
showFolderChildren: jest.fn(),
25+
hideFolderChildren: jest.fn(),
26+
canEdit: true,
27+
authenticated: false
28+
};
29+
component = shallow(<FileNode {...props} />);
30+
});
31+
32+
describe('when changing name', () => {
33+
let input;
34+
let renameTriggerButton;
35+
const changeName = (newFileName) => {
36+
renameTriggerButton.simulate('click');
37+
input.simulate('change', { target: { value: newFileName } });
38+
input.simulate('blur');
39+
};
40+
41+
beforeEach(() => {
42+
input = component.find('.sidebar__file-item-input');
43+
renameTriggerButton = component
44+
.find('.sidebar__file-item-option')
45+
.first();
46+
component.setState({ isEditing: true });
47+
});
48+
it('should render', () => expect(component).toBeDefined());
49+
50+
// it('should debug', () => console.log(component.debug()));
51+
52+
describe('to a valid filename', () => {
53+
const newName = 'newname.jsx';
54+
beforeEach(() => changeName(newName));
55+
56+
it('should save the name', () => {
57+
expect(props.updateFileName).toBeCalledWith(props.id, newName);
58+
});
59+
});
60+
61+
describe('to an empty filename', () => {
62+
const newName = '';
63+
beforeEach(() => changeName(newName));
64+
65+
it('should not save the name', () => {
66+
expect(props.updateFileName).not.toHaveBeenCalled();
67+
});
68+
});
69+
});
70+
});
71+
});

Diff for: client/modules/IDE/actions/files.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,13 @@ export function createFolder(formProps) {
136136
}
137137

138138
export function updateFileName(id, name) {
139-
return {
140-
type: ActionTypes.UPDATE_FILE_NAME,
141-
id,
142-
name
139+
return (dispatch) => {
140+
dispatch(setUnsavedChanges(true));
141+
dispatch({
142+
type: ActionTypes.UPDATE_FILE_NAME,
143+
id,
144+
name
145+
});
143146
};
144147
}
145148

Diff for: client/modules/IDE/actions/project.js

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export function saveProject(selectedFile = null, autosave = false) {
133133
}
134134
const formParams = Object.assign({}, state.project);
135135
formParams.files = [...state.files];
136+
136137
if (selectedFile) {
137138
const fileToUpdate = formParams.files.find(file => file.id === selectedFile.id);
138139
fileToUpdate.content = selectedFile.content;

0 commit comments

Comments
 (0)