While working on a test, I ran into an issue. Despite double-checking everything, I can't seem to call the function correctly. Can anyone please assist me with this?
The code in index.vue looks like this:
import { shallowMount, createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import Login from '../index'
const localVue = createLocalVue()
localVue.use(Vuex)
describe('Testing the login page', () => {
let actions
let store
let loginData = {username: 'user1', password: '123'}
beforeEach (() => {
actions = {
handleLogin: jest.fn(),
login: jest.fn()
}
store = new Vuex.Store({
actions
})
})
it('Sending the login action', () => {
const wrapper = shallowMount(Login, {
computed: {
loginBtn: () => true,
loginStatus: () => '',
showLogoutEverywhereBtn: () => false
},
store,
localVue
})
wrapper.find('.main-form__login-submit').trigger('click')
expect(actions.handleLogin).toHaveBeenCalled()
})
});
This is how the function in index.vue looks:
...
methods: {
handleLogin(e) {
e.preventDefault()
this.$store.dispatch('login', this.loginForm)
.then((response) => {
console.log('login page response: ', response)
if (response.id_user !== undefined) {
this.$router.push({ path: '/' })
}
})
.catch((e) => {
console.log('Error inside: ', e);
});
},
...
Looking at my user.js file:
...
actions: {
// handling user login
login({ commit }, userInfo) {
console.log('Information in store/modules/user.js: ', userInfo)
const { username, password } = userInfo
return new Promise((resolve, reject) => {
login({ user_login: username.trim(), user_password: password }).then(response => {
const decoded = jwtDecode(response)
console.log(decoded)
/***********************SOCKET.IO***************************/
let data = { userId: decoded.id_user, page: 'login' }
socketUrl.emit('authorize', data, (flag) => {
console.log('Main authorize here.')
console.log('This is store token id:', decoded.id_user)
console.log(flag)
if (decoded.status === 0) {
commit('CHANGE_SELECTED_USER_STATUS_ID', 392)
console.log(decoded.full_name)
// commit('SET_TOKEN', decoded)
commit('AUTH_SUCCESS', decoded)
if (flag) {
resolve(decoded)
console.log('This is decoded: ', decoded.statuses[0])
getStatusesArray().then(response => {
console.log('This is presencestatus data:', response)
commit('UPDATE_STATUS', response)
})
} else {
commit('AUTH_ERROR')
}
resolve()
} else {
commit('AUTH_ERROR')
}
})
socketUrl.on('kick', (message) => {
console.log('socket: ', message)
commit('DUPLICATE_LOGIN', message.text)
})
socketUrl.on('message', () => {
commit('AUTH_ERROR')
})
/***********************SOCKET.IO***************************/
}).catch(error => {
console.log('Error in store/modules/user.js login: ', error)
commit('AUTH_ERROR')
reject(error)
})
})
},
logout(state, payload) {
state = null
console.log('User logged out with userId: ', payload)
socketUrl.emit('disconnectUser', { userId: payload })
return new Promise ((resolve, reject) => {
logout({ userId: payload })
.then(response => {
console.log('logout function', response)
resolve(response)
})
.catch((e) => {
console.log('logout error: ', e)
reject(e)
})
})
},
...
Any help would be greatly appreciated. This testing challenge has been consuming a lot of my time, especially since I'm still new to it. It's possible that I'm missing something related to passing parameters to the function. Thank you in advance!