import React from "react";
import { mount, shallow } from "enzyme";
import HomePage, { Rejoin } from "../components/screens/HomePage/HomePage";

jest.mock("../domain/gameStore");
jest.mock("../components/screens/HomePage/useHasSavedInfo");

import { dispatch } from "../domain/gameStore";
import useHasSavedInfo from "../components/screens/HomePage/useHasSavedInfo";

describe("HomePage", () => {
  it("renders", () => {
    useHasSavedInfo.mockReturnValue(false);
    const rendered = shallow(<HomePage />);
    expect(rendered).toMatchSnapshot();
  });

  it("renders with saved info", () => {
    useHasSavedInfo.mockReturnValue(true);
    const rendered = shallow(<HomePage />);
    expect(rendered).toMatchSnapshot();
  });

  it("goes to lobby after game created", () => {
    const rendered = shallow(<HomePage />);
    rendered.find("GameCreationForm").first().prop("afterCreate")(
      "test-game-id"
    );
    expect(dispatch.goToLobby).toHaveBeenCalledWith("test-game-id");
  });

  describe("Rejoin", () => {
    it("renders", () => {
      const rendered = shallow(<Rejoin />);
      expect(rendered).toMatchSnapshot();
    });

    it("handles click", () => {
      const rendered = mount(<Rejoin />);
      rendered.find("button").first().simulate("click");
      expect(rendered).toMatchSnapshot();
    });

    it("rejoins when button ends", () => {
      const rendered = shallow(<Rejoin />);
      rendered.find("DelayedButton").first().prop("onEnd")();
      expect(dispatch.rejoinGame).toHaveBeenCalled();
    });
  });
});
