Skip to content

Commit 243890f

Browse files
TheZokeredgarmueller
authored andcommitted
Add tests
1 parent 30ffbd4 commit 243890f

22 files changed

+1454
-311
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ install:
1616
script:
1717
- npm run compile
1818
- npm run lint
19+
- npm run test
1920
# - npm run coveralls

generator-jsonforms/package-lock.json

+592-129
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generator-jsonforms/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"@types/yeoman-generator": "^3.1.1",
2323
"chalk": "^2.4.2",
2424
"clear": "^0.1.0",
25-
"fs-extra": "^7.0.1",
2625
"figlet": "^1.2.1",
26+
"fs-extra": "^7.0.1",
2727
"jsonforms-react-seed": "git+https://github.com/eclipsesource/jsonforms-react-seed.git",
2828
"jsonforms-scaffolding-project": "git+https://github.com/eclipsesource/jsonforms-scaffolding-project.git",
2929
"make-it-happen-react": "git+https://github.com/eclipsesource/make-it-happen-react.git",
@@ -37,6 +37,8 @@
3737
"devDependencies": {
3838
"@types/figlet": "^1.2.0",
3939
"@types/fs-extra": "^5.0.5",
40-
"typescript": "^3.4.2"
40+
"typescript": "^3.4.2",
41+
"yeoman-assert": "^3.1.1",
42+
"yeoman-test": "^1.9.1"
4143
}
4244
}

generator-jsonforms/src/generators/app/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import { promisify } from 'util';
1212
const clear = require('clear');
1313
const validate = require('validate-npm-package-name');
1414

15-
enum ProjectRepo {
15+
export enum ProjectRepo {
1616
Example = 'make-it-happen-react',
1717
Seed = 'jsonforms-react-seed',
1818
Scaffolding = 'jsonforms-scaffolding-project',
1919
}
2020

21-
enum Project {
21+
export enum Project {
2222
Example = 'example',
2323
Seed = 'seed',
2424
Scaffolding = 'scaffolding',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// tslint:disable:no-var-requires
2+
// tslint:disable:no-require-imports
3+
4+
/*import { join } from 'path';
5+
import { Project } from '../app/index';
6+
7+
const helpers = require('yeoman-test');
8+
const assert = require('yeoman-assert');*/
9+
10+
describe('Generators package tests', () => {
11+
// jest.setTimeout(500000);
12+
13+
test('test to have one test', () => {
14+
expect(true).toBe(true);
15+
});
16+
17+
/*test('Generator with passed options', () => {
18+
const path = './';
19+
const options = {
20+
project: Project.Seed,
21+
path,
22+
name: 'test-project',
23+
schemaPath: '',
24+
skipPrompting: true,
25+
};
26+
return helpers.run(join(__dirname, '../app'))
27+
.withOptions( options )
28+
.withPrompts( {} )
29+
.then((dir: any) => {
30+
assert.file(join(dir, 'package.json'));
31+
});
32+
});
33+
34+
test('Generator without passed options and prompting', async () => {
35+
const path = './';
36+
const prompt = {
37+
project: ProjectRepo.Seed,
38+
path,
39+
name: 'test-project-2',
40+
schemaPath: '',
41+
skipPrompting: false,
42+
};
43+
return await helpers.run(join(__dirname, '../app'))
44+
.withOptions( {} )
45+
.withPrompts( prompt )
46+
.withLocalConfig({ lang: 'en' })
47+
.then((dir: any) => {
48+
assert.file(join(dir, 'package.json'));
49+
});
50+
});
51+
*/
52+
});

jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
4+
globals: {
5+
'ts-jest': {
6+
tsConfig: 'tsconfig.base.json'
7+
}
8+
}
49
};

jsonforms-tooling-common/package-lock.json

+46-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonforms-tooling-common/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
"react-redux": "^6.0.0",
2525
"redux": "^3.0.0",
2626
"util": "^0.11.1",
27+
"rimraf": "^2.6.2",
2728
"yeoman-environment": "^2.3.4"
2829
},
2930
"type-check": "tsc",
3031
"devDependencies": {
3132
"@types/chokidar": "^2.1.3",
32-
"rimraf": "^2.6.2"
33+
"@types/rimraf": "2.0.2"
3334
}
3435
}

jsonforms-tooling-common/src/generate.ts

+40-19
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// tslint:disable:no-use-before-declare
22

33
import { generateDefaultUISchema } from '@jsonforms/core';
4-
import { sep } from 'path';
4+
import { dirname, join } from 'path';
55
import { existsSync } from 'fs';
66

7-
import { MessageType, readFileWithPromise, showMessage, writeFileWithPromise } from './utils';
7+
import { MessageType, readFileWithPromise, showMessage, validateUiSchema, writeFileWithPromise } from './utils';
88

99
/**
1010
* Generates the default UI Schema from a json schema
1111
* @param {any} editorInstance the instance of the editor
12-
* @param {string} path the path to the schema file
12+
* @param {any} path the path to the schema file
1313
*/
14-
export const generateUISchema = async (editorInstance: any, path: string) => {
14+
export const generateUISchema = async (editorInstance: any, path: any) => {
1515
if (!path) {
1616
let fileUri = null;
1717
try {
@@ -23,23 +23,47 @@ export const generateUISchema = async (editorInstance: any, path: string) => {
2323
filters: {
2424
'Json Files': ['json'],
2525
},
26+
id: 'selectSchema',
2627
});
2728
if (fileUri && fileUri[0].fsPath) {
2829
path = fileUri[0].fsPath;
2930
} else {
30-
showMessage(editorInstance, 'Please select a json schema file', MessageType.Error);
31-
return;
31+
throw new Error('Please select a json schema file');
3232
}
3333
} catch (err) {
34-
return;
34+
showMessage(editorInstance, err.message, MessageType.Error);
35+
return err;
3536
}
3637
}
3738

39+
// Read JSON Schema file
40+
let jsonContent = null;
41+
try {
42+
const content = await readFileWithPromise(path, 'utf8');
43+
jsonContent = JSON.parse(content);
44+
} catch (err) {
45+
showMessage(editorInstance, err.message, MessageType.Error);
46+
return err;
47+
}
48+
49+
// Check if file is already a uischema file and throw an error
50+
try {
51+
const validUiSchema = await validateUiSchema(jsonContent);
52+
if (validUiSchema) {
53+
throw new Error('It seems you selected a uischema. This functions does only work with a'
54+
+ ' schema file');
55+
}
56+
} catch (err) {
57+
showMessage(editorInstance, err.message, MessageType.Error);
58+
return err;
59+
}
60+
3861
let fileName = '';
3962
try {
4063
fileName = await editorInstance.window.showInputBox(editorInstance.InputBoxOptions = {
4164
prompt: 'Label: ',
4265
placeHolder: 'Enter a filename for your UI Schema (default: uischema.json)',
66+
id: 'fileName',
4367
});
4468
if (fileName === undefined) {
4569
throw new Error('UI schema generation canceled');
@@ -49,46 +73,43 @@ export const generateUISchema = async (editorInstance: any, path: string) => {
4973
}
5074
} catch (err) {
5175
showMessage(editorInstance, err.message, MessageType.Error);
52-
return;
76+
return err;
5377
}
5478

5579
// Check if file already exist and ask user if it should be overwritten
56-
const newPath = path.substring(0, path.lastIndexOf(sep)) + sep + fileName;
80+
const newPath = join(dirname(path), fileName);
5781
if (existsSync(newPath)) {
5882
let decision = 'No';
5983
try {
6084
decision = await editorInstance.window.showQuickPick(['Yes', 'No'], editorInstance.QuickPickOptions = {
6185
canSelectMany: false,
62-
placeHolder: `This file ${fileName} does already exist. Should it be overwritten?`
86+
placeHolder: `This file ${fileName} does already exist. Should it be overwritten?`,
87+
id: 'overwrite',
6388
});
6489
if (decision !== 'Yes') {
6590
throw new Error('UI schema generation canceled');
6691
}
6792
} catch (err) {
6893
showMessage(editorInstance, err.message, MessageType.Error);
69-
return;
94+
return err;
7095
}
7196
}
7297

73-
// Read JSON Schema file
74-
let jsonContent = null;
98+
// Generate the default UI schema
99+
let jsonUISchema = null;
75100
try {
76-
const content = await readFileWithPromise(path, 'utf8');
77-
jsonContent = JSON.parse(content);
101+
jsonUISchema = await generateDefaultUISchema(jsonContent);
78102
} catch (err) {
79103
showMessage(editorInstance, err.message, MessageType.Error);
80104
return;
81105
}
82106

83-
// Generate the default UI schema
84-
const jsonUISchema = generateDefaultUISchema(jsonContent);
85-
86107
// Write UI Schema file
87108
try {
88109
await writeFileWithPromise(newPath, JSON.stringify(jsonUISchema, null, 2));
89110
} catch (err) {
90111
showMessage(editorInstance, err.message, MessageType.Error);
91-
return;
112+
return err;
92113
}
93114
showMessage(editorInstance, 'Successfully generated UI schema');
94115
return jsonUISchema;

jsonforms-tooling-common/src/metaSchema.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const uiMetaSchema: JsonSchema7 = {
8181
'default': 'Control'
8282
},
8383
'label': {
84-
'type': 'string'
84+
'type': ['string', 'boolean']
8585
},
8686
'scope': {
8787
'$ref': '#/definitions/scope'

0 commit comments

Comments
 (0)