Skip to content

Commit 41bb8d3

Browse files
committed
added jest testing
1 parent 7d02dee commit 41bb8d3

File tree

6 files changed

+74
-78
lines changed

6 files changed

+74
-78
lines changed

__tests__/componentReducer.test.ts

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,12 @@
11
import reducer from '../app/src/reducers/componentReducer';
2-
// import { State, Action, Component, ChildElement } from '../app/src/interfaces/InterfacesNew';
2+
import { State, Action, Component, ChildElement } from '../app/src/interfaces/InterfacesNew';
33

44
import initialState from '../app/src/context/initialState';
55

6-
const path = require('path');
7-
const Application = require('spectron').Application;
8-
const baseDir = path.join(__dirname, '..');
9-
const electronPath = path.join(baseDir, 'node_modules', '.bin', 'electron');
106

11-
const appArg = path.join(baseDir, 'app', 'electron', 'main.js');
12-
//const appArg = path.join(baseDir, 'app', 'electron');
13-
console.log(electronPath);
14-
console.log(appArg);
15-
16-
//const electronPath = '../node_modules/.bin/electron';
17-
//const electronPath = require('electron');
18-
19-
const app = new Application({
20-
path: electronPath,
21-
args: [appArg],
22-
chromeDriverArgs: ['--no-proxy-server'],
23-
env: {
24-
'NO_PROXY': '127.0.0.1,localhost',
25-
'NODE_ENV': 'test'
26-
},
27-
waitTimeout: 10e3
28-
})
29-
30-
console.log(app);
31-
// const testComponent: Component = {
32-
// id: 2,
33-
// name: "Test",
34-
// nextChildId: 1,
35-
// style: {},
36-
// code: '',
37-
// children: []
38-
// }
39-
40-
describe('Testing componentReducer functionality', () => {
41-
let state = initialState;
42-
43-
beforeAll(() => {
44-
return app.start();
45-
})
46-
47-
afterAll(() => {
48-
if (app && app.isRunning()) {
49-
return app.stop();
50-
}
51-
})
7+
describe('Testing componentReducer functionality', function () {
8+
let state: State = initialState;
9+
5210

5311
// TEST 'ADD COMPONENT'
5412
describe('ADD COMPONENT reducer', () => {
@@ -80,21 +38,43 @@ describe('Testing componentReducer functionality', () => {
8038
childId: null
8139
}
8240
}
41+
// switch focus to very first root component
8342
state.canvasFocus = { componentId: 1, childId: null };
8443
console.log(state);
85-
const newState = reducer(state, action);
86-
const newParent = newState.components[0];
87-
// expect newParent's children array to have length 1
44+
state = reducer(state, action);
45+
const newParent = state.components[0];
46+
// expect new parent's children array to have length 1
8847
expect(newParent.children.length).toEqual(1);
89-
// expect child to have type 'Component' and name 'TestRegular'
48+
// expect new child to have type 'Component'
49+
console.log('new child ', newParent.children[0]);
9050
expect(newParent.children[0].type).toEqual('Component');
91-
expect(newParent.children[0].name).toEqual('TestRegular');
51+
const addedChild = state.components.find(comp => comp.id === newParent.children[0].typeId);
52+
// expect new child typeId to correspond to component with name 'TestRegular'
53+
expect(addedChild.name).toEqual('TestRegular');
9254
})
9355
})
56+
9457
// TEST 'CHANGE FOCUS'
58+
describe('CHANGE FOCUS reducer', () => {
59+
it('should change focus to specified component', () => {
60+
const action = {
61+
type: 'CHANGE FOCUS',
62+
payload: {
63+
componentId: 2,
64+
childId: null
65+
}
66+
}
67+
console.log('before change focus ', state.canvasFocus);
68+
state = reducer(state, action);
69+
console.log('after change focus ', state.canvasFocus);
70+
expect(state.canvasFocus.componentId).toEqual(2);
71+
expect(state.canvasFocus.childId).toEqual(null);
72+
})
73+
})
9574

9675
// TEST 'UPDATE CSS'
9776

98-
// TEST 'SET INITIAL STATE'
77+
78+
// TEST 'RESET STATE'
9979

10080
})

