import React from "react";
import { shallow } from "enzyme";
import LinkedGame from "../components/screens/GameSummary/LinkedGame";

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

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

describe("LinkedGame", () => {
  it("renders with no linked game", () => {
    const rendered = shallow(
      <LinkedGame linkedGame={null} gameId="test-game-id" />
    );
    expect(rendered).toMatchSnapshot();
  });
  it("renders with linked game", () => {
    const rendered = shallow(
      <LinkedGame linkedGame={"linked-game-id"} gameId="test-game-id" />
    );
    expect(rendered).toMatchSnapshot();
  });
  it("links game after creation", () => {
    const rendered = shallow(
      <LinkedGame linkedGame={null} gameId="test-game-id" />
    );
    rendered.find("GameCreationForm").first().prop("afterCreate")(
      "linked-game-id"
    );
    expect(linkGame).toHaveBeenCalledWith("test-game-id", "linked-game-id");
  });
  it("goes to linked game on click", () => {
    const rendered = shallow(
      <LinkedGame linkedGame={"linked-game-id"} gameId="test-game-id" />
    );
    rendered.find("button").first().simulate("click");
    expect(dispatch.goToLobby).toHaveBeenCalledWith("linked-game-id");
  });
});
