2020-04-16 18:03:21 +02:00
|
|
|
import React from 'react';
|
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
import { FileNode } from '../../modules/IDE/components/FileNode';
|
|
|
|
|
|
|
|
describe('<FileNode />', () => {
|
|
|
|
let component;
|
|
|
|
let props = {};
|
2020-04-30 17:41:48 +02:00
|
|
|
let input;
|
|
|
|
let renameTriggerButton;
|
|
|
|
const changeName = (newFileName) => {
|
|
|
|
renameTriggerButton.simulate('click');
|
|
|
|
input.simulate('change', { target: { value: newFileName } });
|
|
|
|
input.simulate('blur');
|
|
|
|
};
|
|
|
|
const getState = () => component.state();
|
|
|
|
const getUpdatedName = () => getState().updatedName;
|
2020-04-16 18:03:21 +02:00
|
|
|
|
2020-04-30 17:41:48 +02:00
|
|
|
describe('with valid props, regardless of filetype', () => {
|
2020-04-30 17:59:41 +02:00
|
|
|
['folder', 'file'].forEach((fileType) => {
|
2020-04-30 17:41:48 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
props = {
|
|
|
|
...props,
|
|
|
|
id: '0',
|
|
|
|
name: 'test.jsx',
|
|
|
|
fileType,
|
|
|
|
canEdit: true,
|
|
|
|
children: [],
|
|
|
|
authenticated: false,
|
|
|
|
setSelectedFile: jest.fn(),
|
|
|
|
deleteFile: jest.fn(),
|
|
|
|
updateFileName: jest.fn(),
|
|
|
|
resetSelectedFile: jest.fn(),
|
|
|
|
newFile: jest.fn(),
|
|
|
|
newFolder: jest.fn(),
|
|
|
|
showFolderChildren: jest.fn(),
|
|
|
|
hideFolderChildren: jest.fn(),
|
2020-05-01 16:17:21 +02:00
|
|
|
openUploadFileModal: jest.fn(),
|
|
|
|
setProjectName: jest.fn(),
|
2020-04-30 17:41:48 +02:00
|
|
|
};
|
|
|
|
component = shallow(<FileNode {...props} />);
|
|
|
|
});
|
2020-04-30 17:59:41 +02:00
|
|
|
|
|
|
|
describe('when changing name', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
input = component.find('.sidebar__file-item-input');
|
|
|
|
renameTriggerButton = component
|
|
|
|
.find('.sidebar__file-item-option')
|
|
|
|
.first();
|
|
|
|
component.setState({ isEditing: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('to an empty name', () => {
|
|
|
|
const newName = '';
|
|
|
|
beforeEach(() => changeName(newName));
|
|
|
|
|
|
|
|
it('should not save', () => expect(props.updateFileName).not.toHaveBeenCalled());
|
|
|
|
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
|
|
|
|
});
|
|
|
|
});
|
2020-04-30 17:41:48 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as file with valid props', () => {
|
2020-04-16 18:03:21 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
props = {
|
|
|
|
...props,
|
|
|
|
id: '0',
|
|
|
|
name: 'test.jsx',
|
2020-04-30 17:41:48 +02:00
|
|
|
fileType: 'file',
|
|
|
|
canEdit: true,
|
|
|
|
children: [],
|
|
|
|
authenticated: false,
|
2020-04-16 18:03:21 +02:00
|
|
|
setSelectedFile: jest.fn(),
|
|
|
|
deleteFile: jest.fn(),
|
|
|
|
updateFileName: jest.fn(),
|
|
|
|
resetSelectedFile: jest.fn(),
|
|
|
|
newFile: jest.fn(),
|
|
|
|
newFolder: jest.fn(),
|
|
|
|
showFolderChildren: jest.fn(),
|
|
|
|
hideFolderChildren: jest.fn(),
|
2020-04-28 20:52:41 +02:00
|
|
|
openUploadFileModal: jest.fn()
|
2020-04-16 18:03:21 +02:00
|
|
|
};
|
|
|
|
component = shallow(<FileNode {...props} />);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when changing name', () => {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-30 17:41:48 +02:00
|
|
|
// Failure Scenarios
|
|
|
|
|
|
|
|
describe('to an extensionless filename', () => {
|
|
|
|
const newName = 'extensionless';
|
|
|
|
beforeEach(() => changeName(newName));
|
|
|
|
});
|
2020-05-01 16:17:21 +02:00
|
|
|
it('should not save', () => expect(props.setProjectName).not.toHaveBeenCalled());
|
|
|
|
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
|
2020-04-30 17:41:48 +02:00
|
|
|
describe('to different extension', () => {
|
|
|
|
const newName = 'name.gif';
|
|
|
|
beforeEach(() => changeName(newName));
|
|
|
|
|
2020-05-01 16:17:21 +02:00
|
|
|
it('should not save', () => expect(props.setProjectName).not.toHaveBeenCalled());
|
2020-04-30 17:41:48 +02:00
|
|
|
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('to just an extension', () => {
|
|
|
|
const newName = '.jsx';
|
|
|
|
beforeEach(() => changeName(newName));
|
|
|
|
|
|
|
|
it('should not save', () => expect(props.updateFileName).not.toHaveBeenCalled());
|
|
|
|
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('as folder with valid props', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
props = {
|
|
|
|
...props,
|
|
|
|
id: '0',
|
|
|
|
children: [],
|
|
|
|
name: 'filename',
|
|
|
|
fileType: 'folder',
|
|
|
|
canEdit: true,
|
|
|
|
authenticated: false,
|
|
|
|
setSelectedFile: jest.fn(),
|
|
|
|
deleteFile: jest.fn(),
|
|
|
|
updateFileName: jest.fn(),
|
|
|
|
resetSelectedFile: jest.fn(),
|
|
|
|
newFile: jest.fn(),
|
|
|
|
newFolder: jest.fn(),
|
|
|
|
showFolderChildren: jest.fn(),
|
|
|
|
hideFolderChildren: jest.fn(),
|
|
|
|
openUploadFileModal: jest.fn()
|
|
|
|
};
|
|
|
|
component = shallow(<FileNode {...props} />);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when changing name', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
input = component.find('.sidebar__file-item-input');
|
|
|
|
renameTriggerButton = component
|
|
|
|
.find('.sidebar__file-item-option')
|
|
|
|
.first();
|
|
|
|
component.setState({ isEditing: true });
|
|
|
|
});
|
|
|
|
|
2020-04-30 17:47:12 +02:00
|
|
|
describe('to a foldername', () => {
|
|
|
|
const newName = 'newfoldername';
|
|
|
|
beforeEach(() => changeName(newName));
|
|
|
|
|
|
|
|
it('should save', () => expect(props.updateFileName).toBeCalledWith(props.id, newName));
|
|
|
|
it('should update name', () => expect(getUpdatedName()).toEqual(newName));
|
|
|
|
});
|
|
|
|
|
2020-04-30 17:41:48 +02:00
|
|
|
describe('to a filename', () => {
|
|
|
|
const newName = 'filename.jsx';
|
|
|
|
beforeEach(() => changeName(newName));
|
|
|
|
|
|
|
|
it('should not save', () => expect(props.updateFileName).not.toHaveBeenCalled());
|
|
|
|
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
|
2020-04-16 18:03:21 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|