|
| 1 | +import { defineComponent, ref } from 'vue' |
| 2 | + |
| 3 | +import { mount } from '../src' |
| 4 | + |
| 5 | +describe('setData', () => { |
| 6 | + it('sets component data', async () => { |
| 7 | + const Component = { |
| 8 | + template: '<div>{{ foo }}</div>', |
| 9 | + data: () => ({ foo: 'bar' }) |
| 10 | + } |
| 11 | + |
| 12 | + const wrapper = mount(Component) |
| 13 | + expect(wrapper.html()).toContain('bar') |
| 14 | + |
| 15 | + await wrapper.setData({ foo: 'qux' }) |
| 16 | + expect(wrapper.html()).toContain('qux') |
| 17 | + }) |
| 18 | + |
| 19 | + it('causes nested nodes to re-render', async () => { |
| 20 | + const Component = { |
| 21 | + template: `<div><div v-if="show" id="show">Show</div></div>`, |
| 22 | + data: () => ({ show: false }) |
| 23 | + } |
| 24 | + |
| 25 | + const wrapper = mount(Component) |
| 26 | + |
| 27 | + expect(wrapper.find('#show').exists()).toBe(false) |
| 28 | + |
| 29 | + await wrapper.setData({ show: true }) |
| 30 | + |
| 31 | + expect(wrapper.find('#show').exists()).toBe(true) |
| 32 | + }) |
| 33 | + |
| 34 | + it('updates a single property of a complex object', async () => { |
| 35 | + const Component = { |
| 36 | + template: `<div>{{ complexObject.string }}. bar: {{ complexObject.foo.bar }}</div>`, |
| 37 | + data: () => ({ |
| 38 | + complexObject: { |
| 39 | + string: 'will not change', |
| 40 | + foo: { |
| 41 | + bar: 'old val' |
| 42 | + } |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + const wrapper = mount(Component) |
| 48 | + |
| 49 | + expect(wrapper.html()).toContain('will not change. bar: old val') |
| 50 | + |
| 51 | + await wrapper.setData({ |
| 52 | + complexObject: { |
| 53 | + foo: { |
| 54 | + bar: 'new val' |
| 55 | + } |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + expect(wrapper.html()).toContain('will not change. bar: new val') |
| 60 | + }) |
| 61 | + |
| 62 | + it('does not set new properties', async () => { |
| 63 | + jest.spyOn(console, 'warn').mockImplementationOnce(() => {}) |
| 64 | + |
| 65 | + const Component = { |
| 66 | + template: `<div>{{ foo || 'fallback' }}</div>` |
| 67 | + } |
| 68 | + |
| 69 | + const wrapper = mount(Component) |
| 70 | + |
| 71 | + expect(wrapper.html()).toContain('fallback') |
| 72 | + |
| 73 | + expect(() => wrapper.setData({ foo: 'bar' })).toThrowError( |
| 74 | + 'Cannot add property foo' |
| 75 | + ) |
| 76 | + }) |
| 77 | + |
| 78 | + it('does not modify composition API setup data', async () => { |
| 79 | + const Component = defineComponent({ |
| 80 | + template: `<div>Count is: {{ count }}</div>`, |
| 81 | + setup: () => ({ count: ref(1) }) |
| 82 | + }) |
| 83 | + const wrapper = mount(Component) |
| 84 | + |
| 85 | + expect(wrapper.html()).toContain('Count is: 1') |
| 86 | + |
| 87 | + expect(() => wrapper.setData({ count: 2 })).toThrowError( |
| 88 | + 'Cannot add property count' |
| 89 | + ) |
| 90 | + }) |
| 91 | +}) |
0 commit comments