Skip to content

Commit de7e351

Browse files
authored
fix: support setComputed on Vue 2.1 (#90)
* feat(setComputed): add setComputed method * fix: support setComputed on Vue 2.1
1 parent 438603c commit de7e351

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

flow/wrapper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ declare interface BaseWrapper { // eslint-disable-line no-undef
3333

3434
declare type WrapperOptions = { // eslint-disable-line no-undef
3535
attachedToDocument: boolean,
36+
version: number,
3637
error?: string
3738
}

src/wrappers/wrapper.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
22

3+
import Vue from 'vue'
34
import { isValidSelector } from '../lib/validators'
45
import findVueComponents, { vmCtorMatchesName } from '../lib/find-vue-components'
56
import findMatchingVNodes from '../lib/find-matching-vnodes'
@@ -17,12 +18,14 @@ export default class Wrapper implements BaseWrapper {
1718
element: HTMLElement;
1819
update: Function;
1920
options: WrapperOptions;
21+
version: number
2022

2123
constructor (vnode: VNode, update: Function, options: WrapperOptions) {
2224
this.vnode = vnode
2325
this.element = vnode.elm
2426
this.update = update
2527
this.options = options
28+
this.version = Number(`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`)
2629
}
2730

2831
at () {
@@ -291,12 +294,25 @@ export default class Wrapper implements BaseWrapper {
291294
}
292295

293296
Object.keys(computed).forEach((key) => {
294-
// $FlowIgnore : Problem with possibly null this.vm
295-
if (!this.vm._computedWatchers[key]) {
296-
throwError(`wrapper.setComputed() was passed a value that does not exist as a computed property on the Vue instance. Property ${key} does not exist on the Vue instance`)
297+
if (this.version > 2.1) {
298+
// $FlowIgnore : Problem with possibly null this.vm
299+
if (!this.vm._computedWatchers[key]) {
300+
throwError(`wrapper.setComputed() was passed a value that does not exist as a computed property on the Vue instance. Property ${key} does not exist on the Vue instance`)
301+
}
302+
// $FlowIgnore : Problem with possibly null this.vm
303+
this.vm._computedWatchers[key].value = computed[key]
304+
} else {
305+
// $FlowIgnore : Problem with possibly null this.vm
306+
if (!this.vm._watchers.some(w => w.getter.name === key)) {
307+
throwError(`wrapper.setComputed() was passed a value that does not exist as a computed property on the Vue instance. Property ${key} does not exist on the Vue instance`)
308+
}
309+
// $FlowIgnore : Problem with possibly null this.vm
310+
this.vm._watchers.forEach((watcher) => {
311+
if (watcher.getter.name === key) {
312+
watcher.value = computed[key]
313+
}
314+
})
297315
}
298-
// $FlowIgnore : Problem with possibly null this.vm
299-
this.vm._computedWatchers[key].value = computed[key]
300316
})
301317
this.update()
302318
}

test/unit/specs/mount/Wrapper/setComputed.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe('setComputed', () => {
66
it('sets component computed props and updates when called on Vue instance', () => {
77
const wrapper = mount(ComponentWithComputed)
88
expect(wrapper.text()).to.contain('message')
9+
debugger
910
wrapper.setComputed({ reversedMessage: 'custom' })
1011
expect(wrapper.text()).to.contain('custom')
1112
})

0 commit comments

Comments
 (0)