- Updated test cases

This commit is contained in:
Ferdi Koomen 2020-05-30 14:58:04 +02:00
parent 6fa43925ce
commit c28dcee81e
12 changed files with 171 additions and 20 deletions

View File

@ -0,0 +1,33 @@
import { getRef } from './getRef';
describe('getRef', () => {
it('should produce correct result', () => {
expect(
getRef(
{
swagger: '2.0',
info: {
title: 'dummy',
version: '1.0',
},
host: 'localhost:8080',
basePath: '/api',
schemes: ['http', 'https'],
paths: {},
definitions: {
Example: {
description: 'This is an Example model ',
type: 'integer',
},
},
},
{
$ref: '#/definitions/Example',
}
)
).toEqual({
description: 'This is an Example model ',
type: 'integer',
});
});
});

View File

@ -19,7 +19,7 @@ export interface OpenApiComponents {
examples?: Dictionary<OpenApiExample>;
requestBodies?: Dictionary<OpenApiRequestBody>;
headers?: Dictionary<OpenApiHeader>;
securitySchemes: Dictionary<OpenApiSecurityScheme>;
securitySchemes?: Dictionary<OpenApiSecurityScheme>;
links?: Dictionary<OpenApiLink>;
callbacks?: Dictionary<OpenApiCallback>;
}

View File

@ -1,13 +0,0 @@
import { Enum } from '../../../client/interfaces/Enum';
export function getEnumValues(enumerators: Enum[]): string[] {
// Fetch values from the symbols, just to be sure we filter out
// any double values and finally we sort them to make them easier
// to read when we use them in our generated code.
return enumerators
.map(enumerator => enumerator.value)
.filter((enumerator, index, arr) => {
return arr.indexOf(enumerator) === index;
})
.sort();
}

View File

@ -0,0 +1,37 @@
import { getRef } from './getRef';
describe('getRef', () => {
it('should produce correct result', () => {
expect(
getRef(
{
openapi: '3.0',
info: {
title: 'dummy',
version: '1.0',
},
paths: {},
servers: [
{
url: 'https://localhost:8080/api',
},
],
components: {
schemas: {
Example: {
description: 'This is an Example model ',
type: 'integer',
},
},
},
},
{
$ref: '#/components/schemas/Example',
}
)
).toEqual({
description: 'This is an Example model ',
type: 'integer',
});
});
});

View File

@ -4,7 +4,7 @@ import { OpenApiReference } from '../interfaces/OpenApiReference';
export function getRef<T>(openApi: OpenApi, item: T & OpenApiReference): T {
if (item.$ref) {
// Fetch the paths to the definitions, this converts:
// "#/definitions/Form" to ["definitions", "Form"]
// "#/components/schemas/Form" to ["components", "schemas", "Form"]
const paths = item.$ref
.replace(/^#/g, '')
.split('/')

View File

@ -0,0 +1,9 @@
import { flatMap } from './flatMap';
describe('flatMap', () => {
it('should produce correct result', () => {
expect(flatMap([1, 2, 3], i => [i])).toEqual([1, 2, 3]);
expect(flatMap([1, 2, 3], i => [i + 1])).toEqual([2, 3, 4]);
expect(flatMap([1, 2, 3], () => [1])).toEqual([1, 1, 1]);
});
});

View File

@ -4,7 +4,7 @@
*/
export function flatMap<U, T>(array: T[], callback: (value: T, index: number, array: T[]) => U[]): U[] {
const result: U[] = [];
array.map(callback).forEach(arr => {
array.map<U[]>(callback).forEach(arr => {
result.push(...arr);
});
return result;

40
src/utils/format.spec.ts Normal file
View File

@ -0,0 +1,40 @@
import { format } from './format';
const input1 = `{ foo: true }`;
const output1 = `{ foo: true }`;
const input2 = `{ foo: true, bar: 123 }`;
const output2 = `{ foo: true, bar: 123 }`;
const input3 = `{
foo: true,
bar: 123
}`;
const output3 = `{
foo: true,
bar: 123
}`;
const input4 = `{
\t\t\t\tfoo: true,
\t\t\t\tbar: 123
}`;
const output4 = `{
foo: true,
bar: 123
}`;
describe('format', () => {
it('should produce correct result', () => {
expect(format(``)).toEqual('');
expect(format(`{}`)).toEqual('{}');
expect(format(input1)).toEqual(output1);
expect(format(input2)).toEqual(output2);
expect(format(input3)).toEqual(output3);
expect(format(input4)).toEqual(output4);
});
});

View File

@ -0,0 +1,13 @@
import { isString } from './isString';
describe('isString', () => {
it('should produce correct result', () => {
expect(isString('foo')).toBeTruthy();
expect(isString('123')).toBeTruthy();
expect(isString('-1')).toBeTruthy();
expect(isString('')).toBeTruthy();
expect(isString(null)).toBeFalsy();
expect(isString(undefined)).toBeFalsy();
expect(isString({})).toBeFalsy();
});
});

View File

@ -1,10 +1,11 @@
import { Model } from '../client/interfaces/Model';
import { writeFile } from './fileSystem';
import { Templates } from './registerHandlebarsTemplates';
import { writeClientModels } from './writeClientModels';
import { writeClientSchemas } from './writeClientSchemas';
jest.mock('./fileSystem');
describe('writeClientModels', () => {
describe('writeClientSchemas', () => {
it('should write to filesystem', async () => {
const models: Model[] = [
{
@ -35,8 +36,8 @@ describe('writeClientModels', () => {
settings: () => 'dummy',
};
await writeClientModels(models, templates, '/');
await writeClientSchemas(models, templates, '/');
expect(writeFile).toBeCalledWith('/Item.ts', 'dummy');
expect(writeFile).toBeCalledWith('/$Item.ts', 'dummy');
});
});

View File

@ -4,6 +4,7 @@ import { Templates } from './registerHandlebarsTemplates';
import { writeClientServices } from './writeClientServices';
jest.mock('./fileSystem');
describe('writeClientServices', () => {
it('should write to filesystem', async () => {
const services: Service[] = [

View File

@ -0,0 +1,30 @@
import { Client } from '../client/interfaces/Client';
import { HttpClient } from '../index';
import { writeFile } from './fileSystem';
import { Templates } from './registerHandlebarsTemplates';
import { writeClientSettings } from './writeClientSettings';
jest.mock('./fileSystem');
describe('writeClientSettings', () => {
it('should write to filesystem', async () => {
const client: Client = {
server: 'http://localhost:8080',
version: '1.0',
models: [],
services: [],
};
const templates: Templates = {
index: () => 'dummy',
model: () => 'dummy',
schema: () => 'dummy',
service: () => 'dummy',
settings: () => 'dummy',
};
await writeClientSettings(client, templates, '/', HttpClient.FETCH);
expect(writeFile).toBeCalledWith('/OpenAPI.ts', 'dummy');
});
});