apiMethods.test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. import {
  2. checkScore,
  3. createGame,
  4. getCurrentRound,
  5. getFirstSubmitter,
  6. getGameConfig,
  7. getGameCoords,
  8. getGenerators,
  9. getLinkedGame,
  10. getPlayers,
  11. getStatus,
  12. joinGame,
  13. linkGame,
  14. sendGuess,
  15. sendTimeout,
  16. } from "../domain/apiMethods";
  17. global.fetch = jest.fn();
  18. describe("apiMethods", () => {
  19. describe("getStatus", () => {
  20. it("gets status", async () => {
  21. global.fetch.mockImplementation(() =>
  22. Promise.resolve({
  23. ok: true,
  24. json: () =>
  25. Promise.resolve({
  26. status: "healthy",
  27. version: "test",
  28. }),
  29. })
  30. );
  31. expect(await getStatus()).toMatchSnapshot();
  32. expect(global.fetch).toMatchSnapshot();
  33. });
  34. it("handles server error", async () => {
  35. global.fetch.mockImplementation(() =>
  36. Promise.resolve({
  37. ok: false,
  38. statusText: "failed",
  39. })
  40. );
  41. expect(await getStatus()).toMatchSnapshot();
  42. expect(global.fetch).toMatchSnapshot();
  43. });
  44. it("handles thrown error", async () => {
  45. global.fetch.mockImplementation(() => {
  46. throw new Error("error");
  47. });
  48. expect(await getStatus()).toMatchSnapshot();
  49. expect(global.fetch).toMatchSnapshot();
  50. });
  51. });
  52. describe("checkScore", () => {
  53. it("checks score", async () => {
  54. global.fetch.mockImplementation(() =>
  55. Promise.resolve({
  56. ok: true,
  57. json: () =>
  58. Promise.resolve({
  59. score: 1000,
  60. }),
  61. })
  62. );
  63. expect(await checkScore("point1", "point2")).toMatchSnapshot();
  64. expect(global.fetch).toMatchSnapshot();
  65. });
  66. it("throws on server error", async () => {
  67. global.fetch.mockImplementation(() =>
  68. Promise.resolve({
  69. ok: false,
  70. statusText: "failed",
  71. })
  72. );
  73. await expect(checkScore("point1", "point2")).rejects.toThrow();
  74. expect(global.fetch).toMatchSnapshot();
  75. });
  76. it("passes thrown error", async () => {
  77. global.fetch.mockImplementation(() => {
  78. throw new Error("error");
  79. });
  80. await expect(checkScore("point1", "point2")).rejects.toThrow();
  81. expect(global.fetch).toMatchSnapshot();
  82. });
  83. });
  84. describe("getGenerators", () => {
  85. it("gets generators", async () => {
  86. global.fetch.mockImplementation(() =>
  87. Promise.resolve({
  88. ok: true,
  89. json: () =>
  90. Promise.resolve({
  91. generators: [],
  92. }),
  93. })
  94. );
  95. expect(await getGenerators()).toMatchSnapshot();
  96. expect(global.fetch).toMatchSnapshot();
  97. });
  98. it("throws on server error", async () => {
  99. global.fetch.mockImplementation(() =>
  100. Promise.resolve({
  101. ok: false,
  102. statusText: "failed",
  103. })
  104. );
  105. await expect(getGenerators()).rejects.toThrow();
  106. expect(global.fetch).toMatchSnapshot();
  107. });
  108. it("passes thrown error", async () => {
  109. global.fetch.mockImplementation(() => {
  110. throw new Error("error");
  111. });
  112. await expect(getGenerators()).rejects.toThrow();
  113. expect(global.fetch).toMatchSnapshot();
  114. });
  115. });
  116. describe("createGame", () => {
  117. it("creates game", async () => {
  118. global.fetch.mockImplementation(() =>
  119. Promise.resolve({
  120. ok: true,
  121. json: () =>
  122. Promise.resolve({
  123. gameId: "test-game-id",
  124. }),
  125. })
  126. );
  127. expect(
  128. await createGame(
  129. "timer",
  130. "rounds",
  131. "countryLock",
  132. "generationMethod",
  133. "ruleSet"
  134. )
  135. ).toMatchSnapshot();
  136. expect(global.fetch).toMatchSnapshot();
  137. });
  138. it("throws on server error", async () => {
  139. global.fetch.mockImplementation(() =>
  140. Promise.resolve({
  141. ok: false,
  142. statusText: "failed",
  143. })
  144. );
  145. await expect(
  146. createGame(
  147. "timer",
  148. "rounds",
  149. "countryLock",
  150. "generationMethod",
  151. "ruleSet"
  152. )
  153. ).rejects.toThrow();
  154. expect(global.fetch).toMatchSnapshot();
  155. });
  156. it("passes thrown error", async () => {
  157. global.fetch.mockImplementation(() => {
  158. throw new Error("error");
  159. });
  160. await expect(
  161. createGame(
  162. "timer",
  163. "rounds",
  164. "countryLock",
  165. "generationMethod",
  166. "ruleSet"
  167. )
  168. ).rejects.toThrow();
  169. expect(global.fetch).toMatchSnapshot();
  170. });
  171. });
  172. describe("getGameConfig", () => {
  173. it("gets game config", async () => {
  174. global.fetch.mockImplementation(() =>
  175. Promise.resolve({
  176. ok: true,
  177. json: () =>
  178. Promise.resolve({
  179. config: "test-config",
  180. }),
  181. })
  182. );
  183. expect(await getGameConfig("test-game-id")).toMatchSnapshot();
  184. expect(global.fetch).toMatchSnapshot();
  185. });
  186. it("throws on server error", async () => {
  187. global.fetch.mockImplementation(() =>
  188. Promise.resolve({
  189. ok: false,
  190. statusText: "failed",
  191. })
  192. );
  193. await expect(getGameConfig("test-game-id")).rejects.toThrow();
  194. expect(global.fetch).toMatchSnapshot();
  195. });
  196. it("passes thrown error", async () => {
  197. global.fetch.mockImplementation(() => {
  198. throw new Error("error");
  199. });
  200. await expect(getGameConfig("test-game-id")).rejects.toThrow();
  201. expect(global.fetch).toMatchSnapshot();
  202. });
  203. });
  204. describe("getGameCoords", () => {
  205. it("gets game coords", async () => {
  206. global.fetch.mockImplementation(() =>
  207. Promise.resolve({
  208. ok: true,
  209. json: () =>
  210. Promise.resolve({
  211. coords: ["test-coord"],
  212. }),
  213. })
  214. );
  215. expect(await getGameCoords("test-game-id")).toMatchSnapshot();
  216. expect(global.fetch).toMatchSnapshot();
  217. });
  218. it("throws on server error", async () => {
  219. global.fetch.mockImplementation(() =>
  220. Promise.resolve({
  221. ok: false,
  222. statusText: "failed",
  223. })
  224. );
  225. await expect(getGameCoords("test-game-id")).rejects.toThrow();
  226. expect(global.fetch).toMatchSnapshot();
  227. });
  228. it("passes thrown error", async () => {
  229. global.fetch.mockImplementation(() => {
  230. throw new Error("error");
  231. });
  232. await expect(getGameCoords("test-game-id")).rejects.toThrow();
  233. expect(global.fetch).toMatchSnapshot();
  234. });
  235. });
  236. describe("getPlayers", () => {
  237. it("gets game players", async () => {
  238. global.fetch.mockImplementation(() =>
  239. Promise.resolve({
  240. ok: true,
  241. json: () =>
  242. Promise.resolve({
  243. players: ["test-player"],
  244. }),
  245. })
  246. );
  247. expect(await getPlayers("test-game-id")).toMatchSnapshot();
  248. expect(global.fetch).toMatchSnapshot();
  249. });
  250. it("throws on server error", async () => {
  251. global.fetch.mockImplementation(() =>
  252. Promise.resolve({
  253. ok: false,
  254. statusText: "failed",
  255. })
  256. );
  257. await expect(getPlayers("test-game-id")).rejects.toThrow();
  258. expect(global.fetch).toMatchSnapshot();
  259. });
  260. it("passes thrown error", async () => {
  261. global.fetch.mockImplementation(() => {
  262. throw new Error("error");
  263. });
  264. await expect(getPlayers("test-game-id")).rejects.toThrow();
  265. expect(global.fetch).toMatchSnapshot();
  266. });
  267. });
  268. describe("getLinkedGame", () => {
  269. it("gets linked game", async () => {
  270. global.fetch.mockImplementation(() =>
  271. Promise.resolve({
  272. ok: true,
  273. json: () =>
  274. Promise.resolve({
  275. linkedGame: ["test-linked-game-id"],
  276. }),
  277. })
  278. );
  279. expect(await getLinkedGame("test-game-id")).toMatchSnapshot();
  280. expect(global.fetch).toMatchSnapshot();
  281. });
  282. it("throws on server error", async () => {
  283. global.fetch.mockImplementation(() =>
  284. Promise.resolve({
  285. ok: false,
  286. statusText: "failed",
  287. })
  288. );
  289. await expect(getLinkedGame("test-game-id")).rejects.toThrow();
  290. expect(global.fetch).toMatchSnapshot();
  291. });
  292. it("passes thrown error", async () => {
  293. global.fetch.mockImplementation(() => {
  294. throw new Error("error");
  295. });
  296. await expect(getLinkedGame("test-game-id")).rejects.toThrow();
  297. expect(global.fetch).toMatchSnapshot();
  298. });
  299. });
  300. describe("linkGame", () => {
  301. it("links game to another", async () => {
  302. global.fetch.mockImplementation(() =>
  303. Promise.resolve({
  304. ok: true,
  305. })
  306. );
  307. expect(
  308. await linkGame("test-game-id", "test-linked-game-id")
  309. ).toMatchSnapshot();
  310. expect(global.fetch).toMatchSnapshot();
  311. });
  312. it("throws on server error", async () => {
  313. global.fetch.mockImplementation(() =>
  314. Promise.resolve({
  315. ok: false,
  316. statusText: "failed",
  317. })
  318. );
  319. await expect(
  320. linkGame("test-game-id", "test-linked-game-id")
  321. ).rejects.toThrow();
  322. expect(global.fetch).toMatchSnapshot();
  323. });
  324. it("passes thrown error", async () => {
  325. global.fetch.mockImplementation(() => {
  326. throw new Error("error");
  327. });
  328. await expect(
  329. linkGame("test-game-id", "test-linked-game-id")
  330. ).rejects.toThrow();
  331. expect(global.fetch).toMatchSnapshot();
  332. });
  333. });
  334. describe("getFirstSubmitter", () => {
  335. it("gets first submmiter for a round", async () => {
  336. global.fetch.mockImplementation(() =>
  337. Promise.resolve({
  338. ok: true,
  339. json: () =>
  340. Promise.resolve({
  341. first: "test-player",
  342. }),
  343. })
  344. );
  345. expect(
  346. await getFirstSubmitter("test-game-id", "round")
  347. ).toMatchSnapshot();
  348. expect(global.fetch).toMatchSnapshot();
  349. });
  350. it("returns null on server error", async () => {
  351. global.fetch.mockImplementation(() =>
  352. Promise.resolve({
  353. ok: false,
  354. statusText: "failed",
  355. })
  356. );
  357. expect(await getFirstSubmitter("test-game-id", "round")).toBe(null);
  358. expect(global.fetch).toMatchSnapshot();
  359. });
  360. it("passes thrown error", async () => {
  361. global.fetch.mockImplementation(() => {
  362. throw new Error("error");
  363. });
  364. await expect(
  365. getFirstSubmitter("test-game-id", "round")
  366. ).rejects.toThrow();
  367. expect(global.fetch).toMatchSnapshot();
  368. });
  369. });
  370. describe("joinGame", () => {
  371. it("joins game", async () => {
  372. global.fetch.mockImplementation(() =>
  373. Promise.resolve({
  374. ok: true,
  375. json: () =>
  376. Promise.resolve({
  377. playerId: "test-player-id",
  378. }),
  379. })
  380. );
  381. expect(
  382. await joinGame("test-game-id", "test-player-name")
  383. ).toMatchSnapshot();
  384. expect(global.fetch).toMatchSnapshot();
  385. });
  386. it("throws on server error", async () => {
  387. global.fetch.mockImplementation(() =>
  388. Promise.resolve({
  389. ok: false,
  390. statusText: "failed",
  391. })
  392. );
  393. await expect(
  394. joinGame("test-game-id", "test-player-name")
  395. ).rejects.toThrow();
  396. expect(global.fetch).toMatchSnapshot();
  397. });
  398. it("passes thrown error", async () => {
  399. global.fetch.mockImplementation(() => {
  400. throw new Error("error");
  401. });
  402. await expect(
  403. joinGame("test-game-id", "test-player-name")
  404. ).rejects.toThrow();
  405. expect(global.fetch).toMatchSnapshot();
  406. });
  407. });
  408. describe("getCurrentRound", () => {
  409. it("gets current round", async () => {
  410. global.fetch.mockImplementation(() =>
  411. Promise.resolve({
  412. ok: true,
  413. json: () =>
  414. Promise.resolve({
  415. round: "test-round",
  416. }),
  417. })
  418. );
  419. expect(
  420. await getCurrentRound("test-game-id", "test-player-id")
  421. ).toMatchSnapshot();
  422. expect(global.fetch).toMatchSnapshot();
  423. });
  424. it("throws on server error", async () => {
  425. global.fetch.mockImplementation(() =>
  426. Promise.resolve({
  427. ok: false,
  428. statusText: "failed",
  429. })
  430. );
  431. await expect(
  432. getCurrentRound("test-game-id", "test-player-id")
  433. ).rejects.toThrow();
  434. expect(global.fetch).toMatchSnapshot();
  435. });
  436. it("passes thrown error", async () => {
  437. global.fetch.mockImplementation(() => {
  438. throw new Error("error");
  439. });
  440. await expect(
  441. getCurrentRound("test-game-id", "test-player-id")
  442. ).rejects.toThrow();
  443. expect(global.fetch).toMatchSnapshot();
  444. });
  445. });
  446. describe("sendGuess", () => {
  447. it("sends a guess", async () => {
  448. global.fetch.mockImplementation(() =>
  449. Promise.resolve({
  450. ok: true,
  451. json: () =>
  452. Promise.resolve({
  453. score: 1000,
  454. totalScore: 5000,
  455. }),
  456. })
  457. );
  458. expect(
  459. await sendGuess(
  460. "test-game-id",
  461. "test-player-id",
  462. "test-round",
  463. { lat: "lat", lng: "lng" },
  464. "test-time"
  465. )
  466. ).toMatchSnapshot();
  467. expect(global.fetch).toMatchSnapshot();
  468. });
  469. it("throws on server error", async () => {
  470. global.fetch.mockImplementation(() =>
  471. Promise.resolve({
  472. ok: false,
  473. statusText: "failed",
  474. })
  475. );
  476. await expect(
  477. sendGuess(
  478. "test-game-id",
  479. "test-player-id",
  480. "test-round",
  481. { lat: "lat", lng: "lng" },
  482. "test-time"
  483. )
  484. ).rejects.toThrow();
  485. expect(global.fetch).toMatchSnapshot();
  486. });
  487. it("passes thrown error", async () => {
  488. global.fetch.mockImplementation(() => {
  489. throw new Error("error");
  490. });
  491. await expect(
  492. sendGuess(
  493. "test-game-id",
  494. "test-player-id",
  495. "test-round",
  496. { lat: "lat", lng: "lng" },
  497. "test-time"
  498. )
  499. ).rejects.toThrow();
  500. expect(global.fetch).toMatchSnapshot();
  501. });
  502. });
  503. describe("sendtimeout", () => {
  504. it("sends a timeout for the round", async () => {
  505. global.fetch.mockImplementation(() =>
  506. Promise.resolve({
  507. ok: true,
  508. json: () =>
  509. Promise.resolve({
  510. score: 0,
  511. totalScore: 2000,
  512. }),
  513. })
  514. );
  515. expect(
  516. await sendTimeout("test-game-id", "test-player-id", "test-round")
  517. ).toMatchSnapshot();
  518. expect(global.fetch).toMatchSnapshot();
  519. });
  520. it("throws on server error", async () => {
  521. global.fetch.mockImplementation(() =>
  522. Promise.resolve({
  523. ok: false,
  524. statusText: "failed",
  525. })
  526. );
  527. await expect(
  528. sendTimeout("test-game-id", "test-player-id", "test-round")
  529. ).rejects.toThrow();
  530. expect(global.fetch).toMatchSnapshot();
  531. });
  532. it("passes thrown error", async () => {
  533. global.fetch.mockImplementation(() => {
  534. throw new Error("error");
  535. });
  536. await expect(
  537. sendTimeout("test-game-id", "test-player-id", "test-round")
  538. ).rejects.toThrow();
  539. expect(global.fetch).toMatchSnapshot();
  540. });
  541. });
  542. });