Kirk Trombley 4 лет назад
Родитель
Сommit
8edab490be

+ 7 - 0
client/src/setupTests.js

@@ -4,6 +4,13 @@ import 'jest-enzyme';
 
 configure({ adapter: new Adapter() });
 
+global.localStorage = {
+  getItem: jest.fn(),
+  setItem: jest.fn(),
+  removeItem: jest.fn(),
+  clear: jest.fn(),
+};
+
 global.google = {
   maps: {
     Geocoder: class {

+ 1 - 1
client/src/App.test.js → client/src/tests/App.test.js

@@ -1,6 +1,6 @@
 import React from "react";
 import { shallow } from "enzyme";
-import App from "./App";
+import App from "../App";
 
 it("renders without crashing", () => {
   shallow(<App />);

+ 1 - 1
client/src/components/util/__tests__/ClickToCopy.test.js → client/src/tests/ClickToCopy.test.js

@@ -1,6 +1,6 @@
 import React from "react";
 import { shallow, mount } from "enzyme";
-import ClickToCopy from "../ClickToCopy";
+import ClickToCopy from "../components/util/ClickToCopy";
 
 describe("ClickToCopy", () => {
   it("renders without crashing", () => {

+ 50 - 0
client/src/tests/GamePanel.test.js

@@ -0,0 +1,50 @@
+import React from "react";
+import { shallow } from "enzyme";
+import { FROZEN, NORMAL, RACE } from "../domain/ruleSets";
+import GamePanel from "../components/screens/GamePanel";
+
+jest.mock("../domain/gameStore");
+jest.mock("../hooks/usePreventNavigation");
+jest.mock("../hooks/useGameInfo");
+
+import usePreventNavigation from "../hooks/usePreventNavigation";
+import { useGameConfig } from "../hooks/useGameInfo";
+import { useCurrentRound } from "../domain/gameStore";
+
+describe("GamePanel", () => {
+  it("renders for NORMAL game", () => {
+    useCurrentRound.mockReturnValue("");
+    useGameConfig.mockReturnValue({ ruleSet: NORMAL });
+    const rendered = shallow(<GamePanel/>);
+    expect(rendered).toMatchSnapshot();
+    expect(usePreventNavigation).toHaveBeenCalled();
+    expect(useGameConfig).toHaveBeenCalled();
+  });
+
+  it("renders for end of game", () => {
+    useCurrentRound.mockReturnValue(null);
+    useGameConfig.mockReturnValue({ ruleSet: NORMAL });
+    const rendered = shallow(<GamePanel/>);
+    expect(rendered).toMatchSnapshot();
+    expect(usePreventNavigation).toHaveBeenCalled();
+    expect(useGameConfig).toHaveBeenCalled();
+  });
+
+  it("renders for FROZEN game", () => {
+    useCurrentRound.mockReturnValue("");
+    useGameConfig.mockReturnValue({ ruleSet: FROZEN });
+    const rendered = shallow(<GamePanel/>);
+    expect(rendered).toMatchSnapshot();
+    expect(usePreventNavigation).toHaveBeenCalled();
+    expect(useGameConfig).toHaveBeenCalled();
+  });
+
+  it("renders for RACE game", () => {
+    useCurrentRound.mockReturnValue("");
+    useGameConfig.mockReturnValue({ ruleSet: RACE });
+    const rendered = shallow(<GamePanel/>);
+    expect(rendered).toMatchSnapshot();
+    expect(usePreventNavigation).toHaveBeenCalled();
+    expect(useGameConfig).toHaveBeenCalled();
+  });
+});

+ 1 - 1
client/src/components/util/__tests__/Loading.test.js → client/src/tests/Loading.test.js

@@ -1,6 +1,6 @@
 import React from "react";
 import { shallow } from "enzyme";
-import Loading from "../Loading";
+import Loading from "../components/util/Loading";
 
 describe("Loading icon", () => {
   it("renders", () => {

+ 47 - 0
client/src/tests/PositionedStreetView.test.js

@@ -0,0 +1,47 @@
+import React from "react";
+import { shallow } from "enzyme";
+import PositionedStreetView from "../components/screens/GamePanel/PositionedStreetView";
+
+jest.mock("../domain/gameStore");
+jest.mock("../components/screens/GamePanel/usePano");
+
+import { usePanoStartPosition, usePanoStartPov, useTargetPoint } from "../domain/gameStore";
+import usePano from "../components/screens/GamePanel/usePano";
+
+describe("PositionedStreetView", () => {
+  it("renders", () => {
+    usePanoStartPosition.mockReturnValue();
+    usePanoStartPov.mockReturnValue();
+    useTargetPoint.mockReturnValue();
+
+    const rendered = shallow(<PositionedStreetView/>);
+    expect(rendered).toMatchSnapshot();
+    expect(usePano).toHaveBeenCalled();
+  });
+
+  it("resets", () => {
+    usePanoStartPosition.mockReturnValue();
+    usePanoStartPov.mockReturnValue();
+    useTargetPoint.mockReturnValue("point");
+    const setPosition = jest.fn();
+    usePano.mockReturnValue({ current: { setPosition }});
+
+    const handle = shallow(<PositionedStreetView/>);
+    expect(handle).toMatchSnapshot();
+    handle.find("div.resetButton").first().simulate("click");
+    expect(setPosition).toHaveBeenCalledWith("point");
+  });
+
+  it("resets on enter on button", () => {
+    usePanoStartPosition.mockReturnValue();
+    usePanoStartPov.mockReturnValue();
+    useTargetPoint.mockReturnValue("point");
+    const setPosition = jest.fn();
+    usePano.mockReturnValue({ current: { setPosition }});
+
+    const handle = shallow(<PositionedStreetView/>);
+    expect(handle).toMatchSnapshot();
+    handle.find("div.resetButton").first().simulate("keydown", { key: "Enter" });
+    expect(setPosition).toHaveBeenCalledWith("point");
+  });
+});

+ 13 - 0
client/src/tests/RaceMode.test.js

@@ -0,0 +1,13 @@
+import React from "react";
+import { shallow } from "enzyme";
+import RaceMode from "../components/screens/GamePanel/RaceMode";
+
+jest.mock("../domain/gameStore");
+jest.mock("../domain/apiMethods");
+
+describe("PositionedStreetView", () => {
+  it("renders", () => {
+    const rendered = shallow(<RaceMode/>);
+    expect(rendered).toMatchSnapshot();
+  });
+});

+ 21 - 0
client/src/tests/StartGame.test.js

@@ -0,0 +1,21 @@
+import React from "react";
+import { shallow, mount } from "enzyme";
+import StartGame from "../components/screens/Lobby/StartGame";
+
+jest.mock("../domain/gameStore");
+
+import { usePlayerName } from "../domain/gameStore";
+
+describe("StartGame", () => {
+  it("renders without crashing", () => {
+    usePlayerName.mockReturnValue("test-player");
+    const rendered = shallow(<StartGame/>);
+    expect(rendered).toMatchSnapshot();
+  });
+  it("responds to clicking the join button", () => {
+    usePlayerName.mockReturnValue("test-player");
+    const handle = mount(<StartGame/>);
+    handle.find("button").first().simulate("click");
+    expect(handle).toMatchSnapshot();
+  });
+});

+ 0 - 0
client/src/components/util/__tests__/__snapshots__/ClickToCopy.test.js.snap → client/src/tests/__snapshots__/ClickToCopy.test.js.snap


+ 49 - 0
client/src/tests/__snapshots__/GamePanel.test.js.snap

@@ -0,0 +1,49 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`GamePanel renders for FROZEN game 1`] = `
+<div
+  className="page"
+>
+  <div
+    className="streetview"
+  >
+    <PositionedStreetView />
+    <div
+      className="freeze"
+    />
+  </div>
+  <GuessPane />
+</div>
+`;
+
+exports[`GamePanel renders for NORMAL game 1`] = `
+<div
+  className="page"
+>
+  <div
+    className="streetview"
+  >
+    <PositionedStreetView />
+  </div>
+  <GuessPane />
+</div>
+`;
+
+exports[`GamePanel renders for RACE game 1`] = `
+<div
+  className="page"
+>
+  <RaceMode
+    cutoffTime={10}
+    rate={1000}
+  />
+  <div
+    className="streetview"
+  >
+    <PositionedStreetView />
+  </div>
+  <GuessPane />
+</div>
+`;
+
+exports[`GamePanel renders for end of game 1`] = `<Loading />`;

+ 0 - 0
client/src/components/util/__tests__/__snapshots__/Loading.test.js.snap → client/src/tests/__snapshots__/Loading.test.js.snap


+ 52 - 0
client/src/tests/__snapshots__/PositionedStreetView.test.js.snap

@@ -0,0 +1,52 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`PositionedStreetView renders 1`] = `
+<Fragment>
+  <div
+    className="fullsize"
+  />
+  <div
+    className="resetButton"
+    onClick={[Function]}
+    onKeyDown={[Function]}
+    role="button"
+    tabIndex="0"
+  >
+    Reset
+  </div>
+</Fragment>
+`;
+
+exports[`PositionedStreetView resets 1`] = `
+<Fragment>
+  <div
+    className="fullsize"
+  />
+  <div
+    className="resetButton"
+    onClick={[Function]}
+    onKeyDown={[Function]}
+    role="button"
+    tabIndex="0"
+  >
+    Reset
+  </div>
+</Fragment>
+`;
+
+exports[`PositionedStreetView resets on enter on button 1`] = `
+<Fragment>
+  <div
+    className="fullsize"
+  />
+  <div
+    className="resetButton"
+    onClick={[Function]}
+    onKeyDown={[Function]}
+    role="button"
+    tabIndex="0"
+  >
+    Reset
+  </div>
+</Fragment>
+`;

+ 3 - 0
client/src/tests/__snapshots__/RaceMode.test.js.snap

@@ -0,0 +1,3 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`PositionedStreetView renders 1`] = `<Fragment />`;

+ 51 - 0
client/src/tests/__snapshots__/StartGame.test.js.snap

@@ -0,0 +1,51 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`StartGame renders without crashing 1`] = `
+<Fragment>
+  <span
+    className="label"
+  >
+    Playing as 
+    test-player
+  </span>
+  <DelayedButton
+    autoFocus={true}
+    countDownFormatter={[Function]}
+    onEnd={[MockFunction]}
+  >
+    Start Game
+  </DelayedButton>
+</Fragment>
+`;
+
+exports[`StartGame responds to clicking the join button 1`] = `
+<StartGame>
+  <span
+    className="label"
+  >
+    Playing as 
+    test-player
+  </span>
+  <DelayedButton
+    autoFocus={true}
+    countDownFormatter={[Function]}
+    onEnd={[MockFunction]}
+  >
+    <CountdownButton
+      autoFocus={true}
+      formatter={[Function]}
+      onCancelled={[Function]}
+      onEnd={[MockFunction]}
+      seconds={3}
+    >
+      <button
+        autoFocus={true}
+        onClick={[Function]}
+        type="button"
+      >
+        Click to cancel, 3s...
+      </button>
+    </CountdownButton>
+  </DelayedButton>
+</StartGame>
+`;