Skip to content

Commit 3b2d5ba

Browse files
committed
✅ add tests for folder filename
1 parent a40fcd3 commit 3b2d5ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+18137
-16
lines changed

client/components/__test__/FileNode.test.jsx

Lines changed: 114 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,52 @@ beforeAll(() => {});
66
describe('<FileNode />', () => {
77
let component;
88
let props = {};
9+
let input;
10+
let renameTriggerButton;
11+
const changeName = (newFileName) => {
12+
renameTriggerButton.simulate('click');
13+
input.simulate('change', { target: { value: newFileName } });
14+
input.simulate('blur');
15+
};
16+
const getState = () => component.state();
17+
const getUpdatedName = () => getState().updatedName;
918

10-
describe('with valid props', () => {
19+
describe('with valid props, regardless of filetype', () => {
20+
[''].forEach((fileType) => {
21+
beforeEach(() => {
22+
props = {
23+
...props,
24+
id: '0',
25+
name: 'test.jsx',
26+
fileType,
27+
canEdit: true,
28+
children: [],
29+
authenticated: false,
30+
setSelectedFile: jest.fn(),
31+
deleteFile: jest.fn(),
32+
updateFileName: jest.fn(),
33+
resetSelectedFile: jest.fn(),
34+
newFile: jest.fn(),
35+
newFolder: jest.fn(),
36+
showFolderChildren: jest.fn(),
37+
hideFolderChildren: jest.fn(),
38+
openUploadFileModal: jest.fn()
39+
};
40+
component = shallow(<FileNode {...props} />);
41+
});
42+
});
43+
});
44+
45+
describe('as file with valid props', () => {
1146
beforeEach(() => {
1247
props = {
1348
...props,
1449
id: '0',
15-
children: [],
1650
name: 'test.jsx',
17-
fileType: 'dunno',
51+
fileType: 'file',
52+
canEdit: true,
53+
children: [],
54+
authenticated: false,
1855
setSelectedFile: jest.fn(),
1956
deleteFile: jest.fn(),
2057
updateFileName: jest.fn(),
@@ -23,22 +60,12 @@ describe('<FileNode />', () => {
2360
newFolder: jest.fn(),
2461
showFolderChildren: jest.fn(),
2562
hideFolderChildren: jest.fn(),
26-
canEdit: true,
27-
authenticated: false,
2863
openUploadFileModal: jest.fn()
2964
};
3065
component = shallow(<FileNode {...props} />);
3166
});
3267

3368
describe('when changing name', () => {
34-
let input;
35-
let renameTriggerButton;
36-
const changeName = (newFileName) => {
37-
renameTriggerButton.simulate('click');
38-
input.simulate('change', { target: { value: newFileName } });
39-
input.simulate('blur');
40-
};
41-
4269
beforeEach(() => {
4370
input = component.find('.sidebar__file-item-input');
4471
renameTriggerButton = component
@@ -55,17 +82,88 @@ describe('<FileNode />', () => {
5582
beforeEach(() => changeName(newName));
5683

5784
it('should save the name', () => {
85+
console.log('component.state');
86+
console.log(component.state());
5887
expect(props.updateFileName).toBeCalledWith(props.id, newName);
5988
});
6089
});
6190

91+
// Failure Scenarios
92+
6293
describe('to an empty filename', () => {
6394
const newName = '';
6495
beforeEach(() => changeName(newName));
6596

66-
it('should not save the name', () => {
67-
expect(props.updateFileName).not.toHaveBeenCalled();
68-
});
97+
98+
it('should not save', () => expect(props.updateFileName).not.toHaveBeenCalled());
99+
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
100+
});
101+
102+
describe('to an extensionless filename', () => {
103+
const newName = 'extensionless';
104+
beforeEach(() => changeName(newName));
105+
106+
it('should not save', () => expect(props.updateFileName).not.toHaveBeenCalled());
107+
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
108+
});
109+
110+
describe('to different extension', () => {
111+
const newName = 'name.gif';
112+
beforeEach(() => changeName(newName));
113+
114+
it('should not save', () => expect(props.updateFileName).not.toHaveBeenCalled());
115+
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
116+
});
117+
118+
describe('to just an extension', () => {
119+
const newName = '.jsx';
120+
beforeEach(() => changeName(newName));
121+
122+
it('should not save', () => expect(props.updateFileName).not.toHaveBeenCalled());
123+
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
124+
});
125+
});
126+
});
127+
128+
129+
describe('as folder with valid props', () => {
130+
beforeEach(() => {
131+
props = {
132+
...props,
133+
id: '0',
134+
children: [],
135+
name: 'filename',
136+
fileType: 'folder',
137+
canEdit: true,
138+
authenticated: false,
139+
setSelectedFile: jest.fn(),
140+
deleteFile: jest.fn(),
141+
updateFileName: jest.fn(),
142+
resetSelectedFile: jest.fn(),
143+
newFile: jest.fn(),
144+
newFolder: jest.fn(),
145+
showFolderChildren: jest.fn(),
146+
hideFolderChildren: jest.fn(),
147+
openUploadFileModal: jest.fn()
148+
};
149+
component = shallow(<FileNode {...props} />);
150+
});
151+
152+
describe('when changing name', () => {
153+
beforeEach(() => {
154+
input = component.find('.sidebar__file-item-input');
155+
renameTriggerButton = component
156+
.find('.sidebar__file-item-option')
157+
.first();
158+
component.setState({ isEditing: true });
159+
});
160+
161+
describe('to a filename', () => {
162+
const newName = 'filename.jsx';
163+
beforeEach(() => changeName(newName));
164+
165+
it('should not save', () => expect(props.updateFileName).not.toHaveBeenCalled());
166+
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
69167
});
70168
});
71169
});

0 commit comments

Comments
 (0)