|
| 1 | +// Imports required for testing |
| 2 | +import { AxiosError } from "axios"; |
| 3 | +import { createAsyncThunk } from "@reduxjs/toolkit"; |
| 4 | + |
| 5 | +import axios from "../redux/api/api"; |
| 6 | +import { createUser, verifyUser } from "../redux/reducers/registerSlice"; |
| 7 | + |
| 8 | +jest.mock("../redux/api/api"); |
| 9 | + |
| 10 | +describe("registerSlice Thunks", () => { |
| 11 | + const mockUser = { |
| 12 | + name: "John Doe", |
| 13 | + username: "johndoe", |
| 14 | + |
| 15 | + password: "password123", |
| 16 | + }; |
| 17 | + |
| 18 | + const mockResponseData = { message: "User registered successfully" }; |
| 19 | + const mockToken = "validToken123"; |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + jest.resetAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe("createUser thunk", () => { |
| 26 | + it("should handle fulfilled case", async () => { |
| 27 | + // @ts-ignore |
| 28 | + axios.post.mockResolvedValueOnce({ data: mockResponseData }); |
| 29 | + |
| 30 | + const thunk = createUser(mockUser); |
| 31 | + const dispatch = jest.fn(); |
| 32 | + const getState = jest.fn(); |
| 33 | + |
| 34 | + await thunk(dispatch, getState, null); |
| 35 | + expect(dispatch).toHaveBeenCalled(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should handle rejected case with response error data", async () => { |
| 39 | + const errorMessage = { error: "Registration failed" }; |
| 40 | + // @ts-ignore |
| 41 | + axios.post.mockRejectedValueOnce({ |
| 42 | + response: { data: errorMessage }, |
| 43 | + message: "Request failed", |
| 44 | + }); |
| 45 | + |
| 46 | + const thunk = createUser(mockUser); |
| 47 | + const dispatch = jest.fn(); |
| 48 | + const getState = jest.fn(); |
| 49 | + |
| 50 | + await thunk(dispatch, getState, null); |
| 51 | + expect(dispatch).toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should handle rejected case without response error data", async () => { |
| 55 | + // @ts-ignore |
| 56 | + axios.post.mockRejectedValueOnce(new Error("Network Error")); |
| 57 | + |
| 58 | + const thunk = createUser(mockUser); |
| 59 | + const dispatch = jest.fn(); |
| 60 | + const getState = jest.fn(); |
| 61 | + |
| 62 | + await thunk(dispatch, getState, null); |
| 63 | + expect(dispatch).toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("verifyUser thunk", () => { |
| 68 | + it("should handle fulfilled case", async () => { |
| 69 | + // @ts-ignore |
| 70 | + axios.get.mockResolvedValueOnce({ data: true }); |
| 71 | + |
| 72 | + const thunk = verifyUser(mockToken); |
| 73 | + const dispatch = jest.fn(); |
| 74 | + const getState = jest.fn(); |
| 75 | + |
| 76 | + await thunk(dispatch, getState, null); |
| 77 | + expect(dispatch).toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should handle rejected case with response error data", async () => { |
| 81 | + const errorMessage = { error: "Verification failed" }; |
| 82 | + // @ts-ignore |
| 83 | + axios.get.mockRejectedValueOnce({ |
| 84 | + response: { data: errorMessage }, |
| 85 | + message: "Request failed", |
| 86 | + }); |
| 87 | + |
| 88 | + const thunk = verifyUser(mockToken); |
| 89 | + const dispatch = jest.fn(); |
| 90 | + const getState = jest.fn(); |
| 91 | + |
| 92 | + await thunk(dispatch, getState, null); |
| 93 | + expect(dispatch).toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should handle rejected case without response error data", async () => { |
| 97 | + // @ts-ignore |
| 98 | + axios.get.mockRejectedValueOnce(new Error("Network Error")); |
| 99 | + |
| 100 | + const thunk = verifyUser(mockToken); |
| 101 | + const dispatch = jest.fn(); |
| 102 | + const getState = jest.fn(); |
| 103 | + |
| 104 | + await thunk(dispatch, getState, null); |
| 105 | + expect(dispatch).toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments