Skip to content

Commit ca44136

Browse files
committed
Possible feature request
1 parent 807d3c8 commit ca44136

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

packages/test-utils/src/wrapper.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,17 @@ export default class Wrapper implements BaseWrapper {
407407
return _props || {} // Return an empty object if no props exist
408408
}
409409

410+
/**
411+
* Sets vm data object while preserving existing k/v
412+
*/
413+
setDataObj (obj: string, key: string, value: any) {
414+
if (obj === undefined) {
415+
this.vm.$set(this.vm, key, value)
416+
} else {
417+
this.vm.$set(obj, key, value)
418+
}
419+
}
420+
410421
/**
411422
* Sets vm data
412423
*/

test/specs/wrapper/setDataObj.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
describeWithShallowAndMount,
3+
vueVersion
4+
} from '~resources/utils'
5+
6+
describeWithShallowAndMount('setDataObj', (mountingMethod) => {
7+
let info
8+
9+
beforeEach(() => {
10+
info = sinon.stub(console, 'info')
11+
})
12+
13+
afterEach(() => {
14+
info.restore()
15+
})
16+
it('should allow for setting a data value', () => {
17+
const TestComponent = {
18+
data: () => ({
19+
text: 'hello'
20+
})
21+
}
22+
const wrapper = mountingMethod(TestComponent)
23+
wrapper.setDataObj(wrapper.vm, 'text', 'goodbye')
24+
expect(wrapper.vm.text).to.equal('goodbye')
25+
})
26+
it('should allow for setting a data object', () => {
27+
const TestComponent = {
28+
data: () => ({
29+
message: {
30+
text: 'hello'
31+
}
32+
})
33+
}
34+
const wrapper = mountingMethod(TestComponent)
35+
wrapper.setDataObj(wrapper.vm.message, 'read', true)
36+
expect(wrapper.vm.message.text).to.equal('hello')
37+
expect(wrapper.vm.message.read).to.equal(true)
38+
})
39+
})

0 commit comments

Comments
 (0)