test(auth): write basic unit tests for authStore actions

This commit is contained in:
Ilia Mashkov
2026-03-18 09:58:30 +03:00
parent 51fc64d8c3
commit fd5b50a6f2

View File

@@ -0,0 +1,123 @@
import { setupServer } from "msw/node";
import {
loginMock,
registerMock,
logoutMock,
refreshMock,
} from "../../../api/calls";
import { defaultStoreState, useAuthStore } from "./authStore";
import {
MOCK_EMAIL,
MOCK_NEW_EMAIL,
MOCK_PASSWORD,
MOCK_TOKEN,
} from "../../../api/calls/mocks";
const server = setupServer(loginMock, registerMock, logoutMock, refreshMock);
describe("authStore", () => {
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
afterEach(() => {
useAuthStore.getState().reset();
server.resetHandlers();
});
afterAll(() => server.close());
describe("reset", () => {
it("should reset the store to default state", () => {
useAuthStore.getState().reset();
expect(useAuthStore.getState()).toMatchObject({ ...defaultStoreState });
});
});
describe("login", () => {
it("should set access token, user data, and update status after successful login", async () => {
await useAuthStore
.getState()
.login({ email: MOCK_EMAIL, password: MOCK_PASSWORD });
const { accessToken, user, status, error } = useAuthStore.getState();
expect(accessToken).toBeDefined();
expect(user).toBeDefined();
expect(status).toBe("authenticated");
expect(error).toBeNull();
});
it("should set error and update status if login fails", async () => {
await useAuthStore
.getState()
.login({ email: "wrong@test.com", password: "wrongPassword" });
const { status, error } = useAuthStore.getState();
expect(status).toBe("unauthenticated");
expect(error).toBeDefined();
});
});
describe("register", () => {
it("should set access token, user data, and update status after successful registration", async () => {
await useAuthStore
.getState()
.register({ email: MOCK_NEW_EMAIL, password: MOCK_PASSWORD });
const { accessToken, user, status, error } = useAuthStore.getState();
expect(accessToken).toBeDefined();
expect(user).toBeDefined();
expect(status).toBe("authenticated");
expect(error).toBeNull();
});
it("should set error and update status if registration fails", async () => {
await useAuthStore
.getState()
.register({ email: MOCK_EMAIL, password: MOCK_PASSWORD });
const { status, error } = useAuthStore.getState();
expect(status).toBe("unauthenticated");
expect(error).toBeDefined();
});
});
describe("logout", () => {
it("should clear access token, user data, and update status after logout", async () => {
await useAuthStore.getState().logout();
const { accessToken, user, status, error } = useAuthStore.getState();
expect(accessToken).toBeUndefined();
expect(user).toBeUndefined();
expect(status).toBe("unauthenticated");
expect(error).toBeNull();
});
});
describe("refresh", () => {
it("should update access token and user data after successful refresh", async () => {
useAuthStore.setState({ accessToken: MOCK_TOKEN });
await useAuthStore.getState().refresh();
const { accessToken, user, status, error } = useAuthStore.getState();
expect(accessToken).toBeDefined();
expect(user).toBeDefined();
expect(status).toBe("authenticated");
expect(error).toBeNull();
});
it("should set error and update status if refresh fails", async () => {
useAuthStore.setState({ accessToken: "old_token" });
await useAuthStore.getState().refresh();
const { status, error } = useAuthStore.getState();
expect(status).toBe("unauthenticated");
expect(error).toBeDefined();
});
});
});