From c28dcee81e058c1def908148ff1ac1e57845c986 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Sat, 30 May 2020 14:58:04 +0200 Subject: [PATCH] - Updated test cases --- src/openApi/v2/parser/getRef.spec.ts | 33 +++++++++++++++ .../v3/interfaces/OpenApiComponents.d.ts | 2 +- src/openApi/v3/parser/getEnumValues.ts | 13 ------ src/openApi/v3/parser/getRef.spec.ts | 37 +++++++++++++++++ src/openApi/v3/parser/getRef.ts | 2 +- src/utils/flatMap.spec.ts | 9 +++++ src/utils/flatMap.ts | 2 +- src/utils/format.spec.ts | 40 +++++++++++++++++++ src/utils/isString.spec.ts | 13 ++++++ src/utils/writeClientSchemas.spec.ts | 9 +++-- src/utils/writeClientServices.spec.ts | 1 + src/utils/writeClientSettings.spec.ts | 30 ++++++++++++++ 12 files changed, 171 insertions(+), 20 deletions(-) create mode 100644 src/openApi/v2/parser/getRef.spec.ts delete mode 100644 src/openApi/v3/parser/getEnumValues.ts create mode 100644 src/openApi/v3/parser/getRef.spec.ts create mode 100644 src/utils/flatMap.spec.ts create mode 100644 src/utils/format.spec.ts create mode 100644 src/utils/isString.spec.ts create mode 100644 src/utils/writeClientSettings.spec.ts diff --git a/src/openApi/v2/parser/getRef.spec.ts b/src/openApi/v2/parser/getRef.spec.ts new file mode 100644 index 00000000..c475208b --- /dev/null +++ b/src/openApi/v2/parser/getRef.spec.ts @@ -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', + }); + }); +}); diff --git a/src/openApi/v3/interfaces/OpenApiComponents.d.ts b/src/openApi/v3/interfaces/OpenApiComponents.d.ts index d740bebb..8dafa556 100644 --- a/src/openApi/v3/interfaces/OpenApiComponents.d.ts +++ b/src/openApi/v3/interfaces/OpenApiComponents.d.ts @@ -19,7 +19,7 @@ export interface OpenApiComponents { examples?: Dictionary; requestBodies?: Dictionary; headers?: Dictionary; - securitySchemes: Dictionary; + securitySchemes?: Dictionary; links?: Dictionary; callbacks?: Dictionary; } diff --git a/src/openApi/v3/parser/getEnumValues.ts b/src/openApi/v3/parser/getEnumValues.ts deleted file mode 100644 index 348208fe..00000000 --- a/src/openApi/v3/parser/getEnumValues.ts +++ /dev/null @@ -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(); -} diff --git a/src/openApi/v3/parser/getRef.spec.ts b/src/openApi/v3/parser/getRef.spec.ts new file mode 100644 index 00000000..ef641b44 --- /dev/null +++ b/src/openApi/v3/parser/getRef.spec.ts @@ -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', + }); + }); +}); diff --git a/src/openApi/v3/parser/getRef.ts b/src/openApi/v3/parser/getRef.ts index 303382ba..891d924b 100644 --- a/src/openApi/v3/parser/getRef.ts +++ b/src/openApi/v3/parser/getRef.ts @@ -4,7 +4,7 @@ import { OpenApiReference } from '../interfaces/OpenApiReference'; export function getRef(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('/') diff --git a/src/utils/flatMap.spec.ts b/src/utils/flatMap.spec.ts new file mode 100644 index 00000000..4cd2c7a5 --- /dev/null +++ b/src/utils/flatMap.spec.ts @@ -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]); + }); +}); diff --git a/src/utils/flatMap.ts b/src/utils/flatMap.ts index 2fd63c1b..26fd4f28 100644 --- a/src/utils/flatMap.ts +++ b/src/utils/flatMap.ts @@ -4,7 +4,7 @@ */ export function flatMap(array: T[], callback: (value: T, index: number, array: T[]) => U[]): U[] { const result: U[] = []; - array.map(callback).forEach(arr => { + array.map(callback).forEach(arr => { result.push(...arr); }); return result; diff --git a/src/utils/format.spec.ts b/src/utils/format.spec.ts new file mode 100644 index 00000000..dee81b5c --- /dev/null +++ b/src/utils/format.spec.ts @@ -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); + }); +}); diff --git a/src/utils/isString.spec.ts b/src/utils/isString.spec.ts new file mode 100644 index 00000000..f082aa2c --- /dev/null +++ b/src/utils/isString.spec.ts @@ -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(); + }); +}); diff --git a/src/utils/writeClientSchemas.spec.ts b/src/utils/writeClientSchemas.spec.ts index c78900fa..9707bf76 100644 --- a/src/utils/writeClientSchemas.spec.ts +++ b/src/utils/writeClientSchemas.spec.ts @@ -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'); }); }); diff --git a/src/utils/writeClientServices.spec.ts b/src/utils/writeClientServices.spec.ts index 98968a57..beb30838 100644 --- a/src/utils/writeClientServices.spec.ts +++ b/src/utils/writeClientServices.spec.ts @@ -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[] = [ diff --git a/src/utils/writeClientSettings.spec.ts b/src/utils/writeClientSettings.spec.ts new file mode 100644 index 00000000..b263a142 --- /dev/null +++ b/src/utils/writeClientSettings.spec.ts @@ -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'); + }); +});