Lobby.test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import React from "react";
  2. import { shallow } from "enzyme";
  3. import Lobby, { GameInfo, PlayerList } from "../components/screens/Lobby/Lobby";
  4. import {
  5. COUNTRY_RACE,
  6. DISTANCE,
  7. FROZEN,
  8. NORMAL,
  9. RACE,
  10. TIME_BANK,
  11. } from "../domain/constants";
  12. jest.mock("iso-3166-1");
  13. jest.mock("../domain/gameStore");
  14. jest.mock("../hooks/useGameInfo");
  15. import iso from "iso-3166-1";
  16. import { useGameId } from "../domain/gameStore";
  17. import { useGameConfig, usePlayers } from "../hooks/useGameInfo";
  18. describe("Lobby", () => {
  19. it("renders while loading", () => {
  20. useGameId.mockReturnValue("test-game-id");
  21. usePlayers.mockReturnValue(null);
  22. const onStart = jest.fn();
  23. const rendered = shallow(<Lobby onStart={onStart} />);
  24. expect(rendered).toMatchSnapshot();
  25. });
  26. it("renders with no players", () => {
  27. useGameId.mockReturnValue("test-game-id");
  28. usePlayers.mockReturnValue([]);
  29. const onStart = jest.fn();
  30. const rendered = shallow(<Lobby onStart={onStart} />);
  31. expect(rendered).toMatchSnapshot();
  32. });
  33. it("renders", () => {
  34. useGameId.mockReturnValue("test-game-id");
  35. usePlayers.mockReturnValue([{ name: "foo", name: "bar" }]);
  36. const onStart = jest.fn();
  37. const rendered = shallow(<Lobby onStart={onStart} />);
  38. expect(rendered).toMatchSnapshot();
  39. });
  40. it("properly handles joining and starting", () => {
  41. useGameId.mockReturnValue("test-game-id");
  42. usePlayers.mockReturnValue([]);
  43. const onStart = jest.fn();
  44. const rendered = shallow(<Lobby onStart={onStart} />);
  45. rendered.find("JoinForm").first().prop("onJoined")();
  46. expect(rendered).toMatchSnapshot();
  47. rendered.find("StartGame").first().prop("onStart")();
  48. expect(onStart).toHaveBeenCalled();
  49. });
  50. describe("GameInfo", () => {
  51. it("renders while loading", () => {
  52. useGameConfig.mockReturnValue({});
  53. const rendered = shallow(<GameInfo />);
  54. expect(rendered).toMatchSnapshot();
  55. });
  56. it("renders", () => {
  57. useGameConfig.mockReturnValue({
  58. rounds: 5,
  59. timer: 300,
  60. gameMode: NORMAL,
  61. clockMode: NORMAL,
  62. scoreMethod: DISTANCE,
  63. roundPointCap: null,
  64. });
  65. const rendered = shallow(<GameInfo />);
  66. expect(rendered).toMatchSnapshot();
  67. });
  68. it("renders for single round", () => {
  69. useGameConfig.mockReturnValue({
  70. rounds: 1,
  71. timer: 300,
  72. gameMode: NORMAL,
  73. clockMode: NORMAL,
  74. scoreMethod: DISTANCE,
  75. roundPointCap: null,
  76. });
  77. const rendered = shallow(<GameInfo />);
  78. expect(rendered).toMatchSnapshot();
  79. });
  80. it("renders for COUNTRY_RACE", () => {
  81. useGameConfig.mockReturnValue({
  82. rounds: 5,
  83. timer: 300,
  84. gameMode: NORMAL,
  85. clockMode: NORMAL,
  86. scoreMethod: COUNTRY_RACE,
  87. roundPointCap: null,
  88. });
  89. const rendered = shallow(<GameInfo />);
  90. expect(rendered).toMatchSnapshot();
  91. });
  92. it("renders for FROZEN", () => {
  93. useGameConfig.mockReturnValue({
  94. rounds: 5,
  95. timer: 300,
  96. gameMode: FROZEN,
  97. clockMode: NORMAL,
  98. scoreMethod: DISTANCE,
  99. roundPointCap: null,
  100. });
  101. const rendered = shallow(<GameInfo />);
  102. expect(rendered).toMatchSnapshot();
  103. });
  104. it("renders for single round COUNTRY_RACE", () => {
  105. useGameConfig.mockReturnValue({
  106. rounds: 1,
  107. timer: 300,
  108. gameMode: NORMAL,
  109. clockMode: NORMAL,
  110. scoreMethod: COUNTRY_RACE,
  111. roundPointCap: null,
  112. });
  113. const rendered = shallow(<GameInfo />);
  114. expect(rendered).toMatchSnapshot();
  115. });
  116. it("renders for single round FROZEN", () => {
  117. useGameConfig.mockReturnValue({
  118. rounds: 1,
  119. timer: 300,
  120. gameMode: FROZEN,
  121. clockMode: NORMAL,
  122. scoreMethod: DISTANCE,
  123. roundPointCap: null,
  124. });
  125. const rendered = shallow(<GameInfo />);
  126. expect(rendered).toMatchSnapshot();
  127. });
  128. it("renders for TIME_BANK", () => {
  129. useGameConfig.mockReturnValue({
  130. rounds: 5,
  131. timer: 300,
  132. gameMode: NORMAL,
  133. clockMode: TIME_BANK,
  134. scoreMethod: DISTANCE,
  135. roundPointCap: null,
  136. });
  137. const rendered = shallow(<GameInfo />);
  138. expect(rendered).toMatchSnapshot();
  139. });
  140. it("renders for RACE", () => {
  141. useGameConfig.mockReturnValue({
  142. rounds: 5,
  143. timer: 300,
  144. gameMode: NORMAL,
  145. clockMode: RACE,
  146. scoreMethod: DISTANCE,
  147. roundPointCap: null,
  148. });
  149. const rendered = shallow(<GameInfo />);
  150. expect(rendered).toMatchSnapshot();
  151. });
  152. it("renders for NORMAL", () => {
  153. useGameConfig.mockReturnValue({
  154. rounds: 5,
  155. timer: 300,
  156. gameMode: NORMAL,
  157. clockMode: NORMAL,
  158. scoreMethod: DISTANCE,
  159. roundPointCap: null,
  160. });
  161. const rendered = shallow(<GameInfo />);
  162. expect(rendered).toMatchSnapshot();
  163. });
  164. it("renders with a round point cap", () => {
  165. useGameConfig.mockReturnValue({
  166. rounds: 5,
  167. timer: 300,
  168. gameMode: NORMAL,
  169. clockMode: NORMAL,
  170. scoreMethod: DISTANCE,
  171. roundPointCap: 10000,
  172. });
  173. const rendered = shallow(<GameInfo />);
  174. expect(rendered).toMatchSnapshot();
  175. });
  176. it("renders with country lock", () => {
  177. iso.whereAlpha2.mockReturnValue({ country: "test-country" });
  178. useGameConfig.mockReturnValue({
  179. rounds: 5,
  180. timer: 300,
  181. countryLock: "country",
  182. gameMode: NORMAL,
  183. clockMode: NORMAL,
  184. scoreMethod: DISTANCE,
  185. roundPointCap: null,
  186. });
  187. const rendered = shallow(<GameInfo />);
  188. expect(rendered).toMatchSnapshot();
  189. });
  190. });
  191. describe("PlayerList", () => {
  192. it("renders", () => {
  193. const playerNames = ["foo", "bar"];
  194. const rendered = shallow(<PlayerList playerNames={playerNames} />);
  195. expect(rendered).toMatchSnapshot();
  196. });
  197. });
  198. });