Skip to content
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

Revert "feat: merge mixins options" #273

Merged
merged 1 commit into from
Sep 29, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 9 additions & 17 deletions src/shared/getComponentOption.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import deepmerge from 'deepmerge'
import isArray from './isArray'

/**
* Returns the `opts.option` $option value of the given `opts.component`.
Expand All @@ -25,10 +24,16 @@ export default function getComponentOption (opts, result = {}) {
if (typeof $options[option] !== 'undefined' && $options[option] !== null) {
let data = $options[option]

if (isArray(data)) {
result = data.reduce((result, dataItem) => mergeDataInResult(dataItem, result, component, arrayMerge), result)
// if option is a function, replace it with it's result
if (typeof data === 'function') {
data = data.call(component)
}

if (typeof data === 'object') {
// merge with existing options
result = deepmerge(result, data, { arrayMerge })
} else {
result = mergeDataInResult(data, result, component, arrayMerge)
result = data
}
}

Expand Down Expand Up @@ -62,16 +67,3 @@ export default function getComponentOption (opts, result = {}) {
}
return result
}

function mergeDataInResult (data, result, component, arrayMerge) {
// if option is a function, replace it with it's result
if (typeof data === 'function') {
data = data.call(component)
}

if (typeof data === 'object') {
// merge with existing options
return deepmerge(result, data, { arrayMerge })
}
return data
}
3 changes: 0 additions & 3 deletions src/shared/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ export default function VueMeta (Vue, options = {}) {
// bind the $meta method to this component instance
Vue.prototype.$meta = $meta(options)

// define optionMergeStrategies for the keyName
Vue.config.optionMergeStrategies[options.keyName] = Vue.config.optionMergeStrategies.created

// store an id to keep track of DOM updates
let batchID = null

Expand Down
79 changes: 10 additions & 69 deletions test/getComponentOption.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,77 +43,20 @@ describe('getComponentOption', () => {
expect(mergedOption).to.eql({ bar: 'baz', fizz: 'buzz' })
})

it('allows for a custom array merge strategy in object literal', () => {
it('allows for a custom array merge strategy', () => {
Vue.component('array-child', {
template: '<div></div>',
foo: {
flowers: [
{ name: 'flower', content: 'rose' }
]
}
})

component = new Vue({
render: (h) => h('div', null, [h('array-child')]),
foo: {
flowers: [
{ name: 'flower', content: 'tulip' }
]
},
el: container
})

const mergedOption = getComponentOption({
component,
option: 'foo',
deep: true,
arrayMerge (target, source) {
return target.concat(source)
}
})

expect(mergedOption).to.eql({
flowers: [
{ name: 'flower', content: 'tulip' },
foo: [
{ name: 'flower', content: 'rose' }
]
})
})

it('merges arrays of objects literal options', () => {
component = new Vue({ someOption: [{ foo: 'hello' }, { bar: 'there' }] })

const mergedOption = getComponentOption({ component, option: 'someOption' })
expect(mergedOption).to.eql({ foo: 'hello', bar: 'there' })
})

it('merges arrays of mixed object literals and functions', () => {
component = new Vue({
cake: 'good',
desserts: [
{ yogurt: 'meh' },
function someFunction () {
return { cake: this.$options.cake }
},
function someOtherFunction () {
return { pineapple: 'not bad' }
}
]
})

const mergedOption = getComponentOption({ component, option: 'desserts' })
expect(mergedOption).to.eql({ yogurt: 'meh', cake: 'good', pineapple: 'not bad' })
})

it('uses custom array merge strategy when merging arrays in arrays of options', () => {
component = new Vue({
template: '<div></div>',
render: (h) => h('div', null, [h('array-child')]),
foo: [
{ cars: [{ brand: 'renault', color: 'red' }] },
function someFunction () {
return { cars: [{ brand: 'peugeot', color: 'blue' }] }
}
]
{ name: 'flower', content: 'tulip' }
],
el: container
})

const mergedOption = getComponentOption({
Expand All @@ -125,11 +68,9 @@ describe('getComponentOption', () => {
}
})

expect(mergedOption).to.eql({
cars: [
{ brand: 'renault', color: 'red' },
{ brand: 'peugeot', color: 'blue' }
]
})
expect(mergedOption).to.eql([
{ name: 'flower', content: 'tulip' },
{ name: 'flower', content: 'rose' }
])
})
})
91 changes: 0 additions & 91 deletions test/getMetaInfo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ const defaultOptions = {

const getMetaInfo = _getMetaInfo(defaultOptions)

// define optionMergeStrategies for the keyName
Vue.config.optionMergeStrategies[VUE_META_KEY_NAME] = Vue.config.optionMergeStrategies.created

describe('getMetaInfo', () => {
// const container = document.createElement('div')
let component
Expand Down Expand Up @@ -533,92 +530,4 @@ describe('getMetaInfo', () => {
__dangerouslyDisableSanitizersByTagID: {}
})
})

it('properly merges mixins options', () => {
const mixin1 = {
metaInfo: function () {
return {
title: 'This title will be overridden',
meta: [
{
vmid: 'og:title',
property: 'og:title',
content: 'This title will be overridden'
},
{
vmid: 'og:fromMixin1',
property: 'og:fromMixin1',
content: 'This is from mixin1'
}
]
}
}
}
const mixin2 = {
metaInfo: {
meta: [
{
vmid: 'og:fromMixin2',
property: 'og:fromMixin2',
content: 'This is from mixin2'
}
]
}
}
const component = new Vue({
mixins: [mixin1, mixin2],
metaInfo: {
title: 'New Title',
meta: [
{
vmid: 'og:title',
property: 'og:title',
content: 'New Title! - My page'
},
{
vmid: 'og:description',
property: 'og:description',
content: 'Some Description'
}
]
}
})
expect(getMetaInfo(component)).to.eql({
title: 'New Title',
titleChunk: 'New Title',
titleTemplate: '%s',
htmlAttrs: {},
headAttrs: {},
bodyAttrs: {},
meta: [
{
vmid: 'og:fromMixin1',
property: 'og:fromMixin1',
content: 'This is from mixin1'
},
{
vmid: 'og:fromMixin2',
property: 'og:fromMixin2',
content: 'This is from mixin2'
},
{
vmid: 'og:title',
property: 'og:title',
content: 'New Title! - My page'
},
{
vmid: 'og:description',
property: 'og:description',
content: 'Some Description'
}
],
base: [],
link: [],
style: [],
script: [],
noscript: [],
__dangerouslyDisableSanitizers: [],
__dangerouslyDisableSanitizersByTagID: {}
})
})
})
5 changes: 0 additions & 5 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,4 @@ describe('plugin', () => {
const vm = new Vue(Component).$mount()
expect(vm._hasMetaInfo).to.equal(true)
})

it('setup optionMergeStrategies for the keyName', () => {
const strats = Vue.config.optionMergeStrategies
expect(strats[VUE_META_KEY_NAME]).to.equal(strats.created)
})
})