Skip to content

Commit

Permalink
Add tests for findSchemaDefinitionRecursive
Browse files Browse the repository at this point in the history
  • Loading branch information
MarttiR committed Apr 7, 2024
1 parent edad68d commit 4d0e571
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/utils/test/findSchemaDefinition.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RJSFSchema, findSchemaDefinition } from '../src';
import { findSchemaDefinitionRecursive } from '../src/findSchemaDefinition';

const schema: RJSFSchema = {
type: 'object',
Expand Down Expand Up @@ -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'
);
});
});

0 comments on commit 4d0e571

Please sign in to comment.