How to test components using getModule with vue-test-util
See original GitHub issueHow would you go about stubbing actions and other methods inside of your store when using the getModule functionality inside of a component for testing?
MyComponent.vue
<template>
<div>
{{ this.exampleStore.someData }}
<button v-on:click="handleClick()">Do An Action</button>
<LoadingIcon
:v-if="this.exampleStore.isLoading"
></LoadingIcon>
</div>
</template>
<script lang="ts">
// imports.....
@Component({
components: {
LoadingIcon
}
})
export default class MyComponent extends Vue {
private exampleStore: ExampleStore = getModule(ExampleStore)
private created() {
this.exampleStore.fetchSomeData()
}
private handleClick() {
this.exampleStore.fetchSomeData()
}
}
</script>
ExampleStore.ts
// imports...
@Module({ dynamic: true, namespaced: true, store, name: 'exampleStore' })
export default class ExampleStore extends VuexModule {
public someData: any = ''
@Action({ commit: 'someMutation')}
public async fetchSomeData() {
// async stuff
return data
}
@Mutation
public someMutation(payload: any) {
return this.someData = payload
}
}
Test
const localVue = createLocalVue()
localVue.use(Vuex)
let store: Vuex.Store<any>
beforeEach(() => {
store = new Vuex.Store({})
})
describe('test component', () => {
it('should have the correct html structure', () => {
const component = shallowMount(MyComponent, {
store, localVue
} as any)
expect(component).toMatchSnapshot()
})
})
In the above example I would need to stub the fetchSomeData action
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:22
Top Results From Across the Web
How To Test A Vuex Module Action Is Called In Vue ... - ADocLib
One approach to testing a Vuex store is to test the store parts separately.The benefit of testing parts separately is that the unit...
Read more >Using with Vuex - Vue Test Utils
Using with Vuex. In this guide, we'll see how to test Vuex in components with Vue Test Utils, and how to approach testing...
Read more >How to use Jest with vuex-module-decorators - Stack Overflow
My code works well but when i try to write a unit test using vue-test-utils and jest i'm not able to inject my...
Read more >Things You Might Not Know About Vue-Test-Utils - WebDevEtc
Another common thing to see in tests which use nextTick() after a trigger() : async function someTest() { wrapper = mount(Component); ...
Read more >Top 5 @vue/test-utils Code Examples - Snyk
Learn more about how to use @vue/test-utils, based on @vue/test-utils code examples ... Vue }); // same component, no props, should match in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Is there still not a better solution or any documentation related to this ? We’re nearing 2021 and this issue is so far still the best source of documentation I can find on testing dynamic Vuex modules.
sorry for the delay! been a bit busy 😕
so lets assume you use
getModule(FooModule, this.$store)
from inside a component computed property likethen all you have to do is override
fooModule
in your computed attributes when creating your wrapper, i.e.You can also return your own mock version of the store if you like. personally I don’t like testing store logic in my components so I override it with an empty object, and mock the other getters that I use to access the store getters, and methods used to access the store mutations and actions.