I need to verify if my 'SIGN_IN' action is functioning correctly.
This is how my action currently looks (not the most refined implementation but it's what I have at the moment):
// actions.js
import {
SIGN_IN, SIGN_OUT, SET_AUTH_TOKEN, CLEAR_AUTH_TOKEN, USER_AUTHORISED,
} from '@/store/types';
import AuthService from '../../../authService';
export default {
async [SIGN_IN]({ commit, rootState }) {
const authService = new AuthService(rootState.clientSettings);
let response;
try {
response = await authService.getToken();
} catch (e) {
console.log('User not authorised');
}
if (response) {
commit(SET_AUTH_TOKEN, response);
} else {
commit(USER_AUTHORISED, false);
}
},
...
};
authService.getToken();
is the method I want to mock the response of in my test.
I want to be able to simulate a mock response for this in my testing scenario.
The structure of the AuthService class is as follows:
// authService.js
import { PublicClientApplication } from '@azure/msal-browser';
class AuthService {
constructor(clientSettings) {
// Constructor logic
}
...
async getToken() {
// Method logic
}
...
}
export default AuthService;
This is my attempted test script:
// actions.spec.js
// Testing code
During debugging, I am encountering difficulty in stubbing the response for getToken
. It continues to call the actual instance of AuthService
created within my action.
My goal is to simply confirm that SET_AUTH_TOKEN
is being called and nothing more. How can I effectively replace the entire AuthService
class with a stub so that my action utilizes that one instead? Is there a more efficient way to achieve this without passing an instance of AuthService
into the action itself?