app/electron/main.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ const {
1010
// The splash screen is what appears while the app is loading
1111
const { initSplashScreen, OfficeTemplate } = require('electron-splashscreen');
1212
const { resolve } = require('app-root-path');
13-
const Protocol = require('./protocol');
14-
const MenuBuilder = require('./menu');
15-
const path = require('path');
1613

1714
const {
1815
default: installExtension,
@@ -197,18 +194,19 @@ app.on('ready', createWindow);
197194
app.on('window-all-closed', () => {
198195
// On macOS it is common for applications and their menu bar
199196
// to stay active until the user quits explicitly with Cmd + Q
200-
if (process.platform !== 'darwin') {
201-
app.quit();
202-
} else {
203-
ContextMenu.clearMainBindings(ipcMain);
204-
}
197+
// if (process.platform !== 'darwin') {
198+
// app.quit();
199+
// } else {
200+
// ContextMenu.clearMainBindings(ipcMain);
201+
// }
202+
app.quit();
205203
});
206204

207-
app.on('activate', () => {
205+
app.on('activate', async () => {
208206
// On macOS it's common to re-create a window in the app when the
209207
// dock icon is clicked and there are no other windows open.
210208
if (win === null) {
211-
createWindow();
209+
await createWindow();
212210
}
213211
});
214212

app/src/helperFunctions/generateCode.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { Component, State, ChildElement } from '../interfaces/InterfacesNew';
22
import HTMLTypes from '../context/HTMLTypes';
3+
const { format } = require('prettier');
4+
5+
declare global {
6+
interface Window {
7+
api: any;
8+
}
9+
}
310

411
// generate code based on the component heirarchy
512
const generateUnformattedCode = (
@@ -211,14 +218,20 @@ const generateUnformattedCode = (
211218

212219
// formats code with prettier linter
213220
const formatCode = (code: string) => {
214-
return window.api.formatCode(code);
215-
// return format(code, {
216-
// singleQuote: true,
217-
// trailingComma: 'es5',
218-
// bracketSpacing: true,
219-
// jsxBracketSameLine: true,
220-
// parser: 'babel'
221-
// });
221+
222+
// in test environment, window.api is not defined,
223+
// so we reference original prettier format function instead
224+
if (process.env.NODE_ENV === 'test') {
225+
return format(code, {
226+
singleQuote: true,
227+
trailingComma: 'es5',
228+
bracketSpacing: true,
229+
jsxBracketSameLine: true,
230+
parser: 'babel'
231+
});
232+
} else {
233+
return window.api.formatCode(code);
234+
}
222235
};
223236

224237
// generate code based on component heirarchy and then return the rendered code

app/src/reducers/componentReducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const reducer = (state: State, action: Action) => {
133133
type,
134134
typeId,
135135
childId
136-
}: { type: string; typeId: number; childId: number } = action.payload;
136+
}: { type: string; typeId: number; childId: any} = action.payload;
137137
// the parent of the new child is whichever component that is currently focused on
138138
const parentComponentId: number = state.canvasFocus.componentId;
139139

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
"dist-linux": "npm run prod-build && electron-builder --linux",
3636
"dist-windows": "npm run prod-build && electron-builder --windows",
3737
"dist-all": "npm run prod-build && electron-builder --mac --linux --windows",
38-
"test": "cross-env NODE_ENV=test NO_PROXY='127.0.0.1,localhost' jest --verbose",
39-
"mocha-test": "mocha"
38+
"test": "cross-env NODE_ENV=test jest --verbose"
4039
},
4140
"build": {
4241
"productName": "ReacType",
@@ -88,7 +87,9 @@
8887
"\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest",
8988
"^.+\\.js?$": "babel-jest"
9089
},
91-
"transformIgnorePatterns": ["<rootDir>/node_modules"],
90+
"transformIgnorePatterns": [
91+
"<rootDir>/node_modules"
92+
],
9293
"testRegex": "/__tests__/.*\\.(ts|tsx|js)$",
9394
"globals": {
9495
"ts-jest": {
@@ -173,7 +174,9 @@
173174
"@babel/preset-env": "^7.10.4",
174175
"@babel/preset-react": "^7.10.4",
175176
"@babel/preset-typescript": "^7.10.4",
177+
"@types/chai": "^4.2.11",
176178
"@types/jest": "^25.1.5",
179+
"@types/mocha": "^8.0.0",
177180
"awesome-typescript-loader": "^5.2.1",
178181
"babel-core": "^6.26.3",
179182
"babel-eslint": "^8.2.6",
@@ -184,6 +187,7 @@
184187
"babel-preset-env": "^1.6.1",
185188
"babel-preset-react": "^6.24.1",
186189
"babel-preset-stage-0": "^6.24.1",
190+
"chai": "^4.2.0",
187191
"clean-webpack-plugin": "^0.1.19",
188192
"concurrently": "^5.1.0",
189193
"copy-webpack-plugin": "^4.5.2",

tsconfig.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
"compilerOptions": {
33
"outDir": "./app/dist/",
44
"sourceMap": true,
5-
"noImplicitAny": true,
6-
"noImplicitThis": true,
7-
"strictNullChecks": true,
5+
"noImplicitAny": false,
6+
"noImplicitThis": false,
7+
"strictNullChecks": false,
88
"module": "commonjs",
99
"target": "es6",
1010
"jsx": "react",
1111
"strict": true,
1212
"lib": ["es6", "dom", "es2017"],
1313
"allowSyntheticDefaultImports": true,
1414
"strictFunctionTypes": false,
15-
"esModuleInterop": true
15+
"esModuleInterop": true,
16+
"typeRoots": ["node_modules/@types", "node_modules/@types/node"]
1617
},
1718
// only compile ts files in this directory
1819
"include": [".app/src/**/*"]

0 commit comments

Comments
 (0)