Skip to content

Commit 6ef437d

Browse files
authored
feat(setComputed): add setComputed method (#84)
1 parent eef3792 commit 6ef437d

File tree

10 files changed

+95
-2
lines changed

10 files changed

+95
-2
lines changed

flow/wrapper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ declare interface BaseWrapper { // eslint-disable-line no-undef
2424
name(): string | void,
2525
text(): string | void,
2626
setData(data: Object): void,
27+
setComputed(computed: Object): void,
2728
setMethods(methods: Object): void,
2829
setProps(data: Object): void,
2930
trigger(type: string, options: Object): void,

src/wrappers/error-wrapper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export default class ErrorWrapper implements BaseWrapper {
7676
throwError(`find did not return ${this.selector}, cannot call text() on empty Wrapper`)
7777
}
7878

79+
setComputed (): void {
80+
throwError(`find did not return ${this.selector}, cannot call setComputed() on empty Wrapper`)
81+
}
82+
7983
setData (): void {
8084
throwError(`find did not return ${this.selector}, cannot call setData() on empty Wrapper`)
8185
}

src/wrappers/wrapper-array.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ export default class WrapperArray implements BaseWrapper {
119119
}
120120
}
121121

122+
setComputed (computed: Object): void {
123+
this.throwErrorIfWrappersIsEmpty('setComputed')
124+
125+
this.wrappers.forEach(wrapper => wrapper.setComputed(computed))
126+
}
127+
122128
setData (data: Object): void {
123129
this.throwErrorIfWrappersIsEmpty('setData')
124130

src/wrappers/wrapper.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,26 @@ export default class Wrapper implements BaseWrapper {
283283
}
284284

285285
/**
286-
* Sets vm data
286+
* Sets vm computed
287+
*/
288+
setComputed (computed: Object) {
289+
if (!this.isVueComponent) {
290+
throwError('wrapper.setComputed() can only be called on a Vue instance')
291+
}
292+
293+
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+
}
298+
// $FlowIgnore : Problem with possibly null this.vm
299+
this.vm._computedWatchers[key].value = computed[key]
300+
})
301+
this.update()
302+
}
303+
304+
/**
305+
* Sets vm methods
287306
*/
288307
setMethods (methods: Object) {
289308
if (!this.isVueComponent) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template>
2+
<div>
3+
{{reversedMessage}}
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default{
9+
name: 'component-with-child',
10+
data: () => ({
11+
message: 'egassem'
12+
}),
13+
computed: {
14+
reversedMessage: function () {
15+
return this.message.split('').reverse().join('')
16+
}
17+
}
18+
}
19+
</script>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { compileToFunctions } from 'vue-template-compiler'
2+
import mount from '~src/mount'
3+
import ComponentWithComputed from '~resources/components/component-with-computed.vue'
4+
5+
describe('setComputed', () => {
6+
it('sets component computed props and updates when called on Vue instance', () => {
7+
const wrapper = mount(ComponentWithComputed)
8+
expect(wrapper.text()).to.contain('message')
9+
wrapper.setComputed({ reversedMessage: 'custom' })
10+
expect(wrapper.text()).to.contain('custom')
11+
})
12+
13+
it('throws an error if computed watcher does not exist', () => {
14+
const message = 'wrapper.setComputed() was passed a value that does not exist as a computed property on the Vue instance. Property noExist does not exist on the Vue instance'
15+
const wrapper = mount(ComponentWithComputed)
16+
expect(() => wrapper.setComputed({ noExist: '' })).throw(Error, message)
17+
})
18+
19+
it('throws an error if node is not a Vue instance', () => {
20+
const message = 'wrapper.setComputed() can only be called on a Vue instance'
21+
const compiled = compileToFunctions('<div><p></p></div>')
22+
const wrapper = mount(compiled)
23+
const p = wrapper.find('p')
24+
expect(() => p.setComputed({ ready: true })).throw(Error, message)
25+
})
26+
})

test/unit/specs/wrappers/error-wrapper.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ describe('ErrorWrapper', () => {
113113
expect(() => error.text()).to.throw().with.property('message', message)
114114
})
115115

116+
it('setComputed throws error when called', () => {
117+
const selector = 'div'
118+
const message = `[vue-test-utils]: find did not return ${selector}, cannot call setComputed() on empty Wrapper`
119+
const error = new ErrorWrapper(selector)
120+
expect(() => error.setComputed()).to.throw().with.property('message', message)
121+
})
122+
116123
it('setData throws error when called', () => {
117124
const selector = 'div'
118125
const message = `[vue-test-utils]: find did not return ${selector}, cannot call setData() on empty Wrapper`

test/unit/specs/wrappers/wrapper-array.spec.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,16 @@ describe('WrapperArray', () => {
206206
expect(() => wrapperArray.text()).to.throw().with.property('message', message)
207207
})
208208

209-
it('setData calls setMethods on each wrapper', () => {
209+
it('setComputed calls setMethods on each wrapper', () => {
210+
const setComputed = sinon.stub()
211+
const computed = {}
212+
const wrapperArray = new WrapperArray([{ setComputed }, { setComputed }])
213+
wrapperArray.setComputed(computed)
214+
expect(setComputed.calledTwice).to.equal(true)
215+
expect(setComputed.calledWith(computed)).to.equal(true)
216+
})
217+
218+
it('setMethods calls setMethods on each wrapper', () => {
210219
const setMethods = sinon.stub()
211220
const methods = {}
212221
const wrapperArray = new WrapperArray([{ setMethods }, { setMethods }])

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface BaseWrapper {
4343
isVueInstance (): boolean
4444

4545
update (): void
46+
setComputed (computed: object): void
4647
setData (data: object): void
4748
setMethods (data: object): void
4849
setProps (props: object): void

types/test/wrapper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const emittedByOrder = wrapper.emittedByOrder()
3030
const name: string = emittedByOrder[0].name
3131

3232
wrapper.update()
33+
wrapper.setComputed({computedProp: true})
3334
wrapper.setData({ foo: 'bar' })
3435
wrapper.setMethods({checked: true})
3536
wrapper.setProps({ checked: true })

0 commit comments

Comments
 (0)