Skip to content

Commit 64ce929

Browse files
committed
✅ write tests for toolbar
1 parent 90b1184 commit 64ce929

File tree

2 files changed

+83
-32
lines changed

2 files changed

+83
-32
lines changed

client/components/__test__/FileNode.test.jsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ describe('<FileNode />', () => {
3434
newFolder: jest.fn(),
3535
showFolderChildren: jest.fn(),
3636
hideFolderChildren: jest.fn(),
37-
openUploadFileModal: jest.fn()
37+
openUploadFileModal: jest.fn(),
38+
setProjectName: jest.fn(),
3839
};
3940
component = shallow(<FileNode {...props} />);
4041
});
@@ -108,16 +109,14 @@ describe('<FileNode />', () => {
108109
describe('to an extensionless filename', () => {
109110
const newName = 'extensionless';
110111
beforeEach(() => changeName(newName));
111-
112-
it('should not save', () => expect(props.updateFileName).not.toHaveBeenCalled());
113-
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
114112
});
115-
113+
it('should not save', () => expect(props.setProjectName).not.toHaveBeenCalled());
114+
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
116115
describe('to different extension', () => {
117116
const newName = 'name.gif';
118117
beforeEach(() => changeName(newName));
119118

120-
it('should not save', () => expect(props.updateFileName).not.toHaveBeenCalled());
119+
it('should not save', () => expect(props.setProjectName).not.toHaveBeenCalled());
121120
it('should reset name', () => expect(getUpdatedName()).toEqual(props.name));
122121
});
123122

client/components/__test__/Toolbar.test.jsx

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,106 @@ import React from 'react';
22
import { shallow } from 'enzyme';
33
import { Toolbar } from '../../modules/IDE/components/Toolbar';
44

5+
6+
const initialProps = {
7+
isPlaying: false,
8+
preferencesIsVisible: false,
9+
stopSketch: jest.fn(),
10+
setProjectName: jest.fn(),
11+
openPreferences: jest.fn(),
12+
showEditProjectName: jest.fn(),
13+
hideEditProjectName: jest.fn(),
14+
infiniteLoop: false,
15+
autorefresh: false,
16+
setAutorefresh: jest.fn(),
17+
setTextOutput: jest.fn(),
18+
setGridOutput: jest.fn(),
19+
startSketch: jest.fn(),
20+
startAccessibleSketch: jest.fn(),
21+
saveProject: jest.fn(),
22+
currentUser: 'me',
23+
originalProjectName: 'testname',
24+
25+
owner: {
26+
username: 'me'
27+
},
28+
project: {
29+
name: 'testname',
30+
isEditingName: false,
31+
id: 'id',
32+
},
33+
};
34+
35+
536
describe('<Toolbar />', () => {
637
let component;
7-
let props = {};
38+
let props = initialProps;
839
let input;
940
let renameTriggerButton;
1041
const changeName = (newFileName) => {
42+
component.find('.toolbar__project-name').simulate('click', { preventDefault: jest.fn() });
43+
input = component.find('.toolbar__project-name-input');
44+
renameTriggerButton = component.find('.toolbar__edit-name-button');
1145
renameTriggerButton.simulate('click');
1246
input.simulate('change', { target: { value: newFileName } });
1347
input.simulate('blur');
1448
};
15-
const getState = () => component.state();
16-
const getUpdatedName = () => getState().updatedName;
1749
const setProps = (additionalProps) => {
1850
props = {
19-
isPlaying: false,
20-
preferencesIsVisible: false,
21-
stopSketch: jest.fn(),
22-
setProjectName: jest.fn(),
23-
openPreferences: jest.fn(),
24-
owner: {
25-
username: ''
26-
},
51+
...props,
52+
...additionalProps,
53+
2754
project: {
28-
name: '',
29-
isEditingName: false,
30-
id: '',
55+
...props.project,
56+
...(additionalProps || {}).project
3157
},
32-
showEditProjectName: jest.fn(),
33-
hideEditProjectName: jest.fn(),
34-
infiniteLoop: false,
35-
autorefresh: false,
36-
setAutorefresh: jest.fn(),
37-
setTextOutput: jest.fn(),
38-
setGridOutput: jest.fn(),
39-
startSketch: jest.fn(),
40-
startAccessibleSketch: jest.fn(),
41-
saveProject: jest.fn(),
42-
currentUser: '',
43-
...additionalProps
4458
};
4559
};
4660

61+
// Test Cases
4762

4863
describe('with valid props', () => {
4964
beforeEach(() => {
5065
setProps();
5166
component = shallow(<Toolbar {...props} />);
5267
});
5368
it('renders', () => expect(component).toBeDefined());
69+
70+
describe('when use owns sketch', () => {
71+
beforeEach(() => setProps({ currentUser: props.owner.username }));
72+
73+
describe('when changing sketch name', () => {
74+
beforeEach(() => {
75+
setProps({
76+
project: { isEditingName: true, name: 'testname' },
77+
setProjectName: jest.fn(name => component.setProps({ project: { name } })),
78+
});
79+
component = shallow(<Toolbar {...props} />);
80+
});
81+
82+
// it('should debug', () => console.log(component.debug()));
83+
84+
describe('to a valid name', () => {
85+
beforeEach(() => changeName('hello'));
86+
it('should save', () => expect(props.setProjectName).toBeCalledWith('hello'));
87+
});
88+
89+
90+
describe('to an empty name', () => {
91+
beforeEach(() => changeName(''));
92+
it('should set name to empty', () => expect(props.setProjectName).toBeCalledWith(''));
93+
it(
94+
'should detect empty name and revert to original',
95+
() => expect(props.setProjectName).toHaveBeenLastCalledWith(initialProps.project.name)
96+
);
97+
});
98+
});
99+
});
100+
101+
describe('when user does not own sketch', () => {
102+
beforeEach(() => setProps({ currentUser: 'not-the-owner' }));
103+
104+
it('should disable edition', () => expect(component.find('.toolbar__edit-name-button')).toEqual({}));
105+
});
54106
});
55107
});

0 commit comments

Comments
 (0)