question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use unit test with vuex-loading

See original GitHub issue

I would like to write a unit test with the async action such as: File navigation.spec.js: `

import Vue from ‘vue’
import Vuex from ‘vuex’
import Vuetify from ‘vuetify’
import vueLoading from ‘vuex-loading’
import { shallow, createLocalVue } from ‘@vue/test-utils’
import Navigation from ‘@/components/Navigation’
import { stat } from ‘fs’;

Vue.use(Vuetify) Vue.use(vueLoading)

const localVue = createLocalVue() localVue.use(Vuex)

describe(‘Navigation’, () => { let store let actions let state

beforeEach(() => { state = { items: [] }

actions = {
  getAsync: jest.fn()
}

store = new Vuex.Store({
  state,
  actions
})

})

it(‘dispatches an getAsync action’, () => { const wrapper = shallow(Navigation, { store, localVue }) wrapper.find(‘button’).trigger(‘click’) expect(actions.getAsync).toHaveBeenCalled() }) })

`

File Navigation.vue: `

<template>

<div class="multipane__aside"> <v-btn :disabled="$loading.isLoading('call ajax')" @click="fetchResults" color="primary" > <v-loading loader='call ajax'> <template slot='spinner'>Loading...</template> </v-loading> Call ajax </v-btn> <div v-if="$loading.isLoading('call ajax')">call ajax...</div>
  • {{ item.title }}
    {{ item.body }}
</div> </template> <script> import { mapState, mapActions } from 'vuex' export default { name: 'RecipeNavigation', computed: { ...mapState(['items']) }, methods: { ...mapActions(['getAsync']), fetchResults () { this.getAsync() }, onClose () { this.$emit('close', this.$el) } } } </script>
`

The result of test: [Vue warn]: Error in config.errorHandler: “TypeError: Cannot read property ‘isLoading’ of undefined”

Please help me to pass this issue. Thank you for any help you can offer

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fcommented, Sep 27, 2018

Hi @gabrielrobert, would you help to add a “testing” part into README.md file according to your testing experiences and problems? I need to improve vue-wait to make it more testable. It would be awesome for the people asking for tests (including me) 😃

1reaction
gabrielrobertcommented, Sep 27, 2018

In some cases, we cannot mock v-wait. This library is widely used in our project and stubs/mocks prevent proper testability. For instance:

Component:

<v-wait for="loading">
   <Spinner slot="waiting" />
   <ul class="suggestions" v-if="suggestions">
      <li v-for="...">Bleh</li>
   </ul>
</v-wait>

Test:

it('shows suggestions', async () => {
    const wrapper = shallowMount(SuggestionsComponent, { localVue });
    expect(wrapper.find('.suggestions').exists()).toBe(true);
});

It is not possible to test proper ul rendering without v-wait component. You should:

const localVue = createLocalVue();
localVue.use(Vuex);
localVue.use(VueWait);

it('shows suggestions', async () => {
    const wrapper = shallowMount(SuggestedAddresses, {
      localVue,
      wait: new VueWait()
    });
    expect(wrapper.find('.suggestions').exists()).toBe(true);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing | Vuex
The main parts we want to unit test in Vuex are mutations and actions. ... we can use webpack and inject-loader to bundle...
Read more >
How to unit test a Vuex store - Dev tips and tricks
Tutorial on how two different approaches to unit testing a Vuex store. ... Mutations take a state object and an optional payload object....
Read more >
Unit testing Vuex modules with Jest - LogRocket Blog
In this article, I'll guide you through implementing a Vuex module in TypeScript, then unit testing it using Jest. The complete code for ......
Read more >
Guide to Unit Testing Vue Components - TestDriven.io
Use mocks to test asynchronous functions; Check the code coverage of your unit tests; Structure your unit test files. If you're interested in ......
Read more >
Structuring and testing a Vue/Vuex app with vue-test-utils
TodoContainer — Failing Test · We import vue , vuex , and do Vue.use(Vuex . · Before each test, we mock what the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found