Skip to content

Commit 2682a69

Browse files
committed
Editing Jest Undo Reducer Test
1 parent a3b29f9 commit 2682a69

File tree

4 files changed

+53
-28
lines changed

4 files changed

+53
-28
lines changed

__tests__/componentReducer.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import reducer from '../app/src/reducers/componentReducer';
2-
import { State, Action } from '../app/src/interfaces/InterfacesNew';
2+
import { State, Action } from '../app/src/interfaces/Interfaces';
33

44
import initialState from '../app/src/context/initialState';
5+
import { iterate } from 'localforage';
56

67
describe('Testing componentReducer functionality', () => {
78
let state: State = initialState;
@@ -159,6 +160,48 @@ describe('Testing componentReducer functionality', () => {
159160
});
160161
});
161162

163+
// TEST 'UNDO'
164+
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+
// other ideas: take a snapshot in each of the tests in order to populate the past array
167+
it('should move the last element in past array to the future array, both nested within state.components', () => {
168+
const focusIndex = state.canvasFocus.componentId - 1;
169+
state.components[focusIndex].past.push({
170+
type: "HTML Element", typeId: 1000, name: "separator", childId: 1001, style: { border: "none" }, children: []
171+
},
172+
{
173+
type: "HTML Element", typeId: 4, name: "button", childId: 2, style: { border: "none" }, children: []
174+
});
175+
state.components[focusIndex].children.push({
176+
type: "HTML Element", typeId: 1000, name: "separator", childId: 1001, style: { border: "none" }, children: []
177+
},
178+
{
179+
type: "HTML Element", typeId: 4, name: "button", childId: 2, style: { border: "none" }, children: []
180+
}
181+
);
182+
// const actionHTML: Action = {
183+
// type: 'ADD CHILD',
184+
// payload: {
185+
// type: 'HTML Element',
186+
// typeId: 9,
187+
// childId: null,
188+
// },
189+
// };
190+
// state = reducer(state, actionHTML);
191+
const actionUndo: Action = {
192+
type: 'UNDO',
193+
payload: {},
194+
}
195+
console.log('state 1', state)
196+
state = reducer(state, actionUndo);
197+
console.log('state 2', state);
198+
//const undidEl = state.components[focusIndex].past.pop();
199+
//const undidEl = state.components[focusIndex].past.pop()
200+
expect(state.components[focusIndex].future.length).toEqual(1);
201+
//expect(state.components[focusIndex].past
202+
})
203+
});
204+
162205
// TEST 'RESET STATE'
163206
describe('RESET STATE reducer', () => {
164207
it('should reset project to initial state', () => {
@@ -175,4 +218,6 @@ describe('Testing componentReducer functionality', () => {
175218
expect(state.components[0].children.length).toEqual(0);
176219
});
177220
});
221+
178222
});
223+

app/src/components/main/Canvas.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import renderChildren from '../../helperFunctions/renderChildren';
1010

1111
function Canvas() {
1212
const [state, dispatch] = useContext(StateContext);
13+
console.log('state', state);
1314
// find the current component to render on the canvas
1415
const currentComponent: Component = state.components.find(
1516
(elem: Component) => elem.id === state.canvasFocus.componentId
@@ -33,13 +34,7 @@ function Canvas() {
3334
const focusIndex = state.canvasFocus.componentId - 1;
3435
//pushes the last user action on the canvas into the past array of Component
3536
state.components[focusIndex].past.push(deepCopiedState.components[focusIndex].children);
36-
37-
/** OLD CODE */
38-
// state.past.push(deepCopiedState.components[focusIndex].children);
39-
// state.past.push(deepCopiedState.components[state.canvasFocus.componentId].children);
40-
// state.arrowMovements.push(deepCopiedState.canvasFocus)
41-
// console.log('state in snapshotFunc', state)
42-
};
37+
};
4338

4439
// This hook will allow the user to drag items from the left panel on to the canvas
4540
const [{ isOver }, drop] = useDrop({

app/src/reducers/componentReducer.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,11 @@ const reducer = (state: State, action: Action) => {
215215
};
216216
components.push(newComponent);
217217

218+
// functionality if the new component will become the root component
218219
const rootComponents = [...state.rootComponents];
219220
if (action.payload.root) rootComponents.push(newComponent.id);
220221

221-
// update the focus to the new component
222+
// updates the focus to the new component, which redirects to the new blank canvas of said new component
222223
const canvasFocus = {
223224
...state.canvasFocus,
224225
componentId: newComponent.id,
@@ -344,7 +345,7 @@ const reducer = (state: State, action: Action) => {
344345
case 'CHANGE POSITION': {
345346
const { currentChildId, newParentChildId } = action.payload;
346347

347-
// if the currentChild Id is the same as the newParentId (i.e. a component is trying to drop itself into itself), don't update sate
348+
// if the currentChild Id is the same as the newParentId (i.e. a component is trying to drop itself into itself), don't update state
348349
if (currentChildId === newParentChildId) return state;
349350

350351
// find the current component in focus
@@ -628,14 +629,6 @@ const reducer = (state: State, action: Action) => {
628629
//the last element of the past array gets popped out
629630
state.components[focusIndex].future.push(state.components[focusIndex].past.pop())
630631

631-
632-
/**OLD CODE */
633-
// //if past is empty, return state
634-
// if (state.past.length === 0) return {...state};
635-
// //the children array of state.components[0] will equal the last element of the past array
636-
// state.components[focusIndex].children = state.past[state.past.length-1];
637-
// //the last element of past array gets pushed into future;
638-
// state.future.push(state.past.pop());
639632

640633
//generate code for the Code Preview
641634
state.components.forEach((el, i) => {
@@ -662,15 +655,7 @@ const reducer = (state: State, action: Action) => {
662655
state.components[focusIndex].past.push(state.components[focusIndex].future.pop())
663656

664657

665-
666-
//nothing left to redo
667-
// if(state.future.length === 0) return {...state};
668-
// //the children array of state.components[0] will equal the last element of the future array
669-
// state.components[focusIndex].children = state.future[state.future.length - 1];
670-
// //the last element of the future array gets pushed into the past array and the last element of the future array gets popped off
671-
// state.past.push(state.future.pop());
672-
673-
// //generate code for the Code Preview
658+
// generate code for the Code Preview
674659
state.components.forEach((el, i) => {
675660
el.code = generateCode(
676661
state.components,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
"@types/chai": "^4.2.11",
176176
"@types/enzyme": "^3.10.5",
177177
"@types/enzyme-adapter-react-16": "^1.0.6",
178-
"@types/jest": "^25.1.5",
178+
"@types/jest": "^25.2.3",
179179
"apollo": "^2.32.5",
180180
"babel-eslint": "^8.2.6",
181181
"babel-jest": "^25.2.4",

0 commit comments

Comments
 (0)