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

jest.mock("../domain/gameStore");
jest.mock("../domain/localStorageMethods");

import { dispatch } from "../domain/gameStore";

describe("HomePage", () => {
  it("renders", () => {
    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();
    });
  });
});
