import reducer from "../../../store/reducers/user/usersReducer";
import expect from "expect";
import {
  deleteStateUser,
  deleteUserError,
  setEnableUsers,
  setEnableUsersError,
  setUsers,
  setUsersError,
} from "../../../store/actions/users/usersActions";

describe("users reducer", () => {
  it("should return the initial state", () => {
    expect(reducer(undefined, {})).toEqual({
      fetchUsersErrorMessage: "",
      selected: {},
      toggleEnableErrorMessage: "",
      users: [],
    });
  });

  it("should set error message", () => {
    expect(reducer(undefined, setUsersError("Error"))).toEqual({
      fetchUsersErrorMessage: "Error",
      selected: {},
      toggleEnableErrorMessage: "",
      users: [],
    });
  });

  it("Should set state users", () => {
    expect(reducer(undefined, setUsers(["user1", "user2"]))).toEqual({
      fetchUsersErrorMessage: "",
      selected: {},
      toggleEnableErrorMessage: "",
      users: ["user1", "user2"],
    });
  });

  it("should set enable toggle error message", () => {
    expect(reducer(undefined, setEnableUsersError("Error"))).toEqual({
      fetchUsersErrorMessage: "",
      selected: {},
      toggleEnableErrorMessage: "Error",
      users: [],
    });
  });

  it("should toogle enable on specific user", () => {
    expect(
      reducer(
        {
          fetchUsersErrorMessage: "",
          selected: {},
          toggleEnableErrorMessage: "",
          users: [
            { id: 1, name: "User1", isEnabled: true },
            { id: 2, name: "User2", isEnabled: true },
          ],
        },
        setEnableUsers(1)
      )
    ).toEqual({
      fetchUsersErrorMessage: "",
      selected: {},
      toggleEnableErrorMessage: "",
      users: [
        { id: 1, name: "User1", isEnabled: false },
        { id: 2, name: "User2", isEnabled: true },
      ],
    });
  });

  it("should set delete user error message", () => {
    expect(reducer(undefined, deleteUserError("Error"))).toEqual({
      fetchUsersErrorMessage: "",
      selected: {},
      toggleEnableErrorMessage: "Error",
      users: [],
    });
  });

  it("should delete specific user", () => {
    expect(
      reducer(
        {
          fetchUsersErrorMessage: "",
          selected: {},
          toggleEnableErrorMessage: "",
          users: [
            { id: 1, name: "User1", isEnabled: true },
            { id: 2, name: "User2", isEnabled: true },
          ],
        },
        deleteStateUser(1)
      )
    ).toEqual({
      fetchUsersErrorMessage: "",
      selected: {},
      toggleEnableErrorMessage: "",
      users: [{ id: 2, name: "User2", isEnabled: true }],
    });
  });
});
