@@ -11,7 +11,7 @@ describe('Testing componentReducer functionality', function () {
11
11
// TEST 'ADD COMPONENT'
12
12
describe ( 'ADD COMPONENT reducer' , ( ) => {
13
13
it ( 'should add new reuseable component to state' , ( ) => {
14
- const action = {
14
+ const action : Action = {
15
15
type : 'ADD COMPONENT' ,
16
16
payload : {
17
17
componentName : "TestRegular" ,
@@ -30,7 +30,7 @@ describe('Testing componentReducer functionality', function () {
30
30
// TEST 'ADD CHILD'
31
31
describe ( 'ADD CHILD reducer' , ( ) => {
32
32
it ( 'should add child component to top-level component' , ( ) => {
33
- const action = {
33
+ const action : Action = {
34
34
type : 'ADD CHILD' ,
35
35
payload : {
36
36
type : 'Component' ,
@@ -40,45 +40,136 @@ describe('Testing componentReducer functionality', function () {
40
40
}
41
41
// switch focus to very first root component
42
42
state . canvasFocus = { componentId : 1 , childId : null } ;
43
- console . log ( state ) ;
44
43
state = reducer ( state , action ) ;
45
44
const newParent = state . components [ 0 ] ;
46
45
// expect new parent's children array to have length 1
47
46
expect ( newParent . children . length ) . toEqual ( 1 ) ;
48
47
// expect new child to have type 'Component'
49
- console . log ( 'new child ' , newParent . children [ 0 ] ) ;
50
48
expect ( newParent . children [ 0 ] . type ) . toEqual ( 'Component' ) ;
51
49
const addedChild = state . components . find ( comp => comp . id === newParent . children [ 0 ] . typeId ) ;
52
50
// expect new child typeId to correspond to component with name 'TestRegular'
53
51
expect ( addedChild . name ) . toEqual ( 'TestRegular' ) ;
54
52
} )
55
53
} )
56
-
54
+
55
+ // TEST 'CHANGE POSITION'
56
+ describe ( 'CHANGE POSITION reducer ' , ( ) => {
57
+ it ( 'should move position of an instance' , ( ) => {
58
+ const actionHtml : Action = {
59
+ type : 'ADD CHILD' ,
60
+ payload : {
61
+ type : 'HTML Element' ,
62
+ typeId : 9 ,
63
+ childId : null
64
+ }
65
+ }
66
+ state = reducer ( state , actionHtml ) ;
67
+ const actionChangePos : Action = {
68
+ type : 'CHANGE POSITION' ,
69
+ payload : {
70
+ currentChildId : 1 ,
71
+ newParentChildId : null
72
+ }
73
+ }
74
+ state = reducer ( state , actionChangePos ) ;
75
+ const changeParent = state . components . find ( comp => comp . id === state . canvasFocus . componentId ) ;
76
+ const changeParentChildLength = changeParent . children . length ;
77
+ // expect last child of parent to be moved Component element
78
+ expect ( changeParent . children [ changeParentChildLength - 1 ] . type ) . toEqual ( 'Component' ) ;
79
+ // expect last child of parent to have current child ID of payload
80
+ expect ( changeParent . children [ changeParentChildLength - 1 ] . childId ) . toEqual ( 1 ) ;
81
+ } )
82
+ } )
83
+
84
+ // TEST 'DELETE CHILD'
85
+ describe ( 'DELETE CHILD reducer' , ( ) => {
86
+ it ( 'should delete child of focused top-level component' , ( ) => {
87
+ // canvas still focused on childId: 2, which is an HTML element
88
+ const action : Action = {
89
+ type : 'DELETE CHILD'
90
+ }
91
+ state = reducer ( state , action ) ;
92
+ // expect only one remaining child
93
+ const delParent = state . components . find ( comp => comp . id === state . canvasFocus . componentId ) ;
94
+ // expect remaining child to have type 'Component'
95
+ expect ( delParent . children . length ) . toEqual ( 1 ) ;
96
+ expect ( delParent . children [ delParent . children . length - 1 ] . type ) . toEqual ( 'Component' ) ;
97
+ } )
98
+ } )
99
+
57
100
// TEST 'CHANGE FOCUS'
58
101
describe ( 'CHANGE FOCUS reducer' , ( ) => {
59
102
it ( 'should change focus to specified component' , ( ) => {
60
- const action = {
103
+ const action : Action = {
61
104
type : 'CHANGE FOCUS' ,
62
105
payload : {
63
106
componentId : 2 ,
64
107
childId : null
65
108
}
66
109
}
67
- console . log ( 'before change focus ' , state . canvasFocus ) ;
68
110
state = reducer ( state , action ) ;
69
- console . log ( 'after change focus ' , state . canvasFocus ) ;
70
111
expect ( state . canvasFocus . componentId ) . toEqual ( 2 ) ;
71
112
expect ( state . canvasFocus . childId ) . toEqual ( null ) ;
72
113
} )
73
114
} )
74
115
75
116
// TEST 'UPDATE CSS'
76
117
describe ( 'UPDATE CSS reducer' , ( ) => {
77
- it ( 'should update background color of focused component' , ( ) => {
78
-
118
+ it ( 'should add style to focused component' , ( ) => {
119
+ const action : Action = {
120
+ type : 'UPDATE CSS' ,
121
+ payload : {
122
+ style : {
123
+ backgroundColor : 'gray'
124
+ }
125
+ }
126
+ }
127
+ state = reducer ( state , action ) ;
128
+ const styledComp = state . components . find ( comp => comp . id === state . canvasFocus . componentId ) ;
129
+ // expect the style property on targeted comp to equal style property in payload
130
+ expect ( styledComp . style . backgroundColor ) . toEqual ( action . payload . style . backgroundColor ) ;
131
+ } )
132
+ } )
133
+
134
+ // TEST 'UPDATE PROJECT NAME'
135
+ describe ( 'UPDATE PROJECT NAME reducer' , ( ) => {
136
+ it ( 'should update project with specified name' , ( ) => {
137
+ const action : Action = {
138
+ type : 'UPDATE PROJECT NAME' ,
139
+ payload : 'TESTNAME'
140
+ }
141
+ state = reducer ( state , action ) ;
142
+ // expect state name to equal payload
143
+ expect ( state . name ) . toEqual ( action . payload ) ;
144
+ } )
145
+ } )
146
+
147
+ // TEST 'CHANGE PROJECT TYPE'
148
+ describe ( 'CHANGE PROJECT TYPE reducer' , ( ) => {
149
+ it ( 'should change project type to specified type' , ( ) => {
150
+ const action : Action = {
151
+ type : 'CHANGE PROJECT TYPE' ,
152
+ payload : {
153
+ projectType : 'Classic React'
154
+ }
155
+ } ;
156
+ state = reducer ( state , action ) ;
157
+ expect ( state . projectType ) . toEqual ( action . payload . projectType ) ;
79
158
} )
80
159
} )
81
160
82
161
// TEST 'RESET STATE'
162
+ describe ( 'RESET STATE reducer' , ( ) => {
163
+ it ( 'should reset project to initial state' , ( ) => {
164
+ const action : Action = {
165
+ type : 'RESET STATE'
166
+ }
167
+ state = reducer ( state , action ) ;
168
+ // expect default project to have empty string as name
169
+ expect ( state . name ) . toEqual ( '' ) ;
170
+ // expect default project to only have one component in components array
171
+ expect ( state . components . length ) . toEqual ( 1 ) ;
172
+ } )
173
+ } )
83
174
84
175
} )
0 commit comments