Skip to content

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

Merged
merged 7 commits into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/lib/addProvide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function addProvide (component, options) {
const provide = typeof options.provide === 'function'
Copy link
Contributor Author

@Austio Austio Jun 6, 2017

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.

2017-06-05 at 9 30 pm

? 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
Copy link
Contributor Author

@Austio Austio Jun 6, 2017

Choose a reason for hiding this comment

The 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
6 changes: 6 additions & 0 deletions src/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Vue from 'vue'
import VueWrapper from './VueWrapper'
import addSlots from './lib/addSlots'
import addGlobals from './lib/addGlobals'
import addProvide from './lib/addProvide'

Vue.config.productionTip = false

Expand Down Expand Up @@ -31,7 +32,12 @@ export default function mount (component, options = {}) {
// Remove cached constructor
delete component._Ctor // eslint-disable-line no-param-reassign

if (options.provide) {
addProvide(component, options)
}

const Constructor = Vue.extend(component)

const vm = new Constructor(options)

if (options.slots) {
Expand Down
15 changes: 15 additions & 0 deletions test/resources/components/component-with-inject.vue
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>
32 changes: 32 additions & 0 deletions test/unit/specs/mount/options/provide.spec.js
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')
})
})