Skip to content

Commit 3d398c4

Browse files
committed
* temperature.spec.js: Test for Watched property for temperature does not work the way Vue states it should. Has an open bug: vuejs/vue-test-utils#1419 * Commented out test. Skipping does not work well with output
1 parent ee6a7a1 commit 3d398c4

File tree

3 files changed

+82
-53
lines changed

3 files changed

+82
-53
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"main": "index.js",
55
"scripts": {
6-
"jest": "jest"
6+
"test": "jest"
77
},
88
"repository": "https://github.com/codebryo/vue-school-test-utils-base.git",
99
"author": "Roman Kuba <[email protected]>",

specs/temperature.spec.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Temperature from '@/temperature'
2-
import { mount } from '@vue/test-utils'
2+
import {mount} from '@vue/test-utils'
3+
4+
describe('Computed and Watched Properties Tests', () => {
35

4-
describe('Computed Properties Tests', () => {
56
test('celsius property', () => {
67
//const wrapper = mount(Temperature);
78
const {vm} = mount(Temperature);
@@ -25,5 +26,32 @@ describe('Computed Properties Tests', () => {
2526

2627
vm.degrees = 16;
2728
expect(vm.fahrenheit).toBe(60.8);
28-
})
29+
});
30+
31+
//can suffix test with .skip or prefix with x to skip, but then lists output as pending test
32+
/*test('temperature watcher method', () => {
33+
//set the default data to be 40 degrees celsius
34+
const wrapper = mount(Temperature, {propsData: {temp: 40/!*, watch: '40c'*!/}});
35+
const {vm} = wrapper;
36+
expect(vm.degrees).toBe(40);
37+
expect(vm.type).toBe('celsius');
38+
39+
//setProps() allows you to set the props while the comp is mounted already
40+
//Sets Wrapper vm props and forces update.
41+
wrapper.setProps({temp: '50f'});//This setProps() doesn't work. Per Vue docs: "setProps could be called only for
42+
// top-level component, mounted by mount or shallowMount". To change the properties in the Vue instance, you
43+
// need to use propsData: "pass a propsData object, which will initialize the Vue instance with passed values."
44+
//wrapper.setProps({watch: '50f'});//This doesn't work either.
45+
46+
/!*wrapper.setProps({
47+
watch: {
48+
temp: {handler: '50f'}
49+
}
50+
});*!/ //this doesn't work either
51+
52+
//wrapper.setData({type: 'fahrenheit', degrees: 50}); //this works, but does not test the watched temperature property
53+
console.log("Current degrees: " + vm.degrees + " " + vm.type);
54+
expect(vm.degrees).toBe(50);
55+
expect(vm.type).toBe('fahrenheit');
56+
});*/
2957
});

specs/test.spec.js

+50-49
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,58 @@ import ListComponent from '@/list.vue'
44

55
/*When testing vue.js components, need a way to mount and render the component; use the mount function. Mount
66
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("TestComponent wrapper: " + 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("TestComponent wrapper html(): " + 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("Overriden TestComponent wrapper html(): " + 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-
});
7+
describe('TestComponent and ListComponent Tests', () => {
8+
9+
it('should mount a vue TestComponent', () => {
10+
const wrapper = mount(TestComponent);
11+
console.log("TestComponent wrapper: " + wrapper);
12+
expect(wrapper.text()).toContain("Hello Test");
13+
14+
//Wrapper HTML function returns the entire component DOM node as a string. toMatchSnapshot() creates a snapshot
15+
// of the component in the __snapshots__ dir. To learn more about snapshots, check out Snapshot Testing with Jest
16+
// at Vue School, but this class is not free.
17+
//debugger;
18+
expect(wrapper.html()).toMatchSnapshot();
19+
console.log("TestComponent wrapper html(): " + wrapper.html());
20+
expect(wrapper.html()).toContain("<h1>Hello Test</h1>");
21+
});
22+
23+
it('should override the default value to say Hello to in the TestComponent', () => {
24+
const wrapper = mount(TestComponent,
25+
{propsData: {value: 'VueSchool'}});
26+
expect(wrapper.text()).toContain("Hello VueSchool");
27+
28+
expect(wrapper.html()).toMatchSnapshot();
29+
console.log("Overriden TestComponent wrapper html(): " + 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+
});
4343

4444
//Wrapper properties --> most important one is VM. It gives access to the Vue instance, the same instance you could
4545
// 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();
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+
});
5960
});
6061

0 commit comments

Comments
 (0)