import { expect } from '../../../../test-utils'; import { BaseComponent, CheckboxComponent, NumberComponent, SelectComponent, TextComponent } from '../components'; import { createSchemaComponent } from '../schema-component-factory'; import { EStepType, getJoiDescribeFromCode } from '../schema-generator'; describe('Schema Component Factory - createSchemaComponent', function () { it('should construct TextComponent type for joi string.', function () { const moduleCode = ` const quoteSchema = Joi.object() .keys({ first_name: Joi.string().required(), }); `; const schemaType = EStepType.Quote; const joiDescribeMap = getJoiDescribeFromCode({ localDefinitionCode: moduleCode, alterationHooks: [], schemaType, }); const joiDescribe = joiDescribeMap.get(schemaType); const component = createSchemaComponent({ name: 'first_name', properties: joiDescribe?.keys.first_name, }); expect(component).to.be.an.instanceOf(TextComponent); }); it('should construct NumberComponent type for joi number.', function () { const moduleCode = ` const quoteSchema = Joi.object() .keys({ age: Joi.number().required(), }); `; const schemaType = EStepType.Quote; const joiDescribeMap = getJoiDescribeFromCode({ localDefinitionCode: moduleCode, alterationHooks: [], schemaType, }); const joiDescribe = joiDescribeMap.get(schemaType); const component = createSchemaComponent({ name: 'age', properties: joiDescribe?.keys.age, }); expect(component).to.be.an.instanceOf(NumberComponent); }); it('should construct SelectComponent type for joi string with valids.', function () { const moduleCode = ` const quoteSchema = Joi.object() .keys({ smoker_status: Joi.string().valid('smoker', 'non_smoker').required(), }); `; const schemaType = EStepType.Quote; const joiDescribeMap = getJoiDescribeFromCode({ localDefinitionCode: moduleCode, alterationHooks: [], schemaType, }); const joiDescribe = joiDescribeMap.get(schemaType); const component = createSchemaComponent({ name: 'smoker_status', properties: joiDescribe?.keys.smoker_status, }); expect(component).to.be.an.instanceOf(SelectComponent); }); it('should construct CheckboxComponent type for joi boolean.', function () { const moduleCode = ` const quoteSchema = Joi.object() .keys({ is_smoker: Joi.boolean().required(), }); `; const schemaType = EStepType.Quote; const joiDescribeMap = getJoiDescribeFromCode({ localDefinitionCode: moduleCode, alterationHooks: [], schemaType, }); const joiDescribe = joiDescribeMap.get(schemaType); const component = createSchemaComponent({ name: 'is_smoker', properties: joiDescribe?.keys.is_smoker, }); expect(component).to.be.an.instanceOf(CheckboxComponent); }); it('should construct BaseComponent type for joi type with no concrete class type.', function () { const moduleCode = ` const quoteSchema = Joi.object() .keys({ date_of_birth: Joi.date().iso().required(), }); `; const schemaType = EStepType.Quote; const joiDescribeMap = getJoiDescribeFromCode({ localDefinitionCode: moduleCode, alterationHooks: [], schemaType, }); const joiDescribe = joiDescribeMap.get(schemaType); const component = createSchemaComponent({ name: 'date_of_birth', properties: joiDescribe?.keys.date_of_birth, }); expect(component).to.be.an.instanceOf(BaseComponent); }); });