✅ write test file for <FileNode />
This commit is contained in:
parent
ddec33270e
commit
c4d52457e5
3 changed files with 77 additions and 5 deletions
70
client/components/__test__/FileNode.test.jsx
Normal file
70
client/components/__test__/FileNode.test.jsx
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
import React from 'react';
|
||||||
|
import { shallow } from 'enzyme';
|
||||||
|
import { FileNode } from '../../modules/IDE/components/FileNode';
|
||||||
|
|
||||||
|
beforeAll(() => {});
|
||||||
|
describe('<FileNode />', () => {
|
||||||
|
let component;
|
||||||
|
let props = {};
|
||||||
|
|
||||||
|
describe('with valid props', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
props = {
|
||||||
|
...props,
|
||||||
|
id: '0',
|
||||||
|
children: [],
|
||||||
|
name: 'test.jsx',
|
||||||
|
fileType: 'dunno',
|
||||||
|
setSelectedFile: jest.fn(),
|
||||||
|
deleteFile: jest.fn(),
|
||||||
|
updateFileName: jest.fn(),
|
||||||
|
resetSelectedFile: jest.fn(),
|
||||||
|
newFile: jest.fn(),
|
||||||
|
newFolder: jest.fn(),
|
||||||
|
showFolderChildren: jest.fn(),
|
||||||
|
hideFolderChildren: jest.fn(),
|
||||||
|
canEdit: true,
|
||||||
|
};
|
||||||
|
component = shallow(<FileNode {...props} />);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when changing name', () => {
|
||||||
|
let input;
|
||||||
|
let renameTriggerButton;
|
||||||
|
const changeName = (newFileName) => {
|
||||||
|
renameTriggerButton.simulate('click');
|
||||||
|
input.simulate('change', { target: { value: newFileName } });
|
||||||
|
input.simulate('blur');
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
input = component.find('.sidebar__file-item-input');
|
||||||
|
renameTriggerButton = component
|
||||||
|
.find('.sidebar__file-item-option')
|
||||||
|
.first();
|
||||||
|
component.setState({ isEditing: true });
|
||||||
|
});
|
||||||
|
it('should render', () => expect(component).toBeDefined());
|
||||||
|
|
||||||
|
// it('should debug', () => console.log(component.debug()));
|
||||||
|
|
||||||
|
describe('to a valid filename', () => {
|
||||||
|
const newName = 'newname.jsx';
|
||||||
|
beforeEach(() => changeName(newName));
|
||||||
|
|
||||||
|
it('should save the name', () => {
|
||||||
|
expect(props.updateFileName).toBeCalledWith(props.id, newName);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('to an empty filename', () => {
|
||||||
|
const newName = '';
|
||||||
|
beforeEach(() => changeName(newName));
|
||||||
|
|
||||||
|
it('should not save the name', () => {
|
||||||
|
expect(props.updateFileName).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -135,7 +135,10 @@ export function createFolder(formProps) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let callcount = 0;
|
||||||
export function updateFileName(id, name) {
|
export function updateFileName(id, name) {
|
||||||
|
callcount += 1;
|
||||||
|
console.log(`called ${callcount} times`);
|
||||||
return (dispatch) => {
|
return (dispatch) => {
|
||||||
dispatch(setUnsavedChanges(true));
|
dispatch(setUnsavedChanges(true));
|
||||||
dispatch({
|
dispatch({
|
||||||
|
|
|
@ -49,7 +49,7 @@ export class FileNode extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
get updatedName() {
|
get updatedName() {
|
||||||
return this.state.updatedName;
|
return this.state.updatedName || this.props.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
commitFileNameChange() {
|
commitFileNameChange() {
|
||||||
|
@ -91,10 +91,9 @@ export class FileNode extends React.Component {
|
||||||
const hasEmptyFilename = newFileName === '';
|
const hasEmptyFilename = newFileName === '';
|
||||||
const hasOnlyExtension = newFileExtension && newFileName === newFileExtension[0];
|
const hasOnlyExtension = newFileExtension && newFileName === newFileExtension[0];
|
||||||
if (hasEmptyFilename || hasNoExtension || notSameExtension || hasOnlyExtension || hasExtensionIfFolder) {
|
if (hasEmptyFilename || hasNoExtension || notSameExtension || hasOnlyExtension || hasExtensionIfFolder) {
|
||||||
|
this.setState({ updatedName: this.originalFileName });
|
||||||
this.props.updateFileName(this.props.id, this.originalFileName);
|
this.props.updateFileName(this.props.id, this.originalFileName);
|
||||||
return;
|
} else this.commitFileNameChange();
|
||||||
}
|
|
||||||
this.commitFileNameChange();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleFileOptions(e) {
|
toggleFileOptions(e) {
|
||||||
|
@ -242,7 +241,7 @@ export class FileNode extends React.Component {
|
||||||
<li>
|
<li>
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
this.originalFileName = this.updatedName;
|
this.originalFileName = this.props.name;
|
||||||
this.showEditFileName();
|
this.showEditFileName();
|
||||||
setTimeout(() => this.fileNameInput.focus(), 0);
|
setTimeout(() => this.fileNameInput.focus(), 0);
|
||||||
setTimeout(() => this.hideFileOptions(), 0);
|
setTimeout(() => this.hideFileOptions(), 0);
|
||||||
|
|
Loading…
Reference in a new issue