Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vyacheslav-pushkin committed Oct 31, 2019
1 parent 6361a13 commit a56785e
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Expand Up @@ -4,5 +4,5 @@ test/e2e/generated
src/test/generated
lib
node_modules
./coverage
./.nyc_output
coverage
.nyc_output
14 changes: 7 additions & 7 deletions src/common/model/cuba-model.ts
Expand Up @@ -25,22 +25,22 @@ export interface Entity {
name?: string;
className: string;
packageName: string;
dataStore: string,
table: string,
dataStore?: string,
table?: string,
parentClassName: string,
discriminator: string,
discriminator?: string,
updatable: boolean,
creatable: boolean,
hasUuid: boolean,
softDelete: boolean,
versioned: boolean,
reference: string,
reference?: string,
parentPackage: string,
embeddable: boolean,
persistentEntity: true,
persistentEntity: boolean,
replaceParent: boolean,
systemLevel: boolean,
namePattern: string,
namePattern?: string,
mappedSuperclass: boolean,
fqn: string,
imported: boolean,
Expand Down Expand Up @@ -161,4 +161,4 @@ export function getEntitiesArray(entities: Entity[] | { [entityName: string]: En
return Array.isArray(entities)
? entities
: Object.keys(entities).map(k => (entities as { [entityName: string]: Entity }) [k]);
}
}
3 changes: 2 additions & 1 deletion src/common/questions.ts
Expand Up @@ -2,7 +2,8 @@ import {EntityInfo, StudioTemplateProperty, StudioTemplatePropertyType, ViewInfo
import {Answers, Question as YeomanQuestion} from "yeoman-generator";
import {getEntitiesArray, ProjectModel} from './model/cuba-model';

type Choice = string | {name: string, value: any, short?: string};
export type ObjectChoice = {name: string, value: any, short?: string};
export type Choice = string | ObjectChoice;

interface Question extends YeomanQuestion {
choices?: Choice[] | ((previousAnswers: Answers) => Choice[]), // Property is missing in yeoman typings
Expand Down
31 changes: 23 additions & 8 deletions src/generators/react-typescript/common/i18n.ts
Expand Up @@ -13,16 +13,31 @@ export function writeI18nMessages(
const existingMessagesEn = fs.readJSON(i18nMessagesPathEn);
const existingMessagesRu = fs.readJSON(i18nMessagesPathRu);

const resultMessagesEn = {
...enJson,
const {enOut, ruOut} = mergeI18nMessages(existingMessagesEn, enJson, existingMessagesRu, ruJson, className);

fs.writeJSON(i18nMessagesPathEn, enOut);
fs.writeJSON(i18nMessagesPathRu, ruOut);
}

export function mergeI18nMessages(
enExisting: Record<string, string>,
enTemplate: Record<string, string>,
ruExisting: Record<string, string>,
ruTemplate: Record<string, string>,
className: string
): {
enOut: Record<string, string>, ruOut: Record<string, string>
} {
const enOut = {
...enTemplate,
[`router.${className}`]: className,
...existingMessagesEn
...enExisting
};
const resultMessagesRu = {
...ruJson,
...existingMessagesRu

const ruOut = {
...ruTemplate,
...ruExisting
};

fs.writeJSON(i18nMessagesPathEn, resultMessagesEn);
fs.writeJSON(i18nMessagesPathRu, resultMessagesRu);
return {enOut, ruOut};
}
49 changes: 49 additions & 0 deletions src/test/common/questions.test.ts
@@ -0,0 +1,49 @@
import {fromStudioProperty, ObjectChoice} from "../../common/questions";
import * as projectModel from '../fixtures/mpg-projectModel.json';
import {StudioTemplatePropertyType} from '../../common/studio/studio-model';
import { expect } from "chai";

describe('interactive CLI question helpers', () => {
it('correctly creates choices for an Entity', () => {
const prop = {
code: 'test',
caption: 'test',
propertyType: StudioTemplatePropertyType.ENTITY,
};

// @ts-ignore TODO VP Replace enums with string union types in ProjectModel https://github.com/cuba-platform/front-generator/issues/46
const question = fromStudioProperty(prop, projectModel);

expect(question.choices).to.exist;
expect(question.choices).to.be.an('Array');

const expectedEntityNames = ["MpgUserInfo", "mpg$SparePart", "mpg$Car", "mpg$FavoriteCar", "mpg$TechnicalCertificate",
"mpg$Garage", "mpg$CarRent"];
const actualEntityNames = (question.choices! as ObjectChoice[]).map((choice: ObjectChoice) => choice.name);
expect(actualEntityNames).to.deep.equal(expectedEntityNames);
});

it('correctly creates choices for a View', () => {
const prop = {
code: 'test',
caption: 'test',
propertyType: StudioTemplatePropertyType.VIEW,
};

const previousAnswers = {
entity: {
name: 'mpg$FavoriteCar'
}
};

// @ts-ignore TODO VP Replace enums with string union types in ProjectModel https://github.com/cuba-platform/front-generator/issues/46
const question = fromStudioProperty(prop, projectModel);

expect(question.choices).to.exist;
expect(question.choices).to.be.a('Function');

const expectedViewNames = ["_minimal", "_local", "_base", "favoriteCar-view", "favoriteCar-edit"];
const actualViewNames = (question.choices as Function)(previousAnswers).map((choice: ObjectChoice) => choice.name);
expect(actualViewNames).to.deep.equal(expectedViewNames);
});
});
14 changes: 12 additions & 2 deletions src/test/fixtures/mpg-projectModel.json
Expand Up @@ -3,7 +3,17 @@
"name": "model-playground",
"namespace": "mpg",
"modulePrefix": "app",
"modelPrefix": "app"
"modelPrefix": "app",
"locales": [
{
"code": "en",
"caption": "English"
},
{
"code": "ru",
"caption": "Russian"
}
]
},
"entities": [
{
Expand Down Expand Up @@ -20439,4 +20449,4 @@
]
}
]
}
}
59 changes: 59 additions & 0 deletions src/test/generators/react-typrscript/common/i18n.test.ts
@@ -0,0 +1,59 @@
import {mergeI18nMessages} from '../../../../generators/react-typescript/common/i18n';
import { expect } from 'chai';

describe('react generator helpers', () => {
const enExisting = {
'key1': 'predefined value 1',
'key3': 'predefined value 3',
'keyNotInTemplate': 'key not in template'
};

const enTemplate = {
'key1': 'template value 1',
'key2': 'template value 2',
'key3': 'template value 3',
'key4': 'template value 4',
'key5': 'template value 5',
};

const ruExisting = {
'key1': 'заранее заданное значение 1',
'key3': 'заранее заданное значение 3',
'key4': 'заранее заданное значение 4',
'keyNotInTemplate': 'ключ, отсутствующий в шаблоне'
};

const ruTemplate = {
'key1': 'значение из шаблона 1',
'key2': 'значение из шаблона 2',
'key3': 'значение из шаблона 3',
'key4': 'значение из шаблона 4',
'key5': 'значение из шаблона 5',
};

const enExpected = {
'key1': 'predefined value 1',
'key2': 'template value 2',
'key3': 'predefined value 3',
'key4': 'template value 4',
'key5': 'template value 5',
'router.testClass': 'testClass',
'keyNotInTemplate': 'key not in template'
};

const ruExpected = {
'key1': 'заранее заданное значение 1',
'key2': 'значение из шаблона 2',
'key3': 'заранее заданное значение 3',
'key4': 'заранее заданное значение 4',
'key5': 'значение из шаблона 5',
'keyNotInTemplate': 'ключ, отсутствующий в шаблоне'
};

it('should add i18n keys without overwriting', () => {
const {enOut, ruOut} = mergeI18nMessages(enExisting, enTemplate, ruExisting, ruTemplate, 'testClass');

expect(enOut).to.deep.equal(enExpected);
expect(ruOut).to.deep.equal(ruExpected);
});
});

0 comments on commit a56785e

Please sign in to comment.