Answer the question
In order to leave comments, you need to log in
Jest vue utils test how to test axios requests?
Hello!
Login Component
<template lang="html">
<div class="auth">
<div class="auth__container">
<input
class='auth__input'
v-model='login'
/>
<input
class='auth__input'
v-model='password'
/>
<button
class='auth__button'
@click='entry'
>entry</button>
<span
v-if='error'
class='auth__error'
>{{ error }}</span>
<span
v-if='success'
class='auth__success'
>{{ success }}</span>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
login: '',
password: '',
error: '',
success: '',
rest: {}
}
},
methods: {
async entry() {
let {
data
} = await axios.get('login.js')
this.rest = data
}
}
}
</script>
import { mount } from '@vue/test-utils'
import Login from '@/components/Login.vue'
describe('Login', () => {
it('toEq', async () => {
const wrapper = mount(Login)
wrapper.find('button').trigger('click')
return expect(wrapper.vm.rest).toBe('value')
})
})
import { mount } from '@vue/test-utils'
import Login from '@/components/Login.vue'
describe('Login', () => {
it('toEq', done => {
const wrapper = mount(Login)
wrapper.find('button').trigger('click')
wrapper.vm.$nextTick(() => {
expect(wrapper.vm.rest).toBe('value')
done()
})
})
})
Answer the question
In order to leave comments, you need to log in
Mock the method and check if it is called on click.
Unit testing requests is a bad idea.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question