property-manager
Version:
make it easier to manage the properties/attributes of your class.
130 lines (110 loc) • 4.56 kB
JavaScript
import { AdvancePropertyManager, defineProperties, arrayOf } from '../src/index.js';
import { assert } from 'chai';
// --- Test Setup ---
// 1. Define a nested 'Tag' class
class Tag extends AdvancePropertyManager {}
defineProperties(Tag, {
name: { type: String, required: true },
color: { type: String, value: 'blue' },
ui: {
'ui:title': 'Tag'
}
});
// 2. Define a nested 'Address' class
class Address extends AdvancePropertyManager {}
defineProperties(Address, {
street: { type: String, required: true, title: 'Street Name' },
city: { type: String, value: 'Unknown City', title: 'City' },
ui: {
'ui:order': ['street', 'city'],
city: {
'ui:widget': 'textarea'
}
}
});
// 3. Define the main 'Person' class with nested objects and arrays
class Person extends AdvancePropertyManager {}
defineProperties(Person, {
name: { type: String, required: true, title: 'Full Name' },
age: { type: Number, description: 'Age in years' },
address: { type: Address }, // Nested Object
tags: arrayOf(Tag), // Array of Nested Objects
friends: arrayOf(String),// Array of Primitives
ui: {
'ui:order': ['name', 'age', 'address', 'tags', '*'],
age: {
'ui:widget': 'updown'
},
tags: {
'ui:title': 'User Tags'
}
}
});
describe('RJSF Schema Generation', () => {
let person;
beforeEach(() => {
// Create a new instance before each test
person = new Person({
name: 'John Doe',
age: 30,
address: {
street: '123 Main St'
},
tags: [
{ name: 'VIP' },
{ name: 'Early Adopter', color: 'green' }
],
friends: ['Jane', 'Mike']
});
});
it('should generate a correct and deeply nested JSON Schema', () => {
const { schema } = person.toRjsf();
// Top-level schema
assert.deepStrictEqual(schema.type, 'object');
assert.deepStrictEqual(schema.required, ['name']);
// Primitive types
assert.deepStrictEqual(schema.properties.name, { type: 'string', title: 'Full Name', default: undefined });
assert.deepStrictEqual(schema.properties.age, { type: 'number', description: 'Age in years', default: undefined });
// Nested Object: address
const addressSchema = schema.properties.address;
assert.deepStrictEqual(addressSchema.type, 'object');
assert.deepStrictEqual(addressSchema.required, ['street']);
assert.deepStrictEqual(addressSchema.properties.street, { type: 'string', required: true, title: 'Street Name', default: undefined });
assert.deepStrictEqual(addressSchema.properties.city, { type: 'string', value: 'Unknown City', title: 'City', default: undefined });
// Array of Nested Objects: tags
const tagsSchema = schema.properties.tags;
assert.deepStrictEqual(tagsSchema.type, 'array');
const tagItemSchema = tagsSchema.items;
assert.deepStrictEqual(tagItemSchema.type, 'object');
assert.deepStrictEqual(tagItemSchema.required, ['name']);
assert.deepStrictEqual(tagItemSchema.properties.name, { type: 'string', required: true, default: undefined });
assert.deepStrictEqual(tagItemSchema.properties.color, { type: 'string', value: 'blue', default: undefined });
// Array of Primitives: friends
const friendsSchema = schema.properties.friends;
assert.deepStrictEqual(friendsSchema.type, 'array');
assert.deepStrictEqual(friendsSchema.items, { type: 'string' });
});
it('should generate a correct and deeply nested UI Schema', () => {
const { uiSchema } = person.toRjsf();
// Top-level uiSchema
assert.deepStrictEqual(uiSchema['ui:order'], ['name', 'age', 'address', 'tags', '*']);
assert.deepStrictEqual(uiSchema.age, { 'ui:widget': 'updown' });
// Nested Object UI Schema: address
const addressUiSchema = uiSchema.address;
assert.deepStrictEqual(addressUiSchema['ui:order'], ['street', 'city']);
assert.deepStrictEqual(addressUiSchema.city, { 'ui:widget': 'textarea' });
// Array of Nested Objects UI Schema: tags
const tagsUiSchema = uiSchema.tags;
assert.deepStrictEqual(tagsUiSchema['ui:title'], 'User Tags');
// Check for the item-level uiSchema
const tagItemUiSchema = tagsUiSchema.items;
assert.deepStrictEqual(tagItemUiSchema, { 'ui:title': 'Tag' });
});
it('should return both schemasCombined with toRjsf()', () => {
const rjsfSchemas = person.toRjsf();
assert.exists(rjsfSchemas.schema);
assert.exists(rjsfSchemas.uiSchema);
assert.isObject(rjsfSchemas.schema);
assert.isObject(rjsfSchemas.uiSchema);
});
});