|
| 1 | +import { augmentSchemaWithUiRequired, RJSFSchema, UiSchema } from '../src'; |
| 2 | + |
| 3 | +describe('augmentSchemaWithUiRequired', () => { |
| 4 | + it('returns the same schema when no uiSchema is provided', () => { |
| 5 | + const schema: RJSFSchema = { |
| 6 | + type: 'object', |
| 7 | + properties: { foo: { type: 'string' } }, |
| 8 | + }; |
| 9 | + expect(augmentSchemaWithUiRequired(schema)).toBe(schema); |
| 10 | + }); |
| 11 | + |
| 12 | + it('returns the same schema when no ui:required fields exist', () => { |
| 13 | + const schema: RJSFSchema = { |
| 14 | + type: 'object', |
| 15 | + properties: { foo: { type: 'string' } }, |
| 16 | + }; |
| 17 | + const uiSchema: UiSchema = { foo: { 'ui:widget': 'textarea' } }; |
| 18 | + expect(augmentSchemaWithUiRequired(schema, uiSchema)).toBe(schema); |
| 19 | + }); |
| 20 | + |
| 21 | + it('adds ui:required: true field to required array', () => { |
| 22 | + const schema: RJSFSchema = { |
| 23 | + type: 'object', |
| 24 | + properties: { foo: { type: 'string' }, bar: { type: 'string' } }, |
| 25 | + }; |
| 26 | + const uiSchema: UiSchema = { foo: { 'ui:required': true } }; |
| 27 | + const result = augmentSchemaWithUiRequired(schema, uiSchema); |
| 28 | + expect(result.required).toEqual(['foo']); |
| 29 | + expect(result).not.toBe(schema); |
| 30 | + }); |
| 31 | + |
| 32 | + it('does not duplicate existing required fields', () => { |
| 33 | + const schema: RJSFSchema = { |
| 34 | + type: 'object', |
| 35 | + required: ['foo'], |
| 36 | + properties: { foo: { type: 'string' } }, |
| 37 | + }; |
| 38 | + const uiSchema: UiSchema = { foo: { 'ui:required': true } }; |
| 39 | + const result = augmentSchemaWithUiRequired(schema, uiSchema); |
| 40 | + expect(result).toBe(schema); |
| 41 | + }); |
| 42 | + |
| 43 | + it('preserves existing required and adds new ones', () => { |
| 44 | + const schema: RJSFSchema = { |
| 45 | + type: 'object', |
| 46 | + required: ['foo'], |
| 47 | + properties: { foo: { type: 'string' }, bar: { type: 'string' } }, |
| 48 | + }; |
| 49 | + const uiSchema: UiSchema = { bar: { 'ui:required': true } }; |
| 50 | + const result = augmentSchemaWithUiRequired(schema, uiSchema); |
| 51 | + expect(result.required).toEqual(['foo', 'bar']); |
| 52 | + }); |
| 53 | + |
| 54 | + it('handles nested objects', () => { |
| 55 | + const schema: RJSFSchema = { |
| 56 | + type: 'object', |
| 57 | + properties: { |
| 58 | + address: { |
| 59 | + type: 'object', |
| 60 | + properties: { |
| 61 | + city: { type: 'string' }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }; |
| 66 | + const uiSchema: UiSchema = { |
| 67 | + address: { city: { 'ui:required': true } }, |
| 68 | + }; |
| 69 | + const result = augmentSchemaWithUiRequired(schema, uiSchema); |
| 70 | + expect((result.properties!.address as RJSFSchema).required).toEqual(['city']); |
| 71 | + }); |
| 72 | + |
| 73 | + it('does not mutate the original schema', () => { |
| 74 | + const schema: RJSFSchema = { |
| 75 | + type: 'object', |
| 76 | + properties: { foo: { type: 'string' } }, |
| 77 | + }; |
| 78 | + const uiSchema: UiSchema = { foo: { 'ui:required': true } }; |
| 79 | + augmentSchemaWithUiRequired(schema, uiSchema); |
| 80 | + expect(schema.required).toBeUndefined(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('returns same schema when nested object has uiSchema but no ui:required changes', () => { |
| 84 | + const schema: RJSFSchema = { |
| 85 | + type: 'object', |
| 86 | + properties: { |
| 87 | + address: { |
| 88 | + type: 'object', |
| 89 | + properties: { |
| 90 | + city: { type: 'string' }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }; |
| 95 | + const uiSchema: UiSchema = { |
| 96 | + address: { city: { 'ui:widget': 'textarea' } }, |
| 97 | + }; |
| 98 | + const result = augmentSchemaWithUiRequired(schema, uiSchema); |
| 99 | + expect(result).toBe(schema); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns schema as-is for non-object types', () => { |
| 103 | + const schema: RJSFSchema = { type: 'string' }; |
| 104 | + const uiSchema: UiSchema = { 'ui:required': true }; |
| 105 | + expect(augmentSchemaWithUiRequired(schema, uiSchema)).toBe(schema); |
| 106 | + }); |
| 107 | +}); |
0 commit comments