@@ -2,54 +2,106 @@ import React from 'react';
2
2
import { shallow } from 'enzyme' ;
3
3
import { Toolbar } from '../../modules/IDE/components/Toolbar' ;
4
4
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
+
5
36
describe ( '<Toolbar />' , ( ) => {
6
37
let component ;
7
- let props = { } ;
38
+ let props = initialProps ;
8
39
let input ;
9
40
let renameTriggerButton ;
10
41
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' ) ;
11
45
renameTriggerButton . simulate ( 'click' ) ;
12
46
input . simulate ( 'change' , { target : { value : newFileName } } ) ;
13
47
input . simulate ( 'blur' ) ;
14
48
} ;
15
- const getState = ( ) => component . state ( ) ;
16
- const getUpdatedName = ( ) => getState ( ) . updatedName ;
17
49
const setProps = ( additionalProps ) => {
18
50
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
+
27
54
project : {
28
- name : '' ,
29
- isEditingName : false ,
30
- id : '' ,
55
+ ...props . project ,
56
+ ...( additionalProps || { } ) . project
31
57
} ,
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
44
58
} ;
45
59
} ;
46
60
61
+ // Test Cases
47
62
48
63
describe ( 'with valid props' , ( ) => {
49
64
beforeEach ( ( ) => {
50
65
setProps ( ) ;
51
66
component = shallow ( < Toolbar { ...props } /> ) ;
52
67
} ) ;
53
68
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
+ } ) ;
54
106
} ) ;
55
107
} ) ;
0 commit comments