From 4d0e571fd2b5425bb40a8722e2927b13da559ab8 Mon Sep 17 00:00:00 2001 From: Martti Roitto Date: Sun, 7 Apr 2024 08:14:51 +0300 Subject: [PATCH] Add tests for findSchemaDefinitionRecursive --- .../utils/test/findSchemaDefinition.test.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/utils/test/findSchemaDefinition.test.ts b/packages/utils/test/findSchemaDefinition.test.ts index 00bdf802cc..f960e68b48 100644 --- a/packages/utils/test/findSchemaDefinition.test.ts +++ b/packages/utils/test/findSchemaDefinition.test.ts @@ -1,4 +1,5 @@ import { RJSFSchema, findSchemaDefinition } from '../src'; +import { findSchemaDefinitionRecursive } from '../src/findSchemaDefinition'; const schema: RJSFSchema = { type: 'object', @@ -67,3 +68,38 @@ describe('findSchemaDefinition()', () => { ); }); }); + +describe('()', () => { + it('throws error when ref is missing', () => { + expect(() => findSchemaDefinitionRecursive()).toThrowError('Could not find a definition for undefined'); + }); + it('throws error when ref is malformed', () => { + expect(() => findSchemaDefinitionRecursive('definitions/missing')).toThrowError( + 'Could not find a definition for definitions/missing' + ); + }); + it('throws error when ref does not exist', () => { + expect(() => findSchemaDefinitionRecursive('#/definitions/missing', schema)).toThrowError( + 'Could not find a definition for #/definitions/missing' + ); + }); + it('returns the string ref from its definition', () => { + expect(findSchemaDefinitionRecursive('#/definitions/stringRef', schema)).toBe(schema.definitions!.stringRef); + }); + it('returns the string ref from its nested definition', () => { + expect(findSchemaDefinitionRecursive('#/definitions/nestedRef', schema)).toBe(schema.definitions!.stringRef); + }); + it('returns a combined schema made from its nested definition with the extra props', () => { + expect(findSchemaDefinitionRecursive('#/definitions/extraNestedRef', schema)).toEqual(EXTRA_EXPECTED); + }); + it('throws error when ref is a circular reference', () => { + expect(() => findSchemaDefinitionRecursive('#/definitions/badCircularNestedRef', schema)).toThrowError( + 'Definition for #/definitions/badCircularNestedRef is a circular reference' + ); + }); + it('throws error when ref is a deep circular reference', () => { + expect(() => findSchemaDefinitionRecursive('#/definitions/badCircularDeepNestedRef', schema)).toThrowError( + 'Definition for #/definitions/badCircularDeepNestedRef contains a circular reference through #/definitions/badCircularDeeperNestedRef -> #/definitions/badCircularDeepestNestedRef -> #/definitions/badCircularDeepNestedRef' + ); + }); +});