-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(web-ui): add getGameSlotIndex
- Loading branch information
1 parent
64980e5
commit 9ffe4f5
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
packages/frontend/web-ui/src/game/helpers/getGameSlotIndex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |