Skip to content

Commit 0cf2c69

Browse files
atvanekrachelk585sophia-buiMatteoDiter
committed
Drag and drop/cust. panel elements test
Co-authored-by: rachelk585 <[email protected]> Co-authored-by: Sophia Bui <[email protected]> Co-authored-by: Matteo <[email protected]>
1 parent 7f42069 commit 0cf2c69

File tree

3 files changed

+123
-50
lines changed

3 files changed

+123
-50
lines changed

__tests__/DragAndDrop.test.tsx

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ import DragDropPanel from '../app/src/components/left/DragDropPanel';
77
import ComponentDrag from '../app/src/components/right/ComponentDrag';
88
import { Provider } from 'react-redux';
99
import store from '../app/src/redux/store';
10+
import MainContainer from '../app/src/containers/MainContainer';
11+
import LeftContainepr from '../app/src/containers/LeftContainer';
12+
import { StyledEngineProvider } from '@mui/material';
13+
import Main from 'electron/main';
14+
import { waitFor, within } from '@testing-library/react';
15+
import { BrowserRouter } from 'react-router-dom';
16+
import CustomizationPanel from '../app/src/containers/CustomizationPanel';
1017

1118
function TestContext(component) {
12-
return(
13-
<Provider store={store}>
14-
<DndProvider backend={HTML5Backend}>
15-
{component}
16-
</DnDProvider>
19+
return (
20+
<Provider store={store}>
21+
<DndProvider backend={HTML5Backend}>{component}</DndProvider>
1722
</Provider>
18-
)
23+
);
1924
}
2025

2126
describe('Drag and Drop Side Panel', () => {
22-
2327
test('Renders all HTML Element choices', () => {
24-
render(
25-
TestContext(<DragDropPanel/>)
26-
);
28+
render(TestContext(<DragDropPanel />));
2729
expect(screen.getByText('Div')).toBeInTheDocument();
2830
expect(screen.getByText('Img')).toBeInTheDocument();
2931
expect(screen.getByText('Form')).toBeInTheDocument();
@@ -43,22 +45,65 @@ describe('Drag and Drop Side Panel', () => {
4345
});
4446

4547
test('Renders all React Router Component choices', () => {
46-
render(
47-
TestContext(<DragDropPanel />)
48-
);
48+
render(TestContext(<DragDropPanel />));
4949

5050
expect(screen.getByText('Switch')).toBeInTheDocument();
5151
expect(screen.getByText('Route')).toBeInTheDocument();
5252
expect(screen.getByText('LinkTo')).toBeInTheDocument();
5353
});
5454

5555
test('Should render Roots Components and Reusbale components', () => {
56-
render(
57-
TestContext(<ComponentDrag/>)
58-
);
59-
56+
render(TestContext(<ComponentDrag />));
57+
6058
expect(screen.getByText('Root Components')).toBeInTheDocument();
6159
expect(screen.getByText('Reusable Components')).toBeInTheDocument();
6260
});
61+
test('test drag and drop', () => {
62+
render(
63+
TestContext(
64+
<>
65+
<DragDropPanel />
66+
<MainContainer />
67+
</>
68+
)
69+
);
70+
const drop = screen.getByTestId('drop');
71+
const div = screen.getByText('Div');
72+
expect(drop).toBeInTheDocument();
73+
expect(div).toBeInTheDocument();
74+
fireEvent.dragStart(div);
75+
fireEvent.dragEnter(drop);
76+
fireEvent.dragOver(drop);
77+
fireEvent.drop(drop);
78+
expect(within(drop).getByText('div')).toBeInTheDocument();
79+
});
6380

81+
test('Customization panel elements are properly rendering including their input values', () => {
82+
render(
83+
TestContext(
84+
<>
85+
<DragDropPanel />
86+
<MainContainer />
87+
<BrowserRouter>
88+
<CustomizationPanel />
89+
</BrowserRouter>
90+
</>
91+
)
92+
);
93+
const drop = screen.getByTestId('drop');
94+
const div = screen.getAllByText('Div')[0];
95+
expect(drop).toBeInTheDocument();
96+
expect(div).toBeInTheDocument();
97+
fireEvent.dragStart(div);
98+
fireEvent.dragEnter(drop);
99+
fireEvent.dragOver(drop);
100+
fireEvent.drop(drop);
101+
//check if customization panel elements are rendering correctly
102+
const panel = screen.getByTestId('customization')
103+
expect(within(panel).getAllByRole('textbox')).toHaveLength(4);
104+
// check dropdowns
105+
expect(
106+
within(panel).getAllByRole('button')
107+
).toHaveLength(11);
108+
});
64109
});

app/src/components/main/Canvas.tsx

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ import { combineStyles } from '../../helperFunctions/combineStyles';
66
import renderChildren from '../../helperFunctions/renderChildren';
77
import Arrow from './Arrow';
88
import { useDispatch, useSelector } from 'react-redux';
9-
import { changeFocus, addChild, snapShotAction } from '../../redux/reducers/slice/appStateSlice';
9+
import {
10+
changeFocus,
11+
addChild,
12+
snapShotAction
13+
} from '../../redux/reducers/slice/appStateSlice';
1014
import { RootState } from '../../redux/store';
1115

1216
function Canvas(props): JSX.Element {
13-
14-
const { state, contextParam,isDarkMode } = useSelector((store:RootState) => ({
15-
state: store.appState,
16-
contextParam: store.contextSlice,
17-
isDarkMode: store.darkMode.isDarkMode
18-
}));
17+
const { state, contextParam, isDarkMode } = useSelector(
18+
(store: RootState) => ({
19+
state: store.appState,
20+
contextParam: store.contextSlice,
21+
isDarkMode: store.darkMode.isDarkMode
22+
})
23+
);
1924
const dispatch = useDispatch();
2025

2126
Arrow.deleteLines();
@@ -24,23 +29,30 @@ function Canvas(props): JSX.Element {
2429
(elem: Component) => elem.id === state.canvasFocus.componentId
2530
);
2631

27-
2832
// changes focus of the canvas to a new component / child
29-
const changeFocusFunction = (componentId?: number, childId?: number | null) => {
30-
dispatch(changeFocus({ componentId, childId}));
33+
const changeFocusFunction = (
34+
componentId?: number,
35+
childId?: number | null
36+
) => {
37+
dispatch(changeFocus({ componentId, childId }));
3138
};
3239
// onClickHandler is responsible for changing the focused component and child component
3340
function onClickHandler(event) {
3441
event.stopPropagation();
35-
changeFocusFunction(state.canvasFocus.componentId,null);
42+
changeFocusFunction(state.canvasFocus.componentId, null);
3643
}
3744

3845
// stores a snapshot of state into the past array for UNDO. snapShotFunc is also invoked for nestable elements in DirectChildHTMLNestable.tsx
3946
const snapShotFunc = () => {
4047
// make a deep clone of state
41-
const deepCopiedState = JSON.parse(JSON.stringify(state));
42-
const focusIndex = state.canvasFocus.componentId - 1;
43-
dispatch(snapShotAction({focusIndex: focusIndex, deepCopiedState: deepCopiedState}))
48+
const deepCopiedState = JSON.parse(JSON.stringify(state));
49+
const focusIndex = state.canvasFocus.componentId - 1;
50+
dispatch(
51+
snapShotAction({
52+
focusIndex: focusIndex,
53+
deepCopiedState: deepCopiedState
54+
})
55+
);
4456
};
4557

4658
// This hook will allow the user to drag items from the left panel on to the canvas
@@ -55,16 +67,16 @@ function Canvas(props): JSX.Element {
5567
return;
5668
}
5769
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
58-
if (item.newInstance && item.instanceType !== "Component") {
59-
dispatch(addChild({
60-
type: item.instanceType,
61-
typeId: item.instanceTypeId,
62-
childId: null,
63-
contextParam: contextParam
64-
65-
}))
66-
67-
} else if (item.newInstance && item.instanceType === "Component") {
70+
if (item.newInstance && item.instanceType !== 'Component') {
71+
dispatch(
72+
addChild({
73+
type: item.instanceType,
74+
typeId: item.instanceTypeId,
75+
childId: null,
76+
contextParam: contextParam
77+
})
78+
);
79+
} else if (item.newInstance && item.instanceType === 'Component') {
6880
let hasDiffParent = false;
6981
const components = state.components;
7082
let newChildName = '';
@@ -95,15 +107,17 @@ function Canvas(props): JSX.Element {
95107
}
96108
}
97109
// if (!hasDiffParent) {
98-
dispatch(addChild({
110+
dispatch(
111+
addChild({
99112
type: item.instanceType,
100113
typeId: item.instanceTypeId,
101114
childId: null,
102115
contextParam: contextParam
103-
}))
116+
})
117+
);
104118
}
105119
},
106-
collect: monitor => ({
120+
collect: (monitor) => ({
107121
isOver: !!monitor.isOver()
108122
})
109123
});
@@ -116,25 +130,34 @@ function Canvas(props): JSX.Element {
116130
border: '1px solid #FBFBF2',
117131
borderStyle: isOver ? 'dotted' : 'solid',
118132
aspectRatio: 'auto 774 / 1200',
119-
boxSizing: 'border-box',
133+
boxSizing: 'border-box'
120134
};
121135

122136
const darkCanvasStyle = {
123137
width: '100%',
124138
minHeight: '100%',
125139
backgroundColor: isOver ? '#4d4d4d' : '#21262c',
126140
border: '1px solid #FBFBF2',
127-
borderStyle: isOver ? 'dotted' : 'solid',
141+
borderStyle: isOver ? 'dotted' : 'solid'
128142
};
129143
// Combine the default styles of the canvas with the custom styles set by the user for that component
130144
// The render children function renders all direct children of a given component
131145
// Direct children are draggable/clickable
132146

133147
const canvasStyle = combineStyles(defaultCanvasStyle, currentComponent.style);
134-
const darkCombinedCanvasStyle = combineStyles(darkCanvasStyle, currentComponent.style);
148+
const darkCombinedCanvasStyle = combineStyles(
149+
darkCanvasStyle,
150+
currentComponent.style
151+
);
135152
return (
136-
<div className={'componentContainer'} ref={drop} style={!isDarkMode ? canvasStyle : darkCombinedCanvasStyle} onClick={onClickHandler}>
137-
{renderChildren(currentComponent.children)}
153+
<div
154+
className={'componentContainer'}
155+
ref={drop}
156+
data-testid="drop"
157+
style={!isDarkMode ? canvasStyle : darkCombinedCanvasStyle}
158+
onClick={onClickHandler}
159+
>
160+
{renderChildren(currentComponent.children)}
138161
</div>
139162
);
140163
}

app/src/containers/CustomizationPanel.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,12 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
535535
);
536536
}
537537
return (
538-
<div className="column right" id="rightContainer" style={style.style}>
538+
<div
539+
className="column right"
540+
id="rightContainer"
541+
style={style.style}
542+
data-testid="customization"
543+
>
539544
<ProjectManager />
540545
{/* -----------------------------MOVED PROJECT MANAGER-------------------------------------- */}
541546
<div className="rightPanelWrapper">

0 commit comments

Comments
 (0)