带Jest和Vue测试UTIT的单位测试VUE
我正在与Vue一起进行单位测试。 我正在尝试单元测试导航VUE组件。我开始简单,我只是想测试一种仅制成布尔值false的方法(由单击触发)。我也在使用Vuetify。我在某种程度上设法模仿了一个按钮,然后我希望我通过的变量(Showlogindrawer)在我的单元测试中返回false。我的错误是typeError:_vm.is.is.s.不是一个与代码不同部分有关的函数,我认为我缺少一些东西,但我在网上找不到任何东西。
下面我包括了组件和测试文件。
//Nav.vue
<template>
<v-btn
:to="{ name: 'home' }"
aria-label="Home button"
@click="hideDiv()"
>
</v-btn>
<div>
<transition name="nav-fade">
<component v-bind:is="drawerSection"></component>
</transition>
</div>
</template>
<script>
data() {
return {
drawerSection: function () {
if (this.is.has('learning')) {
return 'nav-learning'
} else {
return 'nav-explore'
}
},
}
},
methods: {
hideDiv() {
if (!this.appState.restrictedAccessAlert)
this.appState.showLoginDrawer = false
}
}
</script>
<style>//disregard</style>
//Nav.spec.js
import Vue from 'vue'
import Vuex from 'vuex'
import Vuetify from 'vuetify'
import { shallowMount, mount } from '@vue/test-utils'
import Nav from '@/components/Nav'
const localVue = createLocalVue()
localVue.use(Vuetify)
localVue.use(Vuex)
describe('Testing Nav component', () => {
let vuetify
beforeEach(() => {
vuetify = new Vuetify()
})
test('Test Button click when logged out', () => {
const wrapper = mount(Navigation, {
data() {
return {
appState: {
showLoginDrawer: true,
lastCapabilityPath: '',
restrictedAccessAlert: false,
},
is: 'learning',
localVue,
vuetify,
}
},
})
const event = jest.fn()
const button = wrapper.findAll('.v-btn').at(0)
expect(button.text()).toContain('My Profile')
button.vm.$on('click', event)
expect(event).toHaveBeenCalledTimes(0)
expect(wrapper.vm.showLoginDrawer).toBeTruthy()
button.trigger('click')
expect(event).toHaveBeenCalledTimes(1)
expect(wrapper.vm.showLoginDrawer).toBeFalsy()
})
})
我收到错误:
typeError:_vm.is.is.has不是函数
感谢您的任何帮助!
I'm brand new at Unit Testing with vue.
I'm trying to unit test a navigation vue component. I started out easy, I just wanted to test a method that just makes a boolean false(triggered by an on click). I am also using vuetify. I somewhat managed to mimic a button and then my hope is that the variable I'm passing (showLoginDrawer) returns false in my unit test. My error is TypeError: _vm.is.has is not a function which relates to a different part of my code, I think I'm missing something but I couldn't find anything online about it.
Below I've included the component and my testing file.
//Nav.vue
<template>
<v-btn
:to="{ name: 'home' }"
aria-label="Home button"
@click="hideDiv()"
>
</v-btn>
<div>
<transition name="nav-fade">
<component v-bind:is="drawerSection"></component>
</transition>
</div>
</template>
<script>
data() {
return {
drawerSection: function () {
if (this.is.has('learning')) {
return 'nav-learning'
} else {
return 'nav-explore'
}
},
}
},
methods: {
hideDiv() {
if (!this.appState.restrictedAccessAlert)
this.appState.showLoginDrawer = false
}
}
</script>
<style>//disregard</style>
//Nav.spec.js
import Vue from 'vue'
import Vuex from 'vuex'
import Vuetify from 'vuetify'
import { shallowMount, mount } from '@vue/test-utils'
import Nav from '@/components/Nav'
const localVue = createLocalVue()
localVue.use(Vuetify)
localVue.use(Vuex)
describe('Testing Nav component', () => {
let vuetify
beforeEach(() => {
vuetify = new Vuetify()
})
test('Test Button click when logged out', () => {
const wrapper = mount(Navigation, {
data() {
return {
appState: {
showLoginDrawer: true,
lastCapabilityPath: '',
restrictedAccessAlert: false,
},
is: 'learning',
localVue,
vuetify,
}
},
})
const event = jest.fn()
const button = wrapper.findAll('.v-btn').at(0)
expect(button.text()).toContain('My Profile')
button.vm.$on('click', event)
expect(event).toHaveBeenCalledTimes(0)
expect(wrapper.vm.showLoginDrawer).toBeTruthy()
button.trigger('click')
expect(event).toHaveBeenCalledTimes(1)
expect(wrapper.vm.showLoginDrawer).toBeFalsy()
})
})
I get the error:
TypeError: _vm.is.has is not a function
Thank you for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦,我的错误是 has() 是一个集合函数,因此它不适用于字符串,所以我必须使其等于集合。
更新的代码:
Oh my mistake was that has() is a set function thus it won't work with a string so I had to make it equal to a set.
Updated code:
请尝试使用
async
&await
,也许每个事件调用都是异步的Please try use
async
&await
, maybe every event call is asynchronous