Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
fix: default boolean false is not accepted (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
marina-mosti authored Oct 5, 2021
1 parent 5f368b3 commit 2d52bd4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/formvuelate/src/features/FormModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function useFormModel (props, parsedSchema) {
const formModel = inject(FORM_MODEL, {})

forEachSchemaElement(parsedSchema, (el, path) => {
if (!el.default) return
if (!('default' in el)) return

updateFormModel(formModel, el.model, el.default, path)
})
Expand Down
145 changes: 92 additions & 53 deletions packages/formvuelate/tests/unit/SchemaForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,98 @@ describe('SchemaForm', () => {
})
})

describe('default schema values', () => {
it('populates the formModel with the schema when created if a default prop exists', () => {
const schema = {
firstName: {
component: FormText,
label: 'First Name',
default: 'Darth'
},
lastName: {
component: FormText,
label: 'Last Name',
default: 'Vader'
},
contact: {
component: SchemaForm,
schema: {
email: {
component: FormText,
label: 'Email',
default: '[email protected]'
},
address: {
FormText,
label: 'Address'
},
deepNest: {
component: SchemaForm,
schema: {
lightSaber: {
component: FormText,
label: 'Lightsaber',
default: 'Red'
}
}
}
}
}
}

const formModel = ref({})

mount(SchemaWrapperFactory(schema, {}, formModel))
expect(formModel.value).toEqual({
firstName: 'Darth',
lastName: 'Vader',
contact: {
email: '[email protected]',
deepNest: {
lightSaber: 'Red'
}
}
})
})

it('can set default schema values to boolean false', () => {
const schema = {
firstName: {
component: FormText,
label: 'First Name',
default: false
},
contact: {
component: SchemaForm,
schema: {
deepNest: {
component: SchemaForm,
schema: {
lightSaber: {
component: FormText,
label: 'Lightsaber',
default: false
}
}
}
}
}
}

const formModel = ref({})

mount(SchemaWrapperFactory(schema, {}, formModel))
expect(formModel.value).toEqual({
firstName: false,
contact: {
deepNest: {
lightSaber: false
}
}
})
})
})

it('renders a form with multiple nested schemas at the same nesting level', () => {
const nestedSchema = {
work: {
Expand Down Expand Up @@ -206,59 +298,6 @@ describe('SchemaForm', () => {
expect(wrapper.findAllComponents(FormSelect)).toHaveLength(2)
})

it('populates the formModel with the schema when created if a default prop exists', () => {
const schema = {
firstName: {
component: FormText,
label: 'First Name',
default: 'Darth'
},
lastName: {
component: FormText,
label: 'Last Name',
default: 'Vader'
},
contact: {
component: SchemaForm,
schema: {
email: {
component: FormText,
label: 'Email',
default: '[email protected]'
},
address: {
FormText,
label: 'Address'
},
deepNest: {
component: SchemaForm,
schema: {
lightSaber: {
component: FormText,
label: 'Lightsaber',
default: 'Red'
}
}
}
}
}
}

const formModel = ref({})

mount(SchemaWrapperFactory(schema, {}, formModel))
expect(formModel.value).toEqual({
firstName: 'Darth',
lastName: 'Vader',
contact: {
email: '[email protected]',
deepNest: {
lightSaber: 'Red'
}
}
})
})

describe('a11y', () => {
it('injects a unique id to each component', () => {
const schema = {
Expand Down

0 comments on commit 2d52bd4

Please sign in to comment.