Skip to content

Commit cc117fb

Browse files
committed
finished setting up test connection files for classic react apps, just need test files, Philip, https://github.com/pmhua, Daniel https://github.com/dreille, William https://github.com/wbrittwage, Miles https://github.com/Miles818
1 parent f901709 commit cc117fb

File tree

4 files changed

+202
-20
lines changed

4 files changed

+202
-20
lines changed

app/src/helperFunctions/generateCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ const generateUnformattedCode = (
206206
${
207207
classBased
208208
? `class ${currentComponent.name} extends Component {`
209-
: `const ${currentComponent.name} = (props): JSX.Element => {`
209+
: `const ${currentComponent.name} = (props: any): JSX.Element => {`
210210
}
211211
212212
${

app/src/utils/createApplication.util.ts

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import createFiles from './createFiles.util';
44
import { Component} from '../interfaces/Interfaces';
55

6+
import createTestSuiteClassic from './createTestSuiteClassic.util'
7+
68
const camelToKebab= (camel:string) => {
79
return camel.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase();
810
};
@@ -108,8 +110,19 @@ export const createDefaultCSS = (path, appName, components) => {
108110
});
109111
};
110112

111-
export const createPackage = (path, appName) => {
113+
export const createPackage = (path, appName, test) => {
112114
const filePath = `${path}/${appName}/package.json`;
115+
let tsjest = `,
116+
"@types/enzyme": "^3.10.9",
117+
"@types/jest": "^27.0.1",
118+
"babel-jest": "^27.2.0",
119+
"enzyme": "^3.11.0",
120+
"enzyme-adapter-react-16": "^1.15.6",
121+
"jest": "^27.2.0",
122+
"@types/enzyme-adapter-react-16": "^1.0.6",
123+
"ts-jest": "^27.0.5",
124+
"enzyme-to-json": "^3.6.2"`;
125+
113126
const data = `
114127
{
115128
"name": "reactype",
@@ -119,7 +132,9 @@ export const createPackage = (path, appName) => {
119132
"scripts": {
120133
"start": "node server/server.js",
121134
"build": "cross-env NODE_ENV=production webpack",
122-
"dev": "cross-env NODE_ENV=development webpack-dev-server"
135+
"dev": "cross-env NODE_ENV=development webpack-dev-server"${
136+
test ? `,
137+
"test": "jest"`: '' }
123138
},
124139
"nodemonConfig": {
125140
"ignore": [
@@ -160,7 +175,8 @@ export const createPackage = (path, appName) => {
160175
"typescript": "^3.8.3",
161176
"webpack": "^4.29.6",
162177
"webpack-cli": "^3.3.0",
163-
"webpack-dev-server": "^3.2.1"
178+
"webpack-dev-server": "^3.2.1"${
179+
test ? tsjest : '' }
164180
}
165181
}
166182
`;
@@ -252,18 +268,20 @@ export const createBabel = (path, appName) => {
252268
export const createTsConfig = (path, appName) => {
253269
const filePath = `${path}/${appName}/tsconfig.json`;
254270
const data = `
255-
{
256-
"compilerOptions": {
257-
"outDir": "./dist/",
258-
"sourceMap": true,
259-
"noImplicitAny": true,
260-
"module": "commonjs",
261-
"target": "es6",
262-
"jsx": "react",
263-
"allowSyntheticDefaultImports": true
264-
},
265-
"include": ["./src/**/*"]
266-
}
271+
{
272+
"compilerOptions": {
273+
"outDir": "./dist/",
274+
"sourceMap": true,
275+
"noImplicitAny": true,
276+
"module": "commonjs",
277+
"target": "es6",
278+
"jsx": "react",
279+
"lib": ["dom", "es6"],
280+
"moduleResolution": "node",
281+
"esModuleInterop": true
282+
},
283+
"include": ["./src/**/*"]
284+
}
267285
`;
268286
window.api.writeFile(filePath, data, err => {
269287
if (err) {
@@ -340,23 +358,29 @@ app.listen(8080, () => {
340358
async function createApplicationUtil({
341359
path,
342360
appName,
343-
components
361+
components,
362+
testchecked,
344363
}: {
345364
path: string;
346365
appName: string;
347366
components: Component[];
367+
testchecked: boolean;
348368
}) {
349369
console.log('in the createApplication util');
350-
370+
console.log('testchecked: ',testchecked);
351371
await createIndexHtml(path, appName);
352372
await createIndexTsx(path, appName);
353373
await createDefaultCSS(path, appName, components);
354-
await createPackage(path, appName);
374+
await createPackage(path, appName, testchecked);
355375
await createWebpack(path, appName);
356376
await createBabel(path, appName);
357377
await createTsConfig(path, appName);
358378
await createTsLint(path, appName);
359379
await createServer(path, appName);
380+
if (testchecked) {
381+
console.log('entering createTestSuiteClassic');
382+
await createTestSuiteClassic({path, appName, components, testchecked});
383+
}
360384
await createFiles(components, path, appName, true);
361385
}
362386
export default createApplicationUtil;
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
2+
3+
4+
const initFolders = (path: string, appName: string) => {
5+
console.log(window); // remember to delete
6+
let dir = path;
7+
dir = `${dir}/${appName}`;
8+
if (!window.api.existsSync(`${dir}/__tests__`)) {
9+
window.api.mkdirSync(`${dir}/__tests__`);
10+
}
11+
}
12+
13+
const createJestConfigFile = (path: string, appName: string) => {
14+
const filePath:string = `${path}/${appName}/jest.config.js`;
15+
const data:String= `
16+
module.exports = {
17+
snapshotSerializers: ["enzyme-to-json/serializer"],
18+
transform: {
19+
"^.+\\.tsx?$": "ts-jest",
20+
"^.+\\.jsx?$": "<rootDir>/jest-preprocess.js",
21+
},
22+
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.([tj]sx?)$",
23+
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
24+
testPathIgnorePatterns: ["node_moules", ".cache"],
25+
globals: {
26+
__PATH_PREFIX__: "",
27+
}
28+
} `
29+
30+
31+
window.api.writeFile(filePath, data, err => {
32+
if (err) {
33+
console.log('createTestSuiteClassic.util createJestConfigFile error:', err.message);
34+
} else {
35+
console.log('createTestSuiteClassic.util createJestConfigFile written successfully');
36+
}
37+
});
38+
};
39+
40+
const createJestPreprocessFile = (path: string, appName: string) => {
41+
const filePath: string = `${path}/${appName}/jest-preprocess.js`;
42+
const data: string = `
43+
module.exports = require("babel-jest")`;
44+
45+
window.api.writeFile(filePath, data, err => {
46+
if (err) {
47+
console.log('createTestSuite.util createJestPreprocessFile error:', err.message);
48+
} else {
49+
console.log('createTestSuit.util createJestPreprocessFile written successfully');
50+
}
51+
});
52+
}
53+
54+
async function createComponentTests(path: string, appName: string, components: Component[]) {
55+
const filePath: string = `${path}/${appName}/__tests__/test.tsx`;
56+
console.log(JSON.stringify(components))
57+
console.log(components);
58+
59+
let data:string = `
60+
import { shallow } from 'enzyme'
61+
import React from 'react';
62+
63+
import * as Enzyme from 'enzyme'
64+
import Adapter from 'enzyme-adapter-react-16'
65+
66+
Enzyme.configure({
67+
adapter: new Adapter(),
68+
})
69+
`;
70+
71+
components.forEach(page => {
72+
73+
let importString = `
74+
import ${capitalize(page.name)} from "../src/components/${page.name}";`;
75+
data = data + importString;
76+
})
77+
78+
//let describe = `describe("${page.name}", () => {`
79+
components.forEach(page => {
80+
data = data + `
81+
82+
describe("${capitalize(page.name)}", () => {`
83+
84+
85+
data = data + `
86+
it('renders snapshots, too', () => {
87+
const wrapper = shallow(< ${capitalize(page.name)} />)
88+
expect(wrapper).toMatchSnapshot()
89+
})`
90+
91+
92+
data = data + `
93+
});`
94+
})
95+
96+
97+
98+
window.api.writeFile(filePath, data, err => {
99+
if (err) {
100+
console.log('createTestSuite.util createComponentTests error:', err.message);
101+
} else {
102+
console.log('createTestSuit.util createComponentTests written successfully');
103+
}
104+
});
105+
}
106+
107+
const capitalize = (string: string) => {
108+
return string.charAt(0).toUpperCase() + string.slice(1);
109+
}
110+
111+
// //////////////////////////
112+
// // DELETE BELOW AFTERWARDS
113+
// import { shallow } from 'enzyme'
114+
// import React from 'react';
115+
116+
// import * as Enzyme from 'enzyme'
117+
// import Adapter from 'enzyme-adapter-react-16'
118+
119+
// Enzyme.configure({
120+
// adapter: new Adapter(),
121+
// })
122+
123+
// describe('Hello, Enzyme!', () => {
124+
// it('renders', () => {
125+
// const wrapper = shallow(<div>
126+
// <h1>Hello, Enzyme!</h1>
127+
// </div>)
128+
// expect(wrapper.find('h1').html()).toMatch(/Hello, Enzyme/)
129+
// })
130+
131+
// it('renders snapshots, too', () => {
132+
// const wrapper = shallow(< />)
133+
// expect(wrapper).toMatchSnapshot()
134+
// })
135+
// })
136+
// // DELETE ABOVE AFTERWARDS
137+
// //////////////////////////
138+
async function createTestSuiteClassic({
139+
path,
140+
appName,
141+
components,
142+
testchecked,
143+
}: {
144+
path: string;
145+
appName: string;
146+
components: Component[];
147+
testchecked: boolean;
148+
}) {
149+
console.log('in the createClassicApplication util');
150+
console.log('testchecked: ', testchecked);
151+
152+
await initFolders(path, appName);
153+
await createJestConfigFile(path, appName);
154+
await createJestPreprocessFile(path, appName);
155+
await createComponentTests(path, appName, components);
156+
}
157+
158+
export default createTestSuiteClassic;

app/src/utils/exportProject.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const exportProject = (
1515
) => {
1616
// Create fully functional classic react application
1717
if (genOption === 1 && projectType === 'Classic React') {
18-
createApplicationUtil({ path, appName, components }).catch(err =>
18+
createApplicationUtil({ path, appName, components, testchecked: tests }).catch(err =>
1919
console.log(err)
2020
);
2121
} // export all component files, but don't create all application files

0 commit comments

Comments
 (0)