diff --git a/src/features/auth/model/stores/authStore/authStore.spec.ts b/src/features/auth/model/stores/authStore/authStore.spec.ts new file mode 100644 index 0000000..314c26b --- /dev/null +++ b/src/features/auth/model/stores/authStore/authStore.spec.ts @@ -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(); + }); + }); +});