vue2/test/unit/features/global-api/mixin.spec.js

60 lines
1.3 KiB
JavaScript
Raw Normal View History

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 () {
calls.push(this.$options.myOption + ' local')
}
})
Vue.mixin({
2016-06-28 00:29:59 +08:00
beforeCreate () {
calls.push(this.$options.myOption + ' global')
}
})
2016-06-26 11:18:08 +08:00
expect(Test.options.name).toBe('test')
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')
})
})