From 8aec696ffad2cd01f13aa4300bd30bf65f7609ce Mon Sep 17 00:00:00 2001 From: Declan de Wet Date: Sat, 5 Nov 2016 07:33:13 +0200 Subject: [PATCH] improve tests for getComponentOption --- src/shared/getComponentOption.js | 22 ++++++++-------------- test/getComponentOption.spec.js | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/shared/getComponentOption.js b/src/shared/getComponentOption.js index 0c337d9..f5cb403 100644 --- a/src/shared/getComponentOption.js +++ b/src/shared/getComponentOption.js @@ -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 diff --git a/test/getComponentOption.spec.js b/test/getComponentOption.spec.js index ef4e2eb..8a7d5a6 100644 --- a/test/getComponentOption.spec.js +++ b/test/getComponentOption.spec.js @@ -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: '
', foo: { bar: 'baz' } })