From b4d69ec76cb14a399526f4af9f91ff8f5355fa27 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Thu, 14 Jun 2018 17:45:09 +0100 Subject: [PATCH 1/4] fix: add true props to props object --- packages/test-utils/src/wrapper.js | 6 +++++- test/specs/components/RouterLink.spec.js | 19 ++++++++++--------- test/specs/mounting-options/localVue.spec.js | 3 ++- test/specs/wrapper/destroy.spec.js | 10 +++++----- test/specs/wrapper/emitted.spec.js | 5 ++++- test/specs/wrapper/find.spec.js | 2 +- test/specs/wrapper/hasClass.spec.js | 7 ------- test/specs/wrapper/setComputed.spec.js | 1 + test/specs/wrapper/setData.spec.js | 5 +++-- 9 files changed, 31 insertions(+), 27 deletions(-) diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 308e0f4f7..5c5e40819 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -405,7 +405,11 @@ export default class Wrapper implements BaseWrapper { // $FlowIgnore _props = this.vm.$props } - return _props || {} // Return an empty object if no props exist + Object.keys(_props || {}).forEach(prop => { + // $FlowIgnore + _props[prop] = this.vm[prop] + }) + return _props || {} } /** diff --git a/test/specs/components/RouterLink.spec.js b/test/specs/components/RouterLink.spec.js index 628bc7ad2..c3c617467 100644 --- a/test/specs/components/RouterLink.spec.js +++ b/test/specs/components/RouterLink.spec.js @@ -8,13 +8,13 @@ describeWithShallowAndMount('RouterLinkStub', (mountingMethod) => {
` @@ -24,12 +24,13 @@ describeWithShallowAndMount('RouterLinkStub', (mountingMethod) => { RouterLink: RouterLinkStub } }) + const routerLink = wrapper.find(RouterLinkStub) expect(routerLink.props().to).to.equal('to1') - expect(routerLink.props().tag).to.equal('tag1') - expect(routerLink.props().exact).to.equal('exact1') - expect(routerLink.props().append).to.equal('append1') - expect(routerLink.props().replace).to.equal('replace1') + expect(routerLink.props().tag).to.equal('a') + expect(routerLink.props().exact).to.equal(true) + expect(routerLink.props().append).to.equal(true) + expect(routerLink.props().replace).to.equal(true) expect(routerLink.props().activeClass).to.equal('activeClass1') expect(routerLink.props().exactActiveClass).to.equal('exactActiveClass1') expect(routerLink.props().event).to.equal('event1') @@ -39,7 +40,7 @@ describeWithShallowAndMount('RouterLinkStub', (mountingMethod) => { const TestComponent = { template: `
- some text + some text
` } diff --git a/test/specs/mounting-options/localVue.spec.js b/test/specs/mounting-options/localVue.spec.js index 1410251f7..bb1c2fe30 100644 --- a/test/specs/mounting-options/localVue.spec.js +++ b/test/specs/mounting-options/localVue.spec.js @@ -15,7 +15,8 @@ describeWithMountingMethods('options.localVue', (mountingMethod) => { const TestComponent = { template: `
{{test}}
- ` + `, + data: { test: '' } } const localVue = Vue.extend() localVue.version = '2.3' diff --git a/test/specs/wrapper/destroy.spec.js b/test/specs/wrapper/destroy.spec.js index b752f3000..a66e7721b 100644 --- a/test/specs/wrapper/destroy.spec.js +++ b/test/specs/wrapper/destroy.spec.js @@ -3,10 +3,10 @@ import { describeWithShallowAndMount } from '~resources/utils' import sinon from 'sinon' describeWithShallowAndMount('destroy', (mountingMethod) => { - it('should trigger beforeDestroy ', () => { + it('triggers beforeDestroy ', () => { const spy = sinon.stub() mountingMethod({ - render: null, + render: () => {}, beforeDestroy () { spy() } @@ -14,10 +14,10 @@ describeWithShallowAndMount('destroy', (mountingMethod) => { expect(spy.calledOnce).to.equal(true) }) - it('should trigger destroy ', () => { + it('triggers destroy ', () => { const spy = sinon.stub() mountingMethod({ - render: null, + render: () => {}, destroyed () { spy() } @@ -25,7 +25,7 @@ describeWithShallowAndMount('destroy', (mountingMethod) => { expect(spy.calledOnce).to.equal(true) }) - it.skip('should remove element from document.body', () => { + it.skip('removes element from document.body', () => { const compiled = compileToFunctions('
') const wrapper = mountingMethod(compiled, { attachToDocument: true }) expect(wrapper.vm.$el.parentNode).to.equal(document.body) diff --git a/test/specs/wrapper/emitted.spec.js b/test/specs/wrapper/emitted.spec.js index 7f28353d9..da0a98930 100644 --- a/test/specs/wrapper/emitted.spec.js +++ b/test/specs/wrapper/emitted.spec.js @@ -63,7 +63,8 @@ describeWithShallowAndMount('emitted', (mountingMethod) => { }, mounted () { this.$emit('bar', 1, 2) - } + }, + render: () => {} }) expect(wrapper.emitted().foo).to.eql([[]]) @@ -74,12 +75,14 @@ describeWithShallowAndMount('emitted', (mountingMethod) => { const localVue = createLocalVue() const wrapper1 = mountingMethod({ + render: () => {}, beforeCreate () { this.$emit('foo') } }, { localVue }) const wrapper2 = mountingMethod({ + render: () => {}, mounted () { this.$emit('bar') } diff --git a/test/specs/wrapper/find.spec.js b/test/specs/wrapper/find.spec.js index a4c7c281e..bf5e7ea22 100644 --- a/test/specs/wrapper/find.spec.js +++ b/test/specs/wrapper/find.spec.js @@ -240,7 +240,7 @@ describeWithShallowAndMount('find', (mountingMethod) => { it('returns error Wrapper if Vue component is below Wrapper', () => { const AComponent = { render: () => {}, - name: 'a component' + name: 'a-component' } const localVue = createLocalVue() localVue.component('a-component', AComponent) diff --git a/test/specs/wrapper/hasClass.spec.js b/test/specs/wrapper/hasClass.spec.js index 88ecd3fb1..b846a2cae 100644 --- a/test/specs/wrapper/hasClass.spec.js +++ b/test/specs/wrapper/hasClass.spec.js @@ -40,13 +40,6 @@ describeWithShallowAndMount('hasClass', (mountingMethod) => { expect(wrapper.hasClass('color-red')).to.equal(true) }) - it('returns false if wrapper does not contain element', () => { - const wrapper = mountingMethod({ render: (h) => h('div.a-class.b-class') }) - const div = wrapper.find('div') - div.element = null - expect(wrapper.hasClass('a-class b-class')).to.equal(false) - }) - it('returns true when the element contains multiple classes', () => { const compiled = compileToFunctions('
') const wrapper = mountingMethod(compiled) diff --git a/test/specs/wrapper/setComputed.spec.js b/test/specs/wrapper/setComputed.spec.js index fdd491ebf..b4609f130 100644 --- a/test/specs/wrapper/setComputed.spec.js +++ b/test/specs/wrapper/setComputed.spec.js @@ -39,6 +39,7 @@ describeWithShallowAndMount('setComputed', (mountingMethod) => { it('updates vm computed value', () => { const TestComponent = { + render: () => {}, data () { return { a: 1 diff --git a/test/specs/wrapper/setData.spec.js b/test/specs/wrapper/setData.spec.js index d335ea798..d5f69a82d 100644 --- a/test/specs/wrapper/setData.spec.js +++ b/test/specs/wrapper/setData.spec.js @@ -133,7 +133,7 @@ describeWithShallowAndMount('setData', (mountingMethod) => { expect(wrapper.text()).to.equal('There is no message yet') }) - it('should update an existing property in a data object', () => { + it('updates an existing property in a data object', () => { const TestComponent = { data: () => ({ anObject: { @@ -142,7 +142,8 @@ describeWithShallowAndMount('setData', (mountingMethod) => { }, propB: 'b' } - }) + }), + render: () => {} } const wrapper = mountingMethod(TestComponent) wrapper.setData({ From 5d9a677686da9f2101e383e12f668a027950b1aa Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Thu, 14 Jun 2018 18:37:55 +0100 Subject: [PATCH 2/4] fix: return default props --- packages/test-utils/src/wrapper.js | 23 +++++++++++------------ test/specs/wrapper/props.spec.js | 13 +++++++++++++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 5c5e40819..00b1e44e4 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -397,19 +397,18 @@ export default class Wrapper implements BaseWrapper { if (!this.vm) { throwError('wrapper.props() must be called on a Vue instance') } - // $props object does not exist in Vue 2.1.x, so use $options.propsData instead - let _props - if (this.vm && this.vm.$options && this.vm.$options.propsData) { - _props = this.vm.$options.propsData - } else { - // $FlowIgnore - _props = this.vm.$props + + const props = {} + // $FlowIgnore + const keys = this.vm.$options._propKeys + + if (keys) { + keys.forEach(key => { + // $FlowIgnore + props[key] = this.vm[key] + }) } - Object.keys(_props || {}).forEach(prop => { - // $FlowIgnore - _props[prop] = this.vm[prop] - }) - return _props || {} + return props } /** diff --git a/test/specs/wrapper/props.spec.js b/test/specs/wrapper/props.spec.js index c325c76dc..731498507 100644 --- a/test/specs/wrapper/props.spec.js +++ b/test/specs/wrapper/props.spec.js @@ -36,6 +36,19 @@ describeWithShallowAndMount('props', (mountingMethod) => { expect(wrapper.props()).to.eql({ prop1: {}, prop2: 'val2' }) // fail }) + it('returns default props', () => { + const TestComponent = { + render: () => {}, + props: { + message: { + default: () => 'hello' + } + } + } + const wrapper = mountingMethod(TestComponent) + expect(wrapper.props().message).to.equal('hello') + }) + itSkipIf(!functionalSFCsSupported, 'works correctly a functional component', () => { const FunctionalComponent = { From 4130e0b5048e8d18b2e8de64bd94ed19ecb966db Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Thu, 14 Jun 2018 18:39:12 +0100 Subject: [PATCH 3/4] chore: add test-compat-all script --- scripts/test-compat-all.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100755 scripts/test-compat-all.sh diff --git a/scripts/test-compat-all.sh b/scripts/test-compat-all.sh new file mode 100755 index 000000000..8ee30c633 --- /dev/null +++ b/scripts/test-compat-all.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +set -e + +scripts/test-compat.sh "2.0.8" +scripts/test-compat.sh "2.1.10" +scripts/test-compat.sh "2.2.6" +scripts/test-compat.sh "2.3.4" +scripts/test-compat.sh "2.4.2" +scripts/test-compat.sh "2.5.13" \ No newline at end of file From 8cf6f9896fe1ce61df97aeec46fabb53c2e1873d Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Thu, 14 Jun 2018 19:21:17 +0100 Subject: [PATCH 4/4] refactor: remove redundant assignments --- packages/test-utils/src/wrapper.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 00b1e44e4..19ff58142 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -518,26 +518,21 @@ export default class Wrapper implements BaseWrapper { if (this.isFunctionalComponent) { throwError('wrapper.setProps() cannot be called on a functional component') } - if (!this.isVueInstance() || !this.vm) { + if (!this.isVm) { throwError('wrapper.setProps() can only be called on a Vue instance') } - if (this.vm && this.vm.$options && !this.vm.$options.propsData) { - this.vm.$options.propsData = {} - } + Object.keys(data).forEach((key) => { // Ignore properties that were not specified in the component options // $FlowIgnore : Problem with possibly null this.vm - if (!this.vm.$options._propKeys || !this.vm.$options._propKeys.some(prop => prop === key)) { + if (!this.vm.$options._propKeys || + !this.vm.$options._propKeys.some(prop => prop === key)) { throwError(`wrapper.setProps() called with ${key} property which is not defined on component`) } // $FlowIgnore : Problem with possibly null this.vm if (this.vm._props) { this.vm._props[key] = data[key] - // $FlowIgnore : Problem with possibly null this.vm.$props - this.vm.$props[key] = data[key] - // $FlowIgnore : Problem with possibly null this.vm.$options - this.vm.$options.propsData[key] = data[key] } else { // $FlowIgnore : Problem with possibly null this.vm this.vm[key] = data[key]