Currently, I am working with Vuex in a TypeScript configuration and facing challenges while attempting to unit test an action due to difficulty in setting the this
parameter of the action method.
The action looks something like this:
export const login: ActionHandler<AuthState, RootState> = ({ commit },
creds: { email: string, password: string }) => {
};
Here is how the test is structured:
describe('actions', () => {
test('login should call API with credentials', () => {
(ApiService.post as jest.Mock).mockResolvedValue({
data: { userId: 1234 }
});
const commit = jest.fn();
login({ commit }, { email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="13666076617d727e7653747e727a7a3a767077">[email protected]</a>', password: 'Pa$$w0rd123' });
});
});
The issue arises from the fact that the ActionHandler
has a signature like:
login(this: Store<RootState>, injectee: ActionContext<AuthState, RootState>, payload?: any): any
.
As a result, TypeScript presents the following error message:
The 'this' context of type 'void' is not assignable to method's 'this' of type 'Store<RootState>'.ts(2684)
I am struggling to determine how to assign an instance of Store<RootState>
as the this
of the login action. Could it be possible that I am approaching this incorrectly? While I understand that this problem might not occur if I were using vanilla JS, my preference is to continue utilizing TypeScript if feasible.