I am writing unit tests for my vuejs components in a larger application. My application state is in vuex repository, so almost all of my components retrieve data from vuex.
I cannot find a working example for writing unit test for this. I found
Where Evan says:
When the module tests the component separately, you can insert the mocked repository directly into the component using the store option.
But I cannot find a good example of how to test these components. I have tried many ways. Below I see how people do it. stack overflow
It basically looks like
test.vue:
<template> <div>test<div> </template> <script> <script> export default { name: 'test', computed: { name(){ return this.$store.state.test; } } } </script>
test.spec.js
import ... const store = new Vuex.Store({ state: { test: 3 } }); describe('test.vue', () => { it('should get test from store', () => { const parent = new Vue({ template: '<div><test ref="test"></test></div>', components: { test }, store: store }).$mount(); expect(parent.$refs.test.name).toBe(3); }); }
Note that "ref" this example does not work without it.
Is this really the right way to do this? It seems like it will be messy because it requires adding props to the template as a string.
The Evan quote seems to imply that the store can be added directly to the child component (i.e. not to the parent component, as an example).
How do you do this?
Patrick_finucane
source share