2016-05-25 02:12:10 +08:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
|
|
|
describe('Global API: mixin', () => {
|
|
|
|
let options
|
|
|
|
beforeEach(() => { options = Vue.options })
|
|
|
|
afterEach(() => { Vue.options = options })
|
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
const spy = jasmine.createSpy('global mixin')
|
|
|
|
Vue.mixin({
|
|
|
|
created () {
|
|
|
|
spy(this.$options.myOption)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
new Vue({
|
|
|
|
myOption: 'hello'
|
|
|
|
})
|
|
|
|
expect(spy).toHaveBeenCalledWith('hello')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should work for constructors created before mixin is applied', () => {
|
|
|
|
const calls = []
|
|
|
|
const Test = Vue.extend({
|
2016-06-26 11:18:08 +08:00
|
|
|
name: 'test',
|
2016-06-28 00:29:59 +08:00
|
|
|
beforeCreate () {
|
2016-05-25 02:12:10 +08:00
|
|
|
calls.push(this.$options.myOption + ' local')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
Vue.mixin({
|
2016-06-28 00:29:59 +08:00
|
|
|
beforeCreate () {
|
2016-05-25 02:12:10 +08:00
|
|
|
calls.push(this.$options.myOption + ' global')
|
|
|
|
}
|
|
|
|
})
|
2016-06-26 11:18:08 +08:00
|
|
|
expect(Test.options.name).toBe('test')
|
2016-05-25 02:12:10 +08:00
|
|
|
new Test({
|
|
|
|
myOption: 'hello'
|
|
|
|
})
|
|
|
|
expect(calls).toEqual(['hello global', 'hello local'])
|
|
|
|
})
|
2016-11-03 03:50:57 +08:00
|
|
|
|
|
|
|
// #3957
|
|
|
|
it('should work for global props', () => {
|
|
|
|
const Test = Vue.extend({
|
|
|
|
template: `<div>{{ prop }}</div>`
|
|
|
|
})
|
|
|
|
|
|
|
|
Vue.mixin({
|
|
|
|
props: ['prop']
|
|
|
|
})
|
|
|
|
|
|
|
|
// test child component
|
|
|
|
const vm = new Vue({
|
|
|
|
template: '<test prop="hi"></test>',
|
|
|
|
components: { Test }
|
|
|
|
}).$mount()
|
|
|
|
|
|
|
|
expect(vm.$el.textContent).toBe('hi')
|
|
|
|
})
|
2016-05-25 02:12:10 +08:00
|
|
|
})
|