Skip to content

Commit

Permalink
fix: coerce empty string to true when prop type is boolean (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn committed Jul 19, 2018
1 parent 5a82e9e commit 2edc911
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/view/components/VueComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@ export default Vue.extend({
const values: Record<string, any> = {}
Object.keys(this.scope.props).forEach(name => {
values[name] =
name in this.propsData
? this.propsData[name]
: this.scope.props[name].value
const prop = this.scope.props[name]
if (name in this.propsData) {
const propValue = this.propsData[name]
// Coerce empty string to `true` when it is declared as boolean prop
values[name] =
prop.type === 'Boolean' && propValue === '' ? true : propValue
} else {
values[name] = prop.value
}
})
Object.keys(this.scope.data).forEach(name => {
Expand Down
37 changes: 37 additions & 0 deletions test/view/VueComponent/child-component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,43 @@ describe('VueComponent child components', () => {
expect(wrapper.find('h1').text()).toBe('hello from parent')
})

it('passes empty valued attribute as `true` value', () => {
// prettier-ignore
const template = createTemplate([
h('div', [], [
h('Foo', [a('enabled', null)], [])
])
])

const components = {
'file:///Foo.vue': {
// prettier-ignore
template: createTemplate([
h('h1', [], [exp('enabled && \'Success\'')])
]),
props: [
{
name: 'enabled',
type: 'Boolean'
}
]
}
}
const wrapper = render(
template,
[],
[],
[
{
name: 'Foo',
uri: 'file:///Foo.vue'
}
],
components
)
expect(wrapper.find('h1').text()).toBe('Success')
})

it('passes v-bind values as props to a child component', () => {
// prettier-ignore
const template = createTemplate([
Expand Down

0 comments on commit 2edc911

Please sign in to comment.