Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify jdl test without external fixture #26135

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions jdl/__test-files__/json_to_jdl_converter/only_app/.yo-rc.json

This file was deleted.

78 changes: 59 additions & 19 deletions jdl/converters/json-to-jdl-converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
*/
/* eslint-disable no-unused-expressions */

import fs from 'fs';
import fs, { readFileSync } from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import { it, describe, expect as jestExpect, beforeEach } from 'esmocha';
import { expect } from 'chai';
import { convertToJDL, convertSingleContentToJDL } from '../converters/json-to-jdl-converter.js';
import { basicHelpers as helpers } from '../../testing/index.js';
import { createJHipsterConfigFiles, basicHelpers as helpers } from '../../testing/index.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand All @@ -36,16 +36,48 @@ describe('jdl - JSONToJDLConverter', () => {

describe('convertToJDL', () => {
describe('when there is a yo-rc file in the passed directory', () => {
let dir;
let jdlFilename;
let jdlFileContent;

describe('without entities', () => {
beforeEach(() => {
dir = path.join(__dirname, '..', '__test-files__', 'json_to_jdl_converter', 'only_app');
jdlFilename = 'app.jdl';
convertToJDL(dir);
jdlFileContent = fs.readFileSync(path.join(dir, jdlFilename), 'utf-8');
beforeEach(async () => {
await helpers
.prepareTemporaryDir()
.withFiles(
createJHipsterConfigFiles({
jhipsterVersion: '6.0.1',
applicationType: 'microservice',
baseName: 'truc',
blueprints: [{ name: 'generator-jhipster-vuejs' }, { name: 'generator-jhipster-dotnetcore' }],
packageName: 'com.mycompany.myapp',
packageFolder: 'com/mycompany/myapp',
serverPort: '8081',
authenticationType: 'jwt',
cacheProvider: 'hazelcast',
enableHibernateCache: true,
websocket: 'no',
databaseType: 'sql',
devDatabaseType: 'h2Disk',
prodDatabaseType: 'mysql',
searchEngine: 'no',
messageBroker: 'no',
serviceDiscoveryType: 'eureka',
buildTool: 'maven',
enableSwaggerCodegen: false,
jwtSecretKey: 'HIDDEN',
testFrameworks: [],
jhiPrefix: 'jhi',
entitySuffix: '',
dtoSuffix: 'DTO',
enableTranslation: false,
clientPackageManager: 'npm',
skipClient: true,
nativeLanguage: 'en',
skipUserManagement: true,
}),
)
.commitFiles();
convertToJDL();
jdlFileContent = fs.readFileSync('app.jdl', 'utf-8');
});

it('should write a JDL file with the application', () => {
Expand Down Expand Up @@ -87,10 +119,9 @@ describe('jdl - JSONToJDLConverter', () => {
});
describe('with entities', () => {
beforeEach(() => {
dir = path.join(__dirname, '..', '__test-files__', 'json_to_jdl_converter', 'app_with_entities');
jdlFilename = 'app.jdl';
const dir = path.join(__dirname, '..', '__test-files__', 'json_to_jdl_converter', 'app_with_entities');
convertToJDL(dir);
jdlFileContent = fs.readFileSync(path.join(dir, jdlFilename), 'utf-8');
jdlFileContent = fs.readFileSync(path.join(dir, 'app.jdl'), 'utf-8');
});

it('should export apps & entities', () => {
Expand Down Expand Up @@ -334,17 +365,26 @@ describe('jdl - JSONToJDLConverter', () => {
});
});
describe('when passing an output file', () => {
let dir;
let output;
let file;

beforeEach(() => {
dir = path.join(__dirname, '..', '__test-files__', 'json_to_jdl_converter', 'only_app');
output = path.resolve('exported.jdl');
convertToJDL(dir, output);
beforeEach(async () => {
file = 'exported.jdl';
await helpers
.prepareTemporaryDir()
.withFiles(createJHipsterConfigFiles({ baseName: 'jhipster' }))
.commitFiles();
convertToJDL('.', file);
});

it('should output it to the output file', () => {
expect(fs.readFileSync(output, 'utf-8')).not.to.be.null;
jestExpect(readFileSync(file, 'utf-8')).toMatchInlineSnapshot(`
"application {
config {
baseName jhipster
}
}
"
`);
});
});
});
Expand Down
6 changes: 5 additions & 1 deletion testing/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { createJHipsterLogger, normalizePathEnd, parseCreationTimestamp } from '
import BaseGenerator from '../generators/base/index.js';
import type { JHipsterGeneratorOptions } from '../generators/base/api.js';
import { getPackageRoot, isDistFolder } from '../lib/index.js';
import type { JSONEntity } from '../jdl/converters/types.js';

type BaseEntity = any;
type BaseEntity = { name: string } & JSONEntity;
type GeneratorTestType = YeomanGenerator<JHipsterGeneratorOptions>;
type GeneratorTestOptions = JHipsterGeneratorOptions;

Expand Down Expand Up @@ -80,6 +81,9 @@ const createFiles = (workspaceFolder: string, configuration: Record<string, unkn
};
};

export const createJHipsterConfigFiles = (configuration: Record<string, unknown>, entities?: BaseEntity[]) =>
createFiles('', configuration, entities);

export type FakeBlueprintOptions = {
packageJson?: any;
generator?: string | string[];
Expand Down
Loading