From 9ffe4f5588d37ffb3c92eebbe3fde8a4ea7be8f8 Mon Sep 17 00:00:00 2001 From: notaphplover Date: Mon, 23 Sep 2024 19:32:49 +0200 Subject: [PATCH] refactor(web-ui): add getGameSlotIndex --- .../src/game/helpers/getGameSlotIndex.spec.ts | 81 +++++++++++++++++++ .../src/game/helpers/getGameSlotIndex.ts | 23 ++++++ 2 files changed, 104 insertions(+) create mode 100644 packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.spec.ts create mode 100644 packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.ts diff --git a/packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.spec.ts b/packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.spec.ts new file mode 100644 index 000000000..1b9a78001 --- /dev/null +++ b/packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.spec.ts @@ -0,0 +1,81 @@ +import { beforeAll, describe, expect, it } from '@jest/globals'; + +import { models as apiModels } from '@cornie-js/api-models'; + +import { getGameSlotIndex } from './getGameSlotIndex'; + +describe(getGameSlotIndex.name, () => { + describe('having a user and a game without a slot belonging to that user', () => { + let gameFixture: apiModels.GameV1; + + let userFixture: apiModels.UserV1; + + beforeAll(() => { + gameFixture = { + id: 'id-fixture', + isPublic: true, + state: { + slots: [], + status: 'nonStarted', + }, + }; + + userFixture = { + active: true, + id: 'id-fixture', + name: 'name-fixture', + }; + }); + + describe('when called', () => { + let result: unknown; + + beforeAll(() => { + result = getGameSlotIndex(gameFixture, userFixture); + }); + + it('should return expected result', () => { + expect(result).toBeNull(); + }); + }); + }); + + describe('having a user and a game with a slot belonging to that user', () => { + let gameFixture: apiModels.GameV1; + + let userFixture: apiModels.UserV1; + + beforeAll(() => { + userFixture = { + active: true, + id: 'id-fixture', + name: 'name-fixture', + }; + + gameFixture = { + id: 'id-fixture', + isPublic: true, + state: { + slots: [ + { + userId: userFixture.id, + }, + ], + status: 'nonStarted', + }, + }; + }); + + describe('when called', () => { + let result: unknown; + + beforeAll(() => { + result = getGameSlotIndex(gameFixture, userFixture); + }); + + it('should return expected result', () => { + expect(result).toBe('0'); + }); + }); + }); +}); diff --git a/packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.ts b/packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.ts new file mode 100644 index 000000000..ca712e888 --- /dev/null +++ b/packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.ts @@ -0,0 +1,23 @@ +import { models as apiModels } from '@cornie-js/api-models'; + +export function getGameSlotIndex( + game: apiModels.GameV1 | undefined, + user: apiModels.UserV1, +): string | null { + if (game === undefined) { + return null; + } + + for (let i: number = 0; i < game.state.slots.length; ++i) { + const gameSlot: apiModels.ActiveGameSlotV1 | apiModels.FinishedGameSlotV1 = + game.state.slots[i] as + | apiModels.ActiveGameSlotV1 + | apiModels.FinishedGameSlotV1; + + if (gameSlot.userId === user.id) { + return i.toString(); + } + } + + return null; +}