-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from vuejs/add-set-data
Add setData
- Loading branch information
Showing
4 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { defineComponent, ref } from 'vue' | ||
|
||
import { mount } from '../src' | ||
|
||
describe('setData', () => { | ||
it('sets component data', async () => { | ||
const Component = { | ||
template: '<div>{{ foo }}</div>', | ||
data: () => ({ foo: 'bar' }) | ||
} | ||
|
||
const wrapper = mount(Component) | ||
expect(wrapper.html()).toContain('bar') | ||
|
||
await wrapper.setData({ foo: 'qux' }) | ||
expect(wrapper.html()).toContain('qux') | ||
}) | ||
|
||
it('causes nested nodes to re-render', async () => { | ||
const Component = { | ||
template: `<div><div v-if="show" id="show">Show</div></div>`, | ||
data: () => ({ show: false }) | ||
} | ||
|
||
const wrapper = mount(Component) | ||
|
||
expect(wrapper.find('#show').exists()).toBe(false) | ||
|
||
await wrapper.setData({ show: true }) | ||
|
||
expect(wrapper.find('#show').exists()).toBe(true) | ||
}) | ||
|
||
it('updates a single property of a complex object', async () => { | ||
const Component = { | ||
template: `<div>{{ complexObject.string }}. bar: {{ complexObject.foo.bar }}</div>`, | ||
data: () => ({ | ||
complexObject: { | ||
string: 'will not change', | ||
foo: { | ||
bar: 'old val' | ||
} | ||
} | ||
}) | ||
} | ||
|
||
const wrapper = mount(Component) | ||
|
||
expect(wrapper.html()).toContain('will not change. bar: old val') | ||
|
||
await wrapper.setData({ | ||
complexObject: { | ||
foo: { | ||
bar: 'new val' | ||
} | ||
} | ||
}) | ||
|
||
expect(wrapper.html()).toContain('will not change. bar: new val') | ||
}) | ||
|
||
it('does not set new properties', async () => { | ||
jest.spyOn(console, 'warn').mockImplementationOnce(() => {}) | ||
|
||
const Component = { | ||
template: `<div>{{ foo || 'fallback' }}</div>` | ||
} | ||
|
||
const wrapper = mount(Component) | ||
|
||
expect(wrapper.html()).toContain('fallback') | ||
|
||
expect(() => wrapper.setData({ foo: 'bar' })).toThrowError( | ||
'Cannot add property foo' | ||
) | ||
}) | ||
|
||
it('does not modify composition API setup data', async () => { | ||
const Component = defineComponent({ | ||
template: `<div>Count is: {{ count }}</div>`, | ||
setup: () => ({ count: ref(1) }) | ||
}) | ||
const wrapper = mount(Component) | ||
|
||
expect(wrapper.html()).toContain('Count is: 1') | ||
|
||
expect(() => wrapper.setData({ count: 2 })).toThrowError( | ||
'Cannot add property count' | ||
) | ||
}) | ||
}) |