Compare commits

...

2 Commits

Author SHA1 Message Date
Ilia Mashkov
fd5b50a6f2 test(auth): write basic unit tests for authStore actions 2026-03-18 09:58:30 +03:00
Ilia Mashkov
51fc64d8c3 feat(auth): add accessToken field to the store 2026-03-18 09:57:59 +03:00
2 changed files with 124 additions and 0 deletions

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();
});
});
});

View File

@@ -77,6 +77,7 @@ export const useAuthStore = create<AuthStore>()((set) => ({
set({ set({
status: "unauthenticated", status: "unauthenticated",
user: undefined, user: undefined,
accessToken: undefined,
error: null, error: null,
}); });
} catch (err) { } catch (err) {