2020-04-30 17:59:41 +02:00
|
|
|
import React from 'react';
|
|
|
|
import { shallow } from 'enzyme';
|
2020-05-01 18:17:59 +02:00
|
|
|
import { ToolbarComponent } from '../../modules/IDE/components/Toolbar';
|
2020-04-30 17:59:41 +02:00
|
|
|
|
2020-05-01 16:17:21 +02:00
|
|
|
|
|
|
|
const initialProps = {
|
|
|
|
isPlaying: false,
|
|
|
|
preferencesIsVisible: false,
|
|
|
|
stopSketch: jest.fn(),
|
|
|
|
setProjectName: jest.fn(),
|
|
|
|
openPreferences: jest.fn(),
|
|
|
|
showEditProjectName: jest.fn(),
|
|
|
|
hideEditProjectName: jest.fn(),
|
|
|
|
infiniteLoop: false,
|
|
|
|
autorefresh: false,
|
|
|
|
setAutorefresh: jest.fn(),
|
|
|
|
setTextOutput: jest.fn(),
|
|
|
|
setGridOutput: jest.fn(),
|
|
|
|
startSketch: jest.fn(),
|
|
|
|
startAccessibleSketch: jest.fn(),
|
|
|
|
saveProject: jest.fn(),
|
|
|
|
currentUser: 'me',
|
|
|
|
originalProjectName: 'testname',
|
|
|
|
|
|
|
|
owner: {
|
|
|
|
username: 'me'
|
|
|
|
},
|
|
|
|
project: {
|
|
|
|
name: 'testname',
|
|
|
|
isEditingName: false,
|
|
|
|
id: 'id',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-05-01 18:17:59 +02:00
|
|
|
describe('<ToolbarComponent />', () => {
|
2020-04-30 18:14:41 +02:00
|
|
|
let component;
|
2020-05-01 16:17:21 +02:00
|
|
|
let props = initialProps;
|
2020-04-30 18:14:41 +02:00
|
|
|
let input;
|
|
|
|
let renameTriggerButton;
|
|
|
|
const changeName = (newFileName) => {
|
2020-05-01 16:17:21 +02:00
|
|
|
component.find('.toolbar__project-name').simulate('click', { preventDefault: jest.fn() });
|
|
|
|
input = component.find('.toolbar__project-name-input');
|
|
|
|
renameTriggerButton = component.find('.toolbar__edit-name-button');
|
2020-04-30 18:14:41 +02:00
|
|
|
renameTriggerButton.simulate('click');
|
|
|
|
input.simulate('change', { target: { value: newFileName } });
|
|
|
|
input.simulate('blur');
|
|
|
|
};
|
|
|
|
const setProps = (additionalProps) => {
|
|
|
|
props = {
|
2020-05-01 16:17:21 +02:00
|
|
|
...props,
|
|
|
|
...additionalProps,
|
|
|
|
|
2020-04-30 18:14:41 +02:00
|
|
|
project: {
|
2020-05-01 16:17:21 +02:00
|
|
|
...props.project,
|
|
|
|
...(additionalProps || {}).project
|
2020-04-30 18:14:41 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-01 16:17:21 +02:00
|
|
|
// Test Cases
|
2020-04-30 18:14:41 +02:00
|
|
|
|
2020-04-30 17:59:41 +02:00
|
|
|
describe('with valid props', () => {
|
2020-04-30 18:14:41 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
setProps();
|
2020-05-01 18:17:59 +02:00
|
|
|
component = shallow(<ToolbarComponent {...props} />);
|
2020-04-30 18:14:41 +02:00
|
|
|
});
|
|
|
|
it('renders', () => expect(component).toBeDefined());
|
2020-05-01 16:17:21 +02:00
|
|
|
|
|
|
|
describe('when use owns sketch', () => {
|
|
|
|
beforeEach(() => setProps({ currentUser: props.owner.username }));
|
|
|
|
|
|
|
|
describe('when changing sketch name', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setProps({
|
|
|
|
project: { isEditingName: true, name: 'testname' },
|
|
|
|
setProjectName: jest.fn(name => component.setProps({ project: { name } })),
|
|
|
|
});
|
2020-05-01 18:17:59 +02:00
|
|
|
component = shallow(<ToolbarComponent {...props} />);
|
2020-05-01 16:17:21 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('to a valid name', () => {
|
|
|
|
beforeEach(() => changeName('hello'));
|
|
|
|
it('should save', () => expect(props.setProjectName).toBeCalledWith('hello'));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('to an empty name', () => {
|
|
|
|
beforeEach(() => changeName(''));
|
|
|
|
it('should set name to empty', () => expect(props.setProjectName).toBeCalledWith(''));
|
|
|
|
it(
|
|
|
|
'should detect empty name and revert to original',
|
|
|
|
() => expect(props.setProjectName).toHaveBeenLastCalledWith(initialProps.project.name)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when user does not own sketch', () => {
|
|
|
|
beforeEach(() => setProps({ currentUser: 'not-the-owner' }));
|
|
|
|
|
|
|
|
it('should disable edition', () => expect(component.find('.toolbar__edit-name-button')).toEqual({}));
|
|
|
|
});
|
2020-04-30 17:59:41 +02:00
|
|
|
});
|
|
|
|
});
|