Compare commits
2 Commits
2afbd73a31
...
fd5b50a6f2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd5b50a6f2 | ||
|
|
51fc64d8c3 |
123
src/features/auth/model/stores/authStore/authStore.spec.ts
Normal file
123
src/features/auth/model/stores/authStore/authStore.spec.ts
Normal 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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user