|
| 1 | +import {mount, shallowMount} from "@vue/test-utils"; |
| 2 | +import TestComponent from '@/test.vue' |
| 3 | +import ListComponent from '@/list.vue' |
| 4 | + |
| 5 | +/*When testing vue.js components, need a way to mount and render the component; use the mount function. Mount |
| 6 | +creates a Wrapper containing the mounted and rendered Vue component. */ |
| 7 | +it('should mount a vue TestComponent', () => { |
| 8 | + const wrapper = mount(TestComponent); |
| 9 | + console.log(wrapper); |
| 10 | + expect(wrapper.text()).toContain("Hello Test"); |
| 11 | + |
| 12 | + //Wrapper HTML function returns the entire component DOM node as a string. toMatchSnapshot() creates a snapshot |
| 13 | + // of the component in the __snapshots__ dir. To learn more about snapshots, check out Snapshot Testing with Jest |
| 14 | + // at Vue School, but this class is not free. |
| 15 | + //debugger; |
| 16 | + expect(wrapper.html()).toMatchSnapshot(); |
| 17 | + console.log(wrapper.html()); |
| 18 | + expect(wrapper.html()).toContain("<h1>Hello Test</h1>"); |
| 19 | +}); |
| 20 | + |
| 21 | +it('should override the default value to say Hello to in the TestComponent', () => { |
| 22 | + const wrapper = mount(TestComponent, |
| 23 | + { |
| 24 | + propsData: {value: 'VueSchool'} |
| 25 | + }); |
| 26 | + expect(wrapper.text()).toContain("Hello VueSchool"); |
| 27 | + |
| 28 | + expect(wrapper.html()).toMatchSnapshot(); |
| 29 | + console.log(wrapper.html()); |
| 30 | + expect(wrapper.html()).toContain("<h1>Hello VueSchool</h1>"); |
| 31 | +}); |
| 32 | + |
| 33 | +it('should shallowMount the ListComponent', () => { |
| 34 | + // shallowMount() and mount() are almost the same except child components of the one comp u shallowMounted get |
| 35 | + // stubbed automatically. Use mount() as much as pos as the default approach as it helps to find issues in child |
| 36 | + // comps early. |
| 37 | + console.log("Log of mount(ListComponent): " + mount(ListComponent).html()); |
| 38 | + const wrapper = shallowMount(ListComponent); |
| 39 | + console.log("Log of shallowMount(ListComponent): " + wrapper.html()); |
| 40 | + //shallowMount() should stub out the list items in the list |
| 41 | + expect(wrapper.html()).toContain('<listitem-stub movie="Iron Man"></listitem-stub>'); |
| 42 | +}); |
| 43 | + |
| 44 | +//Wrapper properties --> most important one is VM. It gives access to the Vue instance, the same instance you could |
| 45 | +// interact w/ from the browser console. |
| 46 | +it('should add a list item to the ListComponent', () => { |
| 47 | + const wrapper = mount(ListComponent); |
| 48 | + //Need to get the current set of data in list.vue, which we can get from the wrapper's VM property: |
| 49 | + //let movies = wrapper.vm.marvelMovies; |
| 50 | + |
| 51 | + //Now, use setData() to update the actual data set. |
| 52 | + wrapper.setData({marvelMovies: [...wrapper.vm.marvelMovies, 'Endgame']}); |
| 53 | + //this above does update the data set/list, but not in the snapshot file (test.spec.js.snap) |
| 54 | + //can see the update in the following log |
| 55 | + console.log("marvelMovies: " + wrapper.vm.marvelMovies); |
| 56 | + |
| 57 | + expect(wrapper.vm.marvelMovies).toContain('Endgame'); |
| 58 | + expect(wrapper.html()).toMatchSnapshot(); |
| 59 | +}); |
| 60 | + |
0 commit comments