Skip to content

Commit c4d5245

Browse files
committed
✅ write test file for <FileNode />
1 parent ddec332 commit c4d5245

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
};
28+
component = shallow(<FileNode {...props} />);
29+
});
30+
31+
describe('when changing name', () => {
32+
let input;
33+
let renameTriggerButton;
34+
const changeName = (newFileName) => {
35+
renameTriggerButton.simulate('click');
36+
input.simulate('change', { target: { value: newFileName } });
37+
input.simulate('blur');
38+
};
39+
40+
beforeEach(() => {
41+
input = component.find('.sidebar__file-item-input');
42+
renameTriggerButton = component
43+
.find('.sidebar__file-item-option')
44+
.first();
45+
component.setState({ isEditing: true });
46+
});
47+
it('should render', () => expect(component).toBeDefined());
48+
49+
// it('should debug', () => console.log(component.debug()));
50+
51+
describe('to a valid filename', () => {
52+
const newName = 'newname.jsx';
53+
beforeEach(() => changeName(newName));
54+
55+
it('should save the name', () => {
56+
expect(props.updateFileName).toBeCalledWith(props.id, newName);
57+
});
58+
});
59+
60+
describe('to an empty filename', () => {
61+
const newName = '';
62+
beforeEach(() => changeName(newName));
63+
64+
it('should not save the name', () => {
65+
expect(props.updateFileName).not.toHaveBeenCalled();
66+
});
67+
});
68+
});
69+
});
70+
});

client/modules/IDE/actions/files.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ export function createFolder(formProps) {
135135
};
136136
}
137137

138+
let callcount = 0;
138139
export function updateFileName(id, name) {
140+
callcount += 1;
141+
console.log(`called ${callcount} times`);
139142
return (dispatch) => {
140143
dispatch(setUnsavedChanges(true));
141144
dispatch({

client/modules/IDE/components/FileNode.jsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class FileNode extends React.Component {
4949
}
5050

5151
get updatedName() {
52-
return this.state.updatedName;
52+
return this.state.updatedName || this.props.name;
5353
}
5454

5555
commitFileNameChange() {
@@ -91,10 +91,9 @@ export class FileNode extends React.Component {
9191
const hasEmptyFilename = newFileName === '';
9292
const hasOnlyExtension = newFileExtension && newFileName === newFileExtension[0];
9393
if (hasEmptyFilename || hasNoExtension || notSameExtension || hasOnlyExtension || hasExtensionIfFolder) {
94+
this.setState({ updatedName: this.originalFileName });
9495
this.props.updateFileName(this.props.id, this.originalFileName);
95-
return;
96-
}
97-
this.commitFileNameChange();
96+
} else this.commitFileNameChange();
9897
}
9998

10099
toggleFileOptions(e) {
@@ -242,7 +241,7 @@ export class FileNode extends React.Component {
242241
<li>
243242
<button
244243
onClick={() => {
245-
this.originalFileName = this.updatedName;
244+
this.originalFileName = this.props.name;
246245
this.showEditFileName();
247246
setTimeout(() => this.fileNameInput.focus(), 0);
248247
setTimeout(() => this.hideFileOptions(), 0);

0 commit comments

Comments
 (0)