我正试图编写一个测试来判断当用户点击"登录"按钮时,URL是否被重定向到/auth/
.前端是用Vue编写的.js和测试是用Jest完成的.
以下是Vue组件如何重定向(从UserLogged.vue
开始).它在浏览器中工作.
export default {
name: 'UserLogged',
props: ['userName'],
methods: {
login: function (event) {
window.location.href = '/auth/'
}
}
}
下面是测试它的try :
import Vue from 'vue'
import UserLogged from '@/components/UserLogged'
describe('UserLogged.vue', () => {
it('should redirect anonymous users to /auth/ when clicking on login button', () => {
const Constructor = Vue.extend(UserLogged)
const vm = new Constructor().$mount()
const button = vm.$el.querySelector('button')
// Simulate click event
// Note: the component won't be listening for any events, so we need to manually run the watcher.
const clickEvent = new window.Event('click')
button.dispatchEvent(clickEvent)
vm._watcher.run()
expect(window.location.href).toEqual('http://testserver/auth/')
})
})
测试输出给出"http://testserver/"
而不是预期的"http://testserver/auth"
.