Skip to content

Commit

Permalink
✅ check coerce is called only when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Apr 18, 2018
1 parent 687e2a0 commit fbb2274
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ VueCoerceProps will inject a computed property named `$coerced` containing every
</p>
```

## FAQ

* Q: Why not passing component props as second argument?
A: That would make every coerce value depend on every prop. At the end `$coerced` is just a computed property

## License

[MIT](http://opensource.org/licenses/MIT)
35 changes: 35 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,39 @@ describe('VueCoerceProps', () => {
}).not.toThrow()
expect(wrapper.vm.$coerced.other).toBe('Hello Ed!')
})

test('calls coerce function only if props change', () => {
const spy = jest.fn()
spy.mockResolvedValue('hey')
wrapper = mount(
{
mixins: [mixin],
render: h => h(),
props: {
value: String,
trimmed: {
type: String,
coerce: spy,
},
},
},
{
propsData: {
value: 'Hi',
trimmed: 'hey',
},
}
)
// trigger a call
expect(wrapper.vm.$coerced).toBeDefined()
expect(spy).toHaveBeenCalledTimes(1)

wrapper.setProps({ value: 'hi2' })
expect(wrapper.vm.$coerced).toBeDefined()
expect(spy).toHaveBeenCalledTimes(1)

wrapper.setProps({ trimmed: 'hi3' })
expect(wrapper.vm.$coerced).toBeDefined()
expect(spy).toHaveBeenCalledTimes(2)
})
})

0 comments on commit fbb2274

Please sign in to comment.