Skip to content

Commit

Permalink
refactor(web-ui): add getGameSlotIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
notaphplover committed Sep 23, 2024
1 parent 64980e5 commit 9ffe4f5
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
});
23 changes: 23 additions & 0 deletions packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 9ffe4f5

Please sign in to comment.