From 0b64040a9ff17b17a761c7b26bd822806ef1e5c9 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Sat, 11 Apr 2020 16:31:06 +1000 Subject: [PATCH] feat: setData --- package.json | 4 +++ src/vue-wrapper.ts | 10 ++++++- tests/setData.spec.ts | 67 +++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 5 ++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 tests/setData.spec.ts diff --git a/package.json b/package.json index 779969fbc..aaaa1bcc9 100644 --- a/package.json +++ b/package.json @@ -56,5 +56,9 @@ "*.ts": [ "prettier --parser=typescript --write" ] + }, + "dependencies": { + "@types/lodash": "^4.14.149", + "lodash": "^4.17.15" } } diff --git a/src/vue-wrapper.ts b/src/vue-wrapper.ts index 93f99ff03..cdae6c634 100644 --- a/src/vue-wrapper.ts +++ b/src/vue-wrapper.ts @@ -1,5 +1,6 @@ -import { ComponentPublicInstance } from 'vue' +import { ComponentPublicInstance, nextTick } from 'vue' import { ShapeFlags } from '@vue/shared' +import merge from 'lodash/merge' import { DOMWrapper } from './dom-wrapper' import { WrapperAPI } from './types' @@ -78,6 +79,13 @@ export class VueWrapper implements WrapperAPI { return Array.from(results).map((x) => new DOMWrapper(x)) } + setData(data: Record) { + // lodash.merge merges by *reference* so this will update + // any existing data with the newly passed data. + merge(this.componentVM.$data, data) + return nextTick() + } + trigger(eventString: string) { const rootElementWrapper = new DOMWrapper(this.element) return rootElementWrapper.trigger(eventString) diff --git a/tests/setData.spec.ts b/tests/setData.spec.ts new file mode 100644 index 000000000..d88436cdf --- /dev/null +++ b/tests/setData.spec.ts @@ -0,0 +1,67 @@ +import { mount } from '../src' + +describe('setData', () => { + it('causes nested nodes to re-render', async () => { + const Comp = { + template: `
Show
`, + data() { + return { show: false } + } + } + + const wrapper = mount(Comp) + expect(wrapper.find('#show').exists()).toBe(false) + + await wrapper.setData({ show: true }) + + expect(wrapper.find('#show').exists()).toBe(true) + }) + + it('runs watch function when data is updated', async () => { + const Comp = { + template: `
{{ sideEffect }}
`, + data() { + return { msg: '', sideEffect: '' } + }, + watch: { + msg() { + this.sideEffect = 'side effect' + } + } + } + const wrapper = mount(Comp) + expect(wrapper.html()).not.toContain('side effect') + + await wrapper.setData({ msg: 'foo' }) + + expect(wrapper.html()).toContain('side effect') + }) + + it('updates a single property of a complex object', async () => { + const Comp = { + template: `
Qux: {{ foo.qux }}. Baz: {{ foo.bar.baz }}
`, + data() { + return { + foo: { + qux: 'will not change', + bar: { + baz: 'old val' + } + } + } + } + } + const wrapper = mount(Comp) + expect(wrapper.html()).toBe('
Qux: will not change. Baz: old val
') + + await wrapper.setData({ + foo: { + bar: { + baz: 'new val' + } + } + }) + + expect(wrapper.html()).toBe('
Qux: will not change. Baz: new val
') + }) +}) diff --git a/yarn.lock b/yarn.lock index 784402de1..c4f4bd225 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1151,6 +1151,11 @@ dependencies: jest-diff "^24.3.0" +"@types/lodash@^4.14.149": + version "4.14.149" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.149.tgz#1342d63d948c6062838fbf961012f74d4e638440" + integrity sha512-ijGqzZt/b7BfzcK9vTrS6MFljQRPn5BFWOx8oE0GYxribu6uV+aA9zZuXI1zc/etK9E8nrgdoF2+LgUw7+9tJQ== + "@types/node@*": version "13.7.4" resolved "https://registry.yarnpkg.com/@types/node/-/node-13.7.4.tgz#76c3cb3a12909510f52e5dc04a6298cdf9504ffd"