Skip to content

Commit

Permalink
add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukad committed Feb 7, 2025
1 parent 581b98f commit 069c597
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions next/src/field/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function buildFieldObject(schema: JsfObjectSchema, name: string, required
...schema['x-jsf-presentation'],
type: schema['x-jsf-presentation']?.inputType || 'fieldset',
inputType: schema['x-jsf-presentation']?.inputType || 'fieldset',
jsonType: 'object',
name: schema.title || name,
required,
fields: orderedFields,
Expand Down
176 changes: 176 additions & 0 deletions next/test/fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,180 @@ describe('fields', () => {
},
])
})

it('should throw an error if the type is an array', () => {
const schema = {
type: 'object',
properties: {
name: { type: ['string', 'number'] },
},
}

expect(() => buildFieldSchema(schema, 'root', true)).toThrow(
'Array type is not yet supported',
)
})

it('should build an object field with multiple properties', () => {
const schema = {
type: 'object',
title: 'User',
properties: {
name: { type: 'string', title: 'Name' },
age: { type: 'number', title: 'Age' },
email: { type: 'string', title: 'Email' },
},
required: ['name', 'email'],
}

const field = buildFieldSchema(schema, 'user', false)

expect(field).toEqual({
type: 'fieldset',
inputType: 'fieldset',
name: 'User',
label: 'User',
required: false,
jsonType: 'object',
fields: [
{
type: 'text',
inputType: 'text',
jsonType: 'string',
name: 'name',
label: 'Name',
required: true,
},
{
type: 'text',
inputType: 'text',
jsonType: 'number',
name: 'age',
label: 'Age',
required: false,
},
{
type: 'text',
inputType: 'text',
jsonType: 'string',
name: 'email',
label: 'Email',
required: true,
},
],
})
})

it('should handle custom x-jsf-presentation properties', () => {
const schema = {
type: 'object',
properties: {
file: {
'type': 'string',
'title': 'Some field',
'x-jsf-presentation': {
inputType: 'file',
accept: '.pdf,.doc',
maxFileSize: 5000000,
},
},
},
}

const fields = buildFieldSchema(schema, 'root', true)!.fields!

expect(fields).toEqual([
{
type: 'text',
inputType: 'text',
jsonType: 'string',
name: 'file',
label: 'Some field',
required: false,
accept: '.pdf,.doc',
maxFileSize: 5000000,
},
])
})

it('should handle boolean schema', () => {
const schema = {
type: 'object',
properties: {
active: true,
},
}

const fields = buildFieldSchema(schema, 'root', true)!.fields!
expect(fields).toEqual([])
})

it('should handle nested objects', () => {
const schema = {
type: 'object',
properties: {
address: {
type: 'object',
title: 'Address',
properties: {
street: { type: 'string', title: 'Street' },
city: { type: 'string', title: 'City' },
},
required: ['street'],
},
},
}

const fields = buildFieldSchema(schema, 'root', true)!.fields!

expect(fields).toEqual([
{
type: 'fieldset',
inputType: 'fieldset',
jsonType: 'object',
name: 'Address',
label: 'Address',
required: false,
fields: [
{
type: 'text',
inputType: 'text',
jsonType: 'string',
name: 'street',
label: 'Street',
required: true,
},
{
type: 'text',
inputType: 'text',
jsonType: 'string',
name: 'city',
label: 'City',
required: false,
},
],
},
])
})

it('should use property name if title is not provided', () => {
const schema = {
type: 'object',
properties: {
user_email: { type: 'string' },
},
}

const fields = buildFieldSchema(schema, 'root', true)!.fields!

expect(fields).toEqual([
{
type: 'text',
inputType: 'text',
jsonType: 'string',
name: 'user_email',
required: false,
},
])
})
})

0 comments on commit 069c597

Please sign in to comment.