-
Notifications
You must be signed in to change notification settings - Fork 668
Add provide option to mount #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
72c47ca
beefb16
24e842f
431cc9f
de333f0
caa8868
9b5e557
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function addProvide (component, options) { | ||
const provide = typeof options.provide === 'function' | ||
? options.provide | ||
: Object.assign({}, options.provide) | ||
|
||
delete options.provide | ||
|
||
const originalBeforeCreate = component.beforeCreate | ||
component.beforeCreate = function beforeCreate () { | ||
this._provided = typeof provide === 'function' | ||
? provide.call(this) | ||
: provide | ||
|
||
if (originalBeforeCreate) { | ||
originalBeforeCreate.call(this) | ||
} | ||
|
||
component.beforeCreate = originalBeforeCreate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so....that was pretty hard to isolate as being the issue. My addProvide was mutating the beforeCreate in between tests. ' This is passing in chrome but not jsdom, will be able to check this out tomorrow :) Brings me to a thought, when we mount things does it make sense to clone them or give the option for them to be cloned? |
||
} | ||
} | ||
|
||
export default addProvide |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<template> | ||
<div> | ||
{{ fromMount }} | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'component-with-inject', | ||
inject: ['fromMount'], | ||
beforeCreate() { | ||
this.setInBeforeCreate = 'created' | ||
} | ||
}; | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import mount from '../../../../../src/mount' | ||
import ComponentWithInject from '../../../../resources/components/component-with-inject.vue' | ||
|
||
describe('provide option in mount', () => { | ||
it('provides objects which is injected by mounted component', () => { | ||
const wrapper = mount(ComponentWithInject, { | ||
provide: { fromMount: 'objectValue' } | ||
}) | ||
|
||
expect(wrapper.text()).to.contain('objectValue') | ||
}) | ||
|
||
it('provides function which is injected by mounted component', () => { | ||
const wrapper = mount(ComponentWithInject, { | ||
provide () { | ||
return { | ||
fromMount: 'functionValue' | ||
} | ||
} | ||
}) | ||
|
||
expect(wrapper.text()).to.contain('functionValue') | ||
}) | ||
|
||
it('supports beforeCreate in component', () => { | ||
const wrapper = mount(ComponentWithInject, { | ||
provide: { fromMount: '_' } | ||
}) | ||
|
||
expect(wrapper.vm.setInBeforeCreate).to.equal('created') | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to test here for if the provide is a function, otherwise the provide was turning into what appears to be an assertion.