Skip to content

Commit 806c394

Browse files
authored
Merge pull request #58 from fredosauce/working
Set up Enzyme testing
2 parents 0be6ec7 + bff8ce4 commit 806c394

File tree

9 files changed

+81
-21
lines changed

9 files changed

+81
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Download for [MacOS](https://github.com/team-reactype/ReacType/releases), [Windo
2929

3030
- **Linux users**: run the application as a super user in order to read and write files.
3131

32-
![Gif of adding](https://i.imgur.com/hdVTFcP.gif)
32+
![Gif of adding](https://i.imgur.com/nOeuuU6.gif)
3333

3434
### How to use
3535

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Enzyme testing suite renders snapshots, too 1`] = `
4+
<div>
5+
<h1>
6+
Hello, Enzyme!
7+
</h1>
8+
</div>
9+
`;

__tests__/enzyme.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { shallow } from 'enzyme';
2+
import React from 'react';
3+
4+
describe('Enzyme testing suite', () => {
5+
6+
it('renders snapshots, too', () => {
7+
const wrapper = shallow(<div>
8+
<h1>Hello, Enzyme!</h1>
9+
</div>)
10+
expect(wrapper).toMatchSnapshot()
11+
})
12+
})

app/setupTests.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { configure } from 'enzyme'
2+
import Adapter from 'enzyme-adapter-react-16'
3+
4+
configure({
5+
adapter: new Adapter(),
6+
})

app/src/components/main/DirectChildComponent.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
5757
boxShadow:
5858
state.canvasFocus.childId === childId ? '1px 1px 3px rgb(11,212,112)' : ''
5959
};
60+
6061
const combinedStyle = combineStyles(
6162
combineStyles(
6263
combineStyles(globalDefaultStyle, referencedComponent.style),
@@ -109,7 +110,13 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
109110
const HTMLDefaultPlacholder = HTMLType.placeHolderShort;
110111
const combinedStyle = combineStyles(HTMLDefaultStyle, child.style);
111112
return (
112-
<React.Fragment>
113+
<React.Fragment
114+
key={
115+
'indChildFrag' +
116+
child.childId.toString() +
117+
child.typeId.toString()
118+
}
119+
>
113120
{child.children.length === 0 ? (
114121
<IndirectChild
115122
style={combinedStyle}
@@ -161,7 +168,7 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
161168
return (
162169
<div
163170
onClick={onClickHandler}
164-
key={'childComp' + childId}
171+
// key={'childComp' + childId}
165172
style={combinedStyle}
166173
ref={drag}
167174
>

app/src/helperFunctions/renderChildren.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const renderChildren = (children: ChildElement[]) => {
2121
type={type}
2222
typeId={typeId}
2323
style={style}
24-
key={childId.toString()}
24+
key={'DirChild' + childId.toString()}
2525
/>
2626
);
2727
}
@@ -33,7 +33,7 @@ const renderChildren = (children: ChildElement[]) => {
3333
type={type}
3434
typeId={typeId}
3535
style={style}
36-
key={childId.toString()}
36+
key={'DirChild' + childId.toString()}
3737
/>
3838
);
3939
}
@@ -46,7 +46,7 @@ const renderChildren = (children: ChildElement[]) => {
4646
typeId={typeId}
4747
style={style}
4848
children={children}
49-
key={childId.toString()}
49+
key={'DirChild' + childId.toString()}
5050
/>
5151
);
5252
}
@@ -60,7 +60,7 @@ const renderChildren = (children: ChildElement[]) => {
6060
typeId={typeId}
6161
style={style}
6262
children={children}
63-
key={childId.toString()}
63+
key={'DirChild' + childId.toString()}
6464
/>
6565
);
6666
}

app/src/reducers/componentReducer.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '../interfaces/Interfaces';
77
import initialState from '../context/initialState';
88
import generateCode from '../helperFunctions/generateCode';
9+
import cloneDeep from '../helperFunctions/cloneDeep';
910

1011
const reducer = (state: State, action: Action) => {
1112
// if the project type is set as Next.js, next component code should be generated
@@ -133,7 +134,7 @@ const reducer = (state: State, action: Action) => {
133134
type,
134135
typeId,
135136
childId
136-
}: { type: string; typeId: number; childId: any} = action.payload;
137+
}: { type: string; typeId: number; childId: any } = action.payload;
137138
// the parent of the new child is whichever component that is currently focused on
138139
const parentComponentId: number = state.canvasFocus.componentId;
139140

@@ -286,7 +287,14 @@ const reducer = (state: State, action: Action) => {
286287
}
287288

288289
case 'SET INITIAL STATE': {
289-
return { ...action.payload };
290+
// set the canvas focus to be the first component
291+
const canvasFocus = {
292+
...action.payload.canvasFocus,
293+
componentId: 1,
294+
childId: null
295+
};
296+
297+
return { ...action.payload, canvasFocus };
290298
}
291299
case 'SET PROJECT NAME': {
292300
return {
@@ -314,9 +322,32 @@ const reducer = (state: State, action: Action) => {
314322

315323
return { ...state, components, projectType };
316324
}
317-
// Reset state of a given project back to its initial state
325+
// Reset all component data back to their initial state but maintain the user's project name and log-in status
318326
case 'RESET STATE': {
319-
return { ...initialState };
327+
const nextChildId = 1;
328+
const rootComponents = [1];
329+
const nextComponentId = 2;
330+
const canvasFocus = {
331+
...state.canvasFocus,
332+
componentId: 1,
333+
childId: null
334+
};
335+
const rootComponent = {
336+
...state.components[0],
337+
code: '',
338+
children: [],
339+
style: {}
340+
};
341+
const components = [rootComponent];
342+
return {
343+
...state,
344+
nextChildId,
345+
rootComponents,
346+
nextComponentId,
347+
components,
348+
canvasFocus
349+
};
350+
// return { ...initialState };
320351
}
321352

322353
case 'UPDATE PROJECT NAME': {

app/src/setupTests.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
"diagnostics": false
7373
}
7474
},
75-
"testTimeout": 50000
75+
"setupFilesAfterEnv": ["<rootDir>/app/setupTests.ts"],
76+
"snapshotSerializers": ["enzyme-to-json/serializer"]
7677
},
7778
"bugs": {
7879
"url": "https://github.com/open-source-labs/ReacType/issues"
@@ -84,8 +85,6 @@
8485
"@material-ui/core": "^4.11.0",
8586
"@material-ui/icons": "^4.0.1",
8687
"@material-ui/styles": "^4.9.6",
87-
"@types/enzyme": "^3.10.5",
88-
"@types/enzyme-adapter-react-16": "^1.0.6",
8988
"@types/js-cookie": "^2.2.6",
9089
"@types/node": "^14.0.20",
9190
"@types/prettier": "^1.19.0",
@@ -125,6 +124,8 @@
125124
"@babel/preset-react": "^7.10.4",
126125
"@babel/preset-typescript": "^7.10.4",
127126
"@types/chai": "^4.2.11",
127+
"@types/enzyme": "^3.10.5",
128+
"@types/enzyme-adapter-react-16": "^1.0.6",
128129
"@types/jest": "^25.1.5",
129130
"babel-eslint": "^8.2.6",
130131
"babel-jest": "^25.2.4",
@@ -137,6 +138,7 @@
137138
"dotenv": "^8.2.0",
138139
"electron": "^9.1.0",
139140
"electron-builder": "^22.7.0",
141+
"enzyme-to-json": "^3.5.0",
140142
"eslint": "^4.19.1",
141143
"eslint-config-airbnb-base": "^13.0.0",
142144
"eslint-plugin-babel": "^5.3.1",

0 commit comments

Comments
 (0)