|
| 1 | +import { configureStore } from "@reduxjs/toolkit"; |
| 2 | +import { userReducer, fetchUser, getUser } from "./userSlice"; // Adjust the import path as necessary |
| 3 | +import { ENVIRONMENT, USER_PROFILE } from "@mds/common/constants"; |
| 4 | +import CustomAxios from "@mds/common/redux/customAxios"; |
| 5 | + |
| 6 | +const showLoadingMock = jest |
| 7 | + .fn() |
| 8 | + .mockReturnValue({ type: "SHOW_LOADING", payload: { show: true } }); |
| 9 | +const hideLoadingMock = jest |
| 10 | + .fn() |
| 11 | + .mockReturnValue({ type: "HIDE_LOADING", payload: { show: false } }); |
| 12 | + |
| 13 | +jest.mock("@mds/common/redux/customAxios"); |
| 14 | +jest.mock("react-redux-loading-bar", () => ({ |
| 15 | + showLoading: () => showLoadingMock, |
| 16 | + hideLoading: () => hideLoadingMock, |
| 17 | +})); |
| 18 | + |
| 19 | +describe("userSlice", () => { |
| 20 | + let store; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + store = configureStore({ |
| 24 | + reducer: { |
| 25 | + user: userReducer, |
| 26 | + }, |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + jest.clearAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("fetchUser", () => { |
| 35 | + const mockResponse = { |
| 36 | + data: { |
| 37 | + sub: "mock-sub", |
| 38 | + display_name: "Mock User", |
| 39 | + |
| 40 | + family_name: "MockFamily", |
| 41 | + given_name: "MockGiven", |
| 42 | + last_logged_in: "2023-10-01T12:00:00.000Z", |
| 43 | + }, |
| 44 | + }; |
| 45 | + |
| 46 | + it("should fetch user data successfully", async () => { |
| 47 | + (CustomAxios as jest.Mock).mockImplementation(() => ({ |
| 48 | + get: jest.fn().mockResolvedValue(mockResponse), |
| 49 | + })); |
| 50 | + |
| 51 | + await store.dispatch(fetchUser()); |
| 52 | + const state = store.getState().user; |
| 53 | + |
| 54 | + // Verify loading state management |
| 55 | + expect(showLoadingMock).toHaveBeenCalledTimes(1); |
| 56 | + expect(hideLoadingMock).toHaveBeenCalledTimes(1); |
| 57 | + |
| 58 | + // Verify state update |
| 59 | + expect(getUser({ user: state })).toEqual(mockResponse.data); |
| 60 | + expect(CustomAxios).toHaveBeenCalledWith({ errorToastMessage: "default" }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should handle API error", async () => { |
| 64 | + const error = new Error("API Error"); |
| 65 | + (CustomAxios as jest.Mock).mockImplementation(() => ({ |
| 66 | + get: jest.fn().mockRejectedValue(error), |
| 67 | + })); |
| 68 | + |
| 69 | + await store.dispatch(fetchUser()); |
| 70 | + const state = store.getState().user; |
| 71 | + |
| 72 | + // Check user state remains null on error |
| 73 | + expect(getUser({ user: state })).toBeNull(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should construct the correct endpoint URL", async () => { |
| 77 | + const getMock = jest.fn().mockResolvedValue(mockResponse); |
| 78 | + (CustomAxios as jest.Mock).mockImplementation(() => ({ |
| 79 | + get: getMock, |
| 80 | + })); |
| 81 | + |
| 82 | + await store.dispatch(fetchUser()); |
| 83 | + |
| 84 | + expect(getMock).toHaveBeenCalledWith( |
| 85 | + `${ENVIRONMENT.apiUrl}${USER_PROFILE()}`, |
| 86 | + expect.any(Object) |
| 87 | + ); |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments