import { createStore, applyMiddleware } from "redux";
import { thunk } from "redux-thunk";
import fetchMock from "fetch-mock";
import { loginThunk } from "./actions"; // Adjust the import path
import { loginAction } from "./actions"; // Adjust the import path
import { reducer, userReducer } from "../reducer";

// Create a mock store using redux-mock-store
const middlewares = [thunk];
const mockStore = createStore(
  () => ({ user: userReducer, search: reducer }),
  applyMiddleware(...middlewares)
);

describe("loginThunk", () => {
  let store;

  beforeEach(() => {
    store = mockStore({});
    fetchMock.restore(); // Reset fetchMock before each test
  });

  it("should dispatch loginAction and store user data on successful login", async () => {
    const mockResponse = { username: "admin", token: "1234" };
    fetchMock.post("/api/login", {
      body: mockResponse,
      headers: { "content-type": "application/json" },
      status: 200,
    });

    const expectedActions = [loginAction(mockResponse)];

    const result = await store.dispatch(loginThunk("testuser", "password123"));

    expect(store.getActions()).toEqual(expectedActions);
    expect(localStorage.getItem("user")).toEqual(JSON.stringify(mockResponse));
    expect(result).toEqual({ success: true });
  });

  it("should return error message on failed login", async () => {
    fetchMock.post("/api/login", {
      body: { message: "Invalid credentials" },
      headers: { "content-type": "application/json" },
      status: 401,
    });

    const result = await store.dispatch(
      loginThunk("testuser", "wrongpassword")
    );

    expect(store.getActions()).toEqual([]);
    expect(result).toEqual({ success: false, message: "Invalid credentials" });
  });

  it("should handle network errors", async () => {
    fetchMock.post("/api/login", 500);

    const result = await store.dispatch(loginThunk("testuser", "password123"));

    expect(store.getActions()).toEqual([]);
    expect(result).toEqual({
      success: false,
      message: "TypeError: Failed to fetch",
    });
  });
});



npm test

> reactspringcopy@0.0.0 test
> node --experimental-vm-modules node_modules/jest/bin/jest.js

(node:34060) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
 FAIL  src/store/actions/actions.test.js
  loginThunk
    × should dispatch loginAction and store user data on successful login (1 ms)
    × should return error message on failed login (1 ms)
    × should handle network errors (1 ms)

  ● loginThunk › should dispatch loginAction and store user data on successful login

    TypeError: mockStore is not a function

      17 |
      18 |   beforeEach(() => {
    > 19 |     store = mockStore({});
         |             ^
      20 |     fetchMock.restore(); // Reset fetchMock before each test
      21 |   });
      22 |

      at Object.<anonymous> (src/store/actions/actions.test.js:19:13)

  ● loginThunk › should return error message on failed login

    TypeError: mockStore is not a function

      17 |
      18 |   beforeEach(() => {
    > 19 |     store = mockStore({});
         |             ^
      20 |     fetchMock.restore(); // Reset fetchMock before each test
      21 |   });
      22 |

      at Object.<anonymous> (src/store/actions/actions.test.js:19:13)

  ● loginThunk › should handle network errors

    TypeError: mockStore is not a function

      17 |
      18 |   beforeEach(() => {
    > 19 |     store = mockStore({});
         |             ^
      20 |     fetchMock.restore(); // Reset fetchMock before each test
      21 |   });
      22 |

      at Object.<anonymous> (src/store/actions/actions.test.js:19:13)

Test Suites: 1 failed, 1 total
Tests:       3 failed, 3 total
Snapshots:   0 total
Time:        0.746 s, estimated 1 s
Ran all test suites.