UNPKG

openapi-codegen-typescript

Version:

OpenApi codegen for generating types an mocks from swagger json file

252 lines (251 loc) 12.9 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var convertToTypesFromSchemaProperties_1 = require("./convertToTypesFromSchemaProperties"); var getResultStringForArrayType_1 = require("./getResultStringForArrayType"); var getResultStringForArrayWithoutItemRef_1 = require("./getResultStringForArrayWithoutItemRef"); var getResultStringForBooleanType_1 = require("./getResultStringForBooleanType"); var getResultStringForDictionaryKey_1 = require("./getResultStringForDictionaryKey"); var getResultStringForExportInterface_1 = require("./getResultStringForExportInterface"); var getResultStringForNumberType_1 = require("./getResultStringForNumberType"); var getResultStringForStringType_1 = require("./getResultStringForStringType"); var getResultStringForUndefinedTypeRefAndOneOf_1 = require("./getResultStringForUndefinedTypeRefAndOneOf"); var getStandardString_1 = require("./getStandardString"); var getResultStringForAdditionalPropertiesType_1 = require("./getResultStringForAdditionalPropertiesType"); var getResultStringForInvalidSchemaProperties_1 = require("./getResultStringForInvalidSchemaProperties"); jest.mock('./getResultStringForAdditionalPropertiesType', function () { return ({ getResultStringForAdditionalPropertiesType: jest.fn(), }); }); jest.mock('./getResultStringForArrayType', function () { return ({ getResultStringForArrayType: jest.fn(), }); }); jest.mock('./getResultStringForArrayWithoutItemRef', function () { return ({ getResultStringForArrayWithoutItemRef: jest.fn(), }); }); jest.mock('./getResultStringForBooleanType', function () { return ({ getResultStringForBooleanType: jest.fn(), }); }); jest.mock('./getResultStringForDictionaryKey', function () { return ({ getResultStringForDictionaryKey: jest.fn(), }); }); jest.mock('./getResultStringForExportInterface', function () { return ({ getResultStringForExportInterface: jest.fn(), }); }); jest.mock('./getResultStringForNumberType', function () { return ({ getResultStringForNumberType: jest.fn(), }); }); jest.mock('./getResultStringForStringType', function () { return ({ getResultStringForStringType: jest.fn(), }); }); jest.mock('./getResultStringForUndefinedTypeRefAndOneOf', function () { return ({ getResultStringForUndefinedTypeRefAndOneOf: jest.fn(), }); }); jest.mock('./getStandardString', function () { return ({ getStandardString: jest.fn(), }); }); jest.mock('./getResultStringForAdditionalPropertiesType', function () { return ({ getResultStringForAdditionalPropertiesType: jest.fn(), }); }); jest.mock('./getResultStringForInvalidSchemaProperties', function () { return ({ getResultStringForInvalidSchemaProperties: jest.fn(), }); }); beforeEach(function () { jest.clearAllMocks(); }); var defaultProps = { $ref: undefined, additionalProperties: undefined, description: undefined, format: undefined, exclusiveMaximum: undefined, exclusiveMinimum: undefined, items: undefined, maxItems: undefined, maxLength: undefined, maximum: undefined, minItems: undefined, minLength: undefined, minimum: undefined, nullable: false, oneOf: undefined, propertyName: undefined, type: undefined, uniqueItems: undefined, xDictionaryKey: undefined, }; describe("convertToTypesFromSchemaProperties", function () { it("should call getResultStringForStringType when property has type string", function () { var schema = { properties: { propertyName: { type: 'string' } } }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getResultStringForStringType_1.getResultStringForStringType).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { propertyName: 'propertyName', type: 'string' })); }); it("should call getResultStringForNumberType when property has type number", function () { var schema = { properties: { propertyName: { type: 'number' } } }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getResultStringForNumberType_1.getResultStringForNumberType).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { propertyName: 'propertyName', type: 'number' })); }); it("should call getResultStringForBooleanType when property has type boolean", function () { var schema = { properties: { propertyName: { type: 'boolean' } } }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getResultStringForBooleanType_1.getResultStringForBooleanType).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { propertyName: 'propertyName', type: 'boolean' })); }); it("should call getResultStringForArrayType when property has type array with items that have $ref", function () { var schema = { properties: { propertyName: { type: 'array', items: { $ref: 'path/to/ref' }, }, }, }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getResultStringForArrayType_1.getResultStringForArrayType).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { propertyName: 'propertyName', type: 'array', refType: ['path', 'to', 'ref'], items: { $ref: 'path/to/ref', } })); }); it("should call getResultStringForArrayWithoutItemRef when property has type array with items without $ref", function () { var schema = { properties: { propertyName: { type: 'array', items: {}, }, }, }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getResultStringForArrayWithoutItemRef_1.getResultStringForArrayWithoutItemRef).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { propertyName: 'propertyName', type: 'array', items: {} })); }); it("should call getStandardString when property has oneOf array", function () { var schema = { properties: { propertyName: { type: 'array', oneOf: [{ $ref: 'path/to/ref' }] } } }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getStandardString_1.getStandardString).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { propertyName: 'propertyName', type: 'array', isArray: false, refType: ['path', 'to', 'ref'], oneOf: [ { $ref: 'path/to/ref', }, ] })); }); it("should call getStandardString when property has $ref", function () { var schema = { properties: { propertyName: { $ref: 'path/to/ref' } } }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getStandardString_1.getStandardString).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { propertyName: 'propertyName', refType: ['path', 'to', 'ref'], $ref: 'path/to/ref', isArray: false })); }); it("should call getResultStringForUndefinedTypeRefAndOneOf when property has undefined type, ref and oneOf", function () { var schema = { properties: { propertyName: { $ref: undefined, type: undefined, oneOf: undefined } } }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getResultStringForUndefinedTypeRefAndOneOf_1.getResultStringForUndefinedTypeRefAndOneOf).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { propertyName: 'propertyName' })); }); it("should call getResultStringForDictionaryKey when property has", function () { var schema = { properties: { propertyName: { type: 'object', 'x-dictionaryKey': { $ref: 'path/to/ref' }, additionalProperties: { $ref: 'path/to/ref' }, }, }, }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getResultStringForDictionaryKey_1.getResultStringForDictionaryKey).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { propertyName: 'propertyName', type: 'object', xDictionaryKey: { $ref: 'path/to/ref', }, additionalProperties: { $ref: 'path/to/ref', } })); }); it("should call getResultStringForAdditionalPropertiesType when property has", function () { var schema = { properties: { propertyName: { type: 'object', additionalProperties: { type: 'array' }, }, }, }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getResultStringForAdditionalPropertiesType_1.getResultStringForAdditionalPropertiesType).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { propertyName: 'propertyName', type: 'object', additionalProperties: { type: 'array', } })); }); it("should call getResultStringForInvalidSchemaProperties when property has invalid type", function () { var schema = { properties: { propertyName: { type: 'any-other-type' } }, }; var props = { schema: schema, schemaKey: 'TypeWithId', interfaces: undefined, }; convertToTypesFromSchemaProperties_1.convertToTypesFromSchemaProperties(props); expect(getResultStringForExportInterface_1.getResultStringForExportInterface).toHaveBeenCalledWith(props); expect(getResultStringForInvalidSchemaProperties_1.getResultStringForInvalidSchemaProperties).toHaveBeenCalledWith(__assign(__assign({}, defaultProps), { type: 'any-other-type', propertyName: 'propertyName', errorMessage: 'Invalid schema properties' })); }); });