@@ -2,54 +2,106 @@ import React from 'react';
22import { shallow } from 'enzyme' ;
33import { 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+
536describe ( '<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