mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-05-25 19:14:04 +03:00
improve tests for getComponentOption
This commit is contained in:
@@ -24,14 +24,10 @@ export default function getComponentOption (opts, result = {}) {
|
||||
|
||||
if (typeof data === 'object') {
|
||||
// bind context of option methods (if any) to this component
|
||||
for (const key in data) {
|
||||
if (data.hasOwnProperty(key)) {
|
||||
const value = data[key]
|
||||
if (typeof value === 'function') {
|
||||
data[key] = value.bind(component)
|
||||
}
|
||||
}
|
||||
}
|
||||
Object.keys(data).forEach((key) => {
|
||||
const value = data[key]
|
||||
data[key] = typeof value === 'function' ? value.bind(component) : value
|
||||
})
|
||||
|
||||
// merge with existing options
|
||||
result = deepmerge(result, data, {
|
||||
@@ -40,17 +36,15 @@ export default function getComponentOption (opts, result = {}) {
|
||||
})
|
||||
|
||||
// collect & aggregate child options if deep = true
|
||||
if (deep) {
|
||||
const { $children } = component
|
||||
for (let i = 0, len = $children.length; i < len; i++) {
|
||||
const component = $children[i]
|
||||
if (deep && component.$children.length) {
|
||||
component.$children.forEach((childComponent) => {
|
||||
result = getComponentOption({
|
||||
component: childComponent,
|
||||
option,
|
||||
deep,
|
||||
component,
|
||||
arrayMerge
|
||||
}, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
@@ -7,12 +7,33 @@ describe('getComponentOption', () => {
|
||||
|
||||
afterEach(() => component.$destroy())
|
||||
|
||||
it('returns an empty object when no matching options are found', () => {
|
||||
component = new Vue()
|
||||
const fetchedOption = getComponentOption({ component, option: 'noop' })
|
||||
expect(fetchedOption).to.eql({})
|
||||
})
|
||||
|
||||
it('fetches the given option from the given component', () => {
|
||||
component = new Vue({ someOption: 'foo' })
|
||||
const fetchedOption = getComponentOption({ component, option: 'someOption' })
|
||||
expect(fetchedOption).to.eql('foo')
|
||||
})
|
||||
|
||||
it('binds option method context to the component instance', () => {
|
||||
component = new Vue({
|
||||
data: {
|
||||
age: 44
|
||||
},
|
||||
foo: {
|
||||
bar () {
|
||||
return this.age
|
||||
}
|
||||
}
|
||||
})
|
||||
const fetchedOption = getComponentOption({ component, option: 'foo' })
|
||||
expect(fetchedOption.bar()).to.equal(44)
|
||||
})
|
||||
|
||||
it('fetches deeply nested component options and merges them', () => {
|
||||
Vue.component('merge-child', { template: '<div></div>', foo: { bar: 'baz' } })
|
||||
|
||||
|
||||
Reference in New Issue
Block a user