UNPKG

schyma

Version:

JSON Schemas Visualizer React component

167 lines 7.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const reusables_1 = require("../reusables"); const types_1 = require("../../types"); describe('getCompositionType', () => { it('returns OneOf for oneOf schemas', () => { expect((0, reusables_1.getCompositionType)({ oneOf: [] })).toBe(types_1.CompositionType.OneOf); }); it('returns AnyOf for anyOf schemas', () => { expect((0, reusables_1.getCompositionType)({ anyOf: [] })).toBe(types_1.CompositionType.AnyOf); }); it('returns AllOf for allOf schemas', () => { expect((0, reusables_1.getCompositionType)({ allOf: [] })).toBe(types_1.CompositionType.AllOf); }); it('returns Not for not schemas', () => { expect((0, reusables_1.getCompositionType)({ not: {} })).toBe(types_1.CompositionType.Not); }); it('returns null for regular schemas', () => { expect((0, reusables_1.getCompositionType)({ type: 'object', properties: {} })).toBe(null); }); }); describe('getNameFromRef', () => { it('extracts name from definition ref', () => { expect((0, reusables_1.nameFromRef)('#/definitions/Message')).toBe('Message'); }); it('extracts name from components ref', () => { expect((0, reusables_1.nameFromRef)('#/components/schemas/User')).toBe('User'); }); // Coming back to this test... gave me an idea for external // referencing it('handles file refs', () => { expect((0, reusables_1.nameFromRef)('./common/types.json')).toBe('types'); }); }); describe('convertArrayToPropertyObject', () => { it('uses title for named schemas', () => { const schemas = [{ title: 'Queue', type: 'object' }]; const result = (0, reusables_1.arrayToProps)(schemas, 'destination'); expect(Object.keys(result)).toEqual(['Queue']); }); it('uses $ref name for ref schemas', () => { const schemas = [{ $ref: '#/definitions/Message' }]; const result = (0, reusables_1.arrayToProps)(schemas, 'item'); expect(Object.keys(result)).toEqual(['Message']); }); it('uses const discriminator for objects with const', () => { const schemas = [{ properties: { type: { const: 'queue' } } }, { properties: { type: { const: 'topic' } } }]; const result = (0, reusables_1.arrayToProps)(schemas, 'destination'); expect(Object.keys(result)).toEqual(['queue', 'topic']); }); it('uses primitive type as name', () => { const schemas = [{ type: 'string' }, { type: 'number' }]; const result = (0, reusables_1.arrayToProps)(schemas, 'value'); expect(Object.keys(result)).toEqual(['string', 'number']); }); it('falls back to indexed name for unnamed object schemas', () => { const schemas = [ { type: 'object', properties: { foo: {} } }, { type: 'object', properties: { bar: {} } }, ]; const result = (0, reusables_1.arrayToProps)(schemas, 'item'); expect(Object.keys(result)).toEqual(['item 1', 'item 2']); }); }); describe('propertiesMerge', () => { it('merges patternProperties', () => { const schema = { patternProperties: { '^x-': { type: 'string' }, }, }; const result = (0, reusables_1.propMerge)(schema, 'test'); expect(Object.keys(result)).toEqual(['^x-']); }); it('flattens oneOf into merged props', () => { const schema = { oneOf: [ { title: 'CreditCard', type: 'object', properties: { cardNumber: { type: 'string' } } }, { title: 'BankTransfer', type: 'object', properties: { accountNumber: { type: 'string' } } }, ], }; const result = (0, reusables_1.propMerge)(schema, 'paymentMethod'); expect(Object.keys(result)).toContain('CreditCard'); expect(Object.keys(result)).toContain('BankTransfer'); }); it('tags oneOf children with _compositionSource', () => { const schema = { oneOf: [{ title: 'Queue', type: 'object' }], }; const result = (0, reusables_1.propMerge)(schema, 'test'); expect(result.Queue).toHaveProperty('_compositionSource', types_1.CompositionType.OneOf); }); it('tags anyOf children with _compositionSource', () => { const schema = { anyOf: [ { title: 'Email', type: 'object', properties: { address: { type: 'string' } } }, { title: 'SMS', type: 'object', properties: { phoneNumber: { type: 'string' } } }, ], }; const result = (0, reusables_1.propMerge)(schema, 'notification'); expect(result.Email).toHaveProperty('_compositionSource', types_1.CompositionType.AnyOf); expect(result.SMS).toHaveProperty('_compositionSource', types_1.CompositionType.AnyOf); }); it('recursively processes items schema', () => { const schema = { type: 'array', items: { properties: { name: { type: 'string' }, }, }, }; const result = (0, reusables_1.propMerge)(schema, 'test'); expect(Object.keys(result)).toContain('name'); }); it('propagates _nestedComposition from items with composition', () => { const schema = { type: 'array', items: { oneOf: [ { title: 'Rectangle', type: 'object', properties: { width: { type: 'number' } } }, { title: 'Circle', type: 'object', properties: { radius: { type: 'number' } } }, ], }, }; const result = (0, reusables_1.propMerge)(schema, 'shapes'); expect(result._nestedComposition).toBe(types_1.CompositionType.OneOf); }); it('handles if/then/else conditions', () => { const schema = { if: { properties: { type: { const: 'premium' } } }, then: { properties: { discount: { type: 'number' } } }, else: { properties: { standard: { type: 'boolean' } } }, }; const result = (0, reusables_1.propMerge)(schema, 'test'); expect(Object.keys(result)).toContain('if type = premium'); expect(Object.keys(result)).toContain('else (type)'); }); it('merges properties with oneOf (Solace-style schema)', () => { const schema = { type: 'array', items: { properties: { deliveryMode: { type: 'string' }, }, oneOf: [ { properties: { destinationType: { const: 'queue' } } }, { properties: { destinationType: { const: 'topic' } } }, ], }, }; const result = (0, reusables_1.propMerge)(schema, 'destinations'); // Should have deliveryMode from properties expect(Object.keys(result)).toContain('deliveryMode'); // Should have queue and topic from oneOf expect(Object.keys(result)).toContain('queue'); expect(Object.keys(result)).toContain('topic'); // oneOf items should be tagged expect(result.queue).toHaveProperty('_compositionSource', types_1.CompositionType.OneOf); expect(result.topic).toHaveProperty('_compositionSource', types_1.CompositionType.OneOf); // deliveryMode should NOT be tagged expect(result.deliveryMode).not.toHaveProperty('_compositionSource'); // Should propagate nested composition expect(result._nestedComposition).toBe(types_1.CompositionType.OneOf); }); }); //# sourceMappingURL=reusables.test.js.map