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

jest.mock("../domain/gameStore");
jest.mock("../hooks/useMap");
jest.mock("../hooks/usePreventNavigation");
jest.mock("../hooks/useGameInfo");
jest.mock("../hooks/useMarkersFromGuesses");
jest.mock("../components/screens/RoundSummary/useClickToCheckScore");

import { dispatch, useCurrentRound, useLastRound } from "../domain/gameStore";
import useMap from "../hooks/useMap";
import usePreventNavigation from "../hooks/usePreventNavigation";
import { usePlayers } from "../hooks/useGameInfo";
import useMarkersFromGuesses from "../hooks/useMarkersFromGuesses";
import useClickToCheckScore from "../components/screens/RoundSummary/useClickToCheckScore";

describe("RoundSummary", () => {
  it("renders", () => {
    useLastRound.mockReturnValue({
      roundNum: "round",
      score: 5000,
      totalScore: 10000,
      targetPoint: "target",
    });
    useMap.mockReturnValue("map");
    usePlayers.mockReturnValue("players");
    const rendered = shallow(<RoundSummary />);
    expect(rendered).toMatchSnapshot();
    expect(usePreventNavigation).toHaveBeenCalled();
    expect(useMarkersFromGuesses).toHaveBeenCalledWith(
      "map",
      "players",
      "round",
      "target"
    );
    expect(useClickToCheckScore).toHaveBeenCalledWith("map", "target");
  });

  describe("NextRoundButton", () => {
    it("renders when there is not a next round", () => {
      useCurrentRound.mockReturnValue(null);
      const rendered = shallow(<NextRoundButton />);
      expect(rendered).toMatchSnapshot();
    });

    it("proceeds to summary when clicked if there is not a next round", () => {
      useCurrentRound.mockReturnValue(null);
      const rendered = shallow(<NextRoundButton />);
      rendered.find("button").first().simulate("click");
      expect(dispatch.goToSummary).toHaveBeenCalled();
    });

    it("renders when there is a next round", () => {
      useCurrentRound.mockReturnValue("round");
      const rendered = shallow(<NextRoundButton />);
      expect(rendered).toMatchSnapshot();
    });

    it("handles click if there is a next round", () => {
      useCurrentRound.mockReturnValue("round");
      const rendered = mount(<NextRoundButton />);
      rendered.find("button").first().simulate("click");
      expect(rendered).toMatchSnapshot();
    });

    it("proceeds to next round when button ends", () => {
      useCurrentRound.mockReturnValue("round");
      const rendered = mount(<NextRoundButton />);
      rendered.find("DelayedButton").first().prop("onEnd")();
      expect(dispatch.startRound).toHaveBeenCalled();
    });
  });
});
