Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Dec 3, 2021
1 parent 14b547a commit 456d081
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
// Module file extensions for importing
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],

testTimeout: 10000,
testTimeout: 20000,

setupFiles: ["./tests/jest.setup.ts"],
globals: {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/generator.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { GeneratorService } from '../services/generator.service';
import { prepareParserConfig } from '../utils/parser';

/**
* Controller which implements the Generator functionality
* Controller which exposes the Generator functionality
*/
export class GeneratorController implements Controller {
public path = '/generator';
Expand All @@ -18,7 +18,7 @@ export class GeneratorController implements Controller {
private generatorService = new GeneratorService();

private async generate(req: Request, res: Response) {
const zip = this.archiverService.create(res);
const zip = this.archiverService.createZip(res);
this.archiverService.appendHeaders(res);

let tmpDir: string;
Expand Down
2 changes: 1 addition & 1 deletion src/services/archiver.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { retrieveLangauge } from "../utils/retrieve-language";
import { createTempDirectory, removeTempDirectory } from "../utils/temp-dir";

export class ArchiverService {
public create(res?: Response) {
public createZip(res?: Response) {
const zip = archiver('zip', { zlib: { level: 9 } });
res && zip.pipe(res);
return zip;
Expand Down
9 changes: 7 additions & 2 deletions src/services/generator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { prepareParserConfig } from "../utils/parser";

export class GeneratorService {
public async generate(
asyncapi: AsyncAPIDocument,
asyncapi: AsyncAPIDocument | string,
template: string,
parameters: Record<string, any>,
destDir: string,
Expand All @@ -15,6 +15,11 @@ export class GeneratorService {
forceWrite: true,
templateParams: parameters,
});
await generator.generate(asyncapi, parserOptions);

if (typeof asyncapi === 'string') {
await generator.generateFromString(asyncapi, parserOptions);
} else {
await generator.generate(asyncapi, parserOptions);
}
}
}
43 changes: 43 additions & 0 deletions src/services/tests/generator.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fs from "fs";
import path from "path";

import { GeneratorService } from "../generator.service";
import { createTempDirectory, removeTempDirectory } from '../../utils/temp-dir';
import { prepareParserConfig } from "../../utils/parser";

describe('GeneratorService', () => {
const generatorService = new GeneratorService();

describe('.generate()', () => {
it('should generate given template to the destination dir', async () => {
const asyncapi = {
"asyncapi": "2.2.0",
"info": {
"title": "Test Service",
"version": "1.0.0",
},
"channels": {},
};
const template = '@asyncapi/html-template';
const parameters = {
version: '2.1.37',
};

const tmpDir = createTempDirectory();
try {
await generatorService.generate(
JSON.stringify(asyncapi),
template,
parameters,
tmpDir,
prepareParserConfig(),
);

expect(fs.existsSync(path.join(tmpDir, 'template'))).toEqual(true);
expect(fs.existsSync(path.join(tmpDir, 'template/index.html'))).toEqual(true);
} catch(e: any) {
removeTempDirectory(tmpDir);
}
});
});
});
10 changes: 9 additions & 1 deletion src/utils/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ registerSchemaParser(openapiSchemaParser);
registerSchemaParser(ramlDtParser);
registerSchemaParser(avroSchemaParser);

function prepareParserConfig(req: Request) {
function prepareParserConfig(req?: Request) {
if (!req) {
return {
resolve: {
file: false,
},
}
}

return {
resolve: {
file: false,
Expand Down

0 comments on commit 456d081

Please sign in to comment.