Skip to content

Commit c27a27c

Browse files
committed
* Added bunch of practice tests in test.spec.js * Removed other spec files to dir outside of project b/c getting in way when run tests w/ npm from cmd line
1 parent a33f5be commit c27a27c

12 files changed

+65
-26
lines changed

jest.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ module.exports = {
77
},
88
transform: {
99
"^.+\\.js$": "babel-jest",
10+
"^.+\\.vue$": "vue-jest"//tell Jest that every file ending in .vue should run thru vue-jest
1011
},
1112
snapshotSerializers: [
1213
"<rootDir>/node_modules/jest-serializer-vue"
1314
]
14-
}
15+
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"babel-jest": "^24.5.0",
1717
"jest": "^24.5.0",
1818
"jest-serializer-vue": "^2.0.2",
19-
"vue": "^2.6.9",
19+
"vue": "^2.6.12",
2020
"vue-jest": "^3.0.7",
2121
"vue-template-compiler": "^2.6.12",
2222
"vuex": "^3.1.1"

specs/alert.spec.js

-2
This file was deleted.

specs/exercise-1.spec.js

-2
This file was deleted.

specs/exercise-2.spec.js

-2
This file was deleted.

specs/fruits.spec.js

-2
This file was deleted.

specs/github.spec.js

-6
This file was deleted.

specs/salad.spec.js

-6
This file was deleted.

specs/stubs.spec.js

Whitespace-only changes.

specs/temp.spec.js

-2
This file was deleted.

specs/test.spec.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+

src/list.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
marvelMovies: [
2525
'Iron Man',
2626
'Avengers',
27-
'Infitinity War',
27+
'Infinity War',
2828
]
2929
}
30-
}
30+
}//end data()
3131
3232
}
3333
</script>

0 commit comments

Comments
 (0)