@@ -26,6 +26,27 @@ describe('Testing componentReducer functionality', () => {
26
26
} ) ;
27
27
} ) ;
28
28
29
+ // TEST 'ADD COMPONENT' with new root
30
+ describe ( 'ADD COMPONENT reducer' , ( ) => {
31
+ it ( 'should add new reuseable component to state as the new root' , ( ) => {
32
+ const action : Action = {
33
+ type : 'ADD COMPONENT' ,
34
+ payload : {
35
+ componentName : 'TestRootChange' ,
36
+ id : 3 ,
37
+ root : true ,
38
+ } ,
39
+ } ;
40
+ state = reducer ( state , action ) ;
41
+
42
+ // expect state.components array to have length 3
43
+ const length = state . components . length ;
44
+ expect ( length ) . toEqual ( 3 ) ;
45
+ // expect new root to match id of component id of TestRootChange
46
+ expect ( state . rootComponents [ state . rootComponents . length - 1 ] ) . toEqual ( action . payload . id ) ;
47
+ } ) ;
48
+ } ) ;
49
+
29
50
// TEST 'ADD CHILD'
30
51
describe ( 'ADD CHILD reducer' , ( ) => {
31
52
it ( 'should add child component and separator to top-level component' , ( ) => {
@@ -159,43 +180,61 @@ describe('Testing componentReducer functionality', () => {
159
180
expect ( state . projectType ) . toEqual ( action . payload . projectType ) ;
160
181
} ) ;
161
182
} ) ;
183
+
162
184
163
185
// TEST 'UNDO'
164
186
describe ( 'UNDO reducer' , ( ) => {
165
- // problem: past array up to this point is empty because snapshot function is responsible for populating it- not invoked in testing
166
- // undo is not deleting the children because it relies on Canvas.tsx's function Canvas and snapShotFunc inside it
167
- it ( 'should remove the last 2 elements in the children array' , ( ) => {
187
+ it ( 'should remove the last element from the past array and push it to the future array' , ( ) => {
168
188
const focusIndex = state . canvasFocus . componentId - 1 ;
169
- const actionHTML : Action = {
189
+ state . components [ focusIndex ] . past = [ ] ;
190
+
191
+ // snapShotFunc taken from src/components/main/canvas.tsx to test undo functionality
192
+ const snapShotFuncCopy = ( ) => {
193
+ const deepCopiedState = JSON . parse ( JSON . stringify ( state ) ) ;
194
+ // pushes the last user action on the canvas into the past array of Component
195
+ state . components [ focusIndex ] . past . push ( deepCopiedState . components [ focusIndex ] . children ) ;
196
+ }
197
+
198
+ const actionHTML2 : Action = {
170
199
type : 'ADD CHILD' ,
171
200
payload : {
172
201
type : 'HTML Element' ,
173
- typeId : 9 ,
202
+ typeId : 4 ,
174
203
childId : null ,
175
204
}
176
205
}
177
- console . log ( 'focusIndex before UNDO' , focusIndex ) ;
178
- console . log ( 'before add child' , state . components [ focusIndex ] ) ;
179
- state = reducer ( state , actionHTML ) ;
180
- console . log ( 'after add kid' , state . components [ focusIndex ] ) ;
206
+ state = reducer ( state , actionHTML2 ) ;
207
+ snapShotFuncCopy ( ) ;
208
+
181
209
const actionUndo : Action = {
182
210
type : 'UNDO' ,
183
211
payload : { } ,
184
212
} ;
185
-
186
213
state = reducer ( state , actionUndo ) ;
187
- console . log ( 'focusIndex after UNDO' , focusIndex ) ;
188
214
189
- const undoParent = state . components . find ( comp => comp . id === state . canvasFocus . componentId ) ;
190
-
191
- expect ( undoParent . children . length ) . toEqual ( 0 ) ;
192
-
193
- console . log ( 'after undo' , state . components [ focusIndex ] ) ;
215
+ expect ( state . components [ focusIndex ] . past . length ) . toEqual ( 0 ) ;
216
+ expect ( state . components [ focusIndex ] . future . length ) . toEqual ( 1 ) ;
217
+ } )
218
+ } ) ;
194
219
195
- //expect(state.components[focusIndex].children.length).toEqual(0);
220
+
221
+ // TEST 'REDO'
222
+ describe ( 'REDO reducer' , ( ) => {
223
+ it ( 'should remove the last element from the future array and push it to the past array' , ( ) => {
224
+ const focusIndex = state . canvasFocus . componentId - 1 ;
225
+
226
+ const actionRedo : Action = {
227
+ type : 'REDO' ,
228
+ payload : { } ,
229
+ } ;
230
+ state = reducer ( state , actionRedo ) ;
231
+
232
+ expect ( state . components [ focusIndex ] . future . length ) . toEqual ( 0 ) ;
233
+ expect ( state . components [ focusIndex ] . past . length ) . toEqual ( 1 ) ;
196
234
} )
197
235
} ) ;
198
236
237
+
199
238
// TEST 'RESET STATE'
200
239
describe ( 'RESET STATE reducer' , ( ) => {
201
240
it ( 'should reset project to initial state' , ( ) => {
@@ -212,19 +251,5 @@ describe('Testing componentReducer functionality', () => {
212
251
expect ( state . components [ 0 ] . children . length ) . toEqual ( 0 ) ;
213
252
} ) ;
214
253
} ) ;
215
-
216
254
} ) ;
217
255
218
- // state.components[focusIndex].past.push({
219
- // type: "HTML Element", typeId: 1000, name: "separator", childId: 1001, style: { border: "none" }, children: []
220
- // },
221
- // {
222
- // type: "HTML Element", typeId: 4, name: "button", childId: 2, style: { border: "none" }, children: []
223
- // });
224
- // state.components[focusIndex].children.push({
225
- // type: "HTML Element", typeId: 1000, name: "separator", childId: 1001, style: { border: "none" }, children: []
226
- // },
227
- // {
228
- // type: "HTML Element", typeId: 4, name: "button", childId: 2, style: { border: "none" }, children: []
229
- // }
230
- // );
0 commit comments