-
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.
Merge pull request #1624 from notaphplover/refactor/extract-use-get-g…
…ames-v1-mine Extract useGetGamesMineWithSpecsV1
- Loading branch information
Showing
9 changed files
with
952 additions
and
349 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
packages/frontend/web-ui/src/game/helpers/buildGameWithSpecPairArrayResult.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,129 @@ | ||
import { beforeAll, describe, expect, it } from '@jest/globals'; | ||
|
||
import { models as apiModels } from '@cornie-js/api-models'; | ||
|
||
import { Left, Right } from '../../common/models/Either'; | ||
import { GameWithSpecPair } from '../models/GameWithSpecPair'; | ||
import { buildGameWithSpecPairArrayResult } from './buildGameWithSpecPairArrayResult'; | ||
|
||
describe(buildGameWithSpecPairArrayResult.name, () => { | ||
describe('having gamesV1Result null or gamesSpecsV1Result null', () => { | ||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
result = buildGameWithSpecPairArrayResult(null, null); | ||
}); | ||
|
||
it('should return null', () => { | ||
expect(result).toBeNull(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('having Left gamesV1Result or Left gamesSpecsV1Result', () => { | ||
let gamesV1Result: Left<string>; | ||
let gamesSpecsV1Result: Left<string>; | ||
|
||
beforeAll(() => { | ||
gamesV1Result = { | ||
isRight: false, | ||
value: 'games-v1-result-fixture', | ||
}; | ||
|
||
gamesSpecsV1Result = { | ||
isRight: false, | ||
value: 'games-specs-v1-result-fixture', | ||
}; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
result = buildGameWithSpecPairArrayResult( | ||
gamesV1Result, | ||
gamesSpecsV1Result, | ||
); | ||
}); | ||
|
||
it('should return Left', () => { | ||
const expected: Left<string> = { | ||
isRight: false, | ||
value: `${gamesV1Result.value}\n${gamesSpecsV1Result.value}`, | ||
}; | ||
|
||
expect(result).toStrictEqual(expected); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('having Right gamesV1Result and Right gamesSpecsV1Result', () => { | ||
let gamesV1Result: Right<[apiModels.GameV1]>; | ||
let gamesSpecsV1Result: Right<[apiModels.GameSpecV1]>; | ||
|
||
beforeAll(() => { | ||
gamesV1Result = { | ||
isRight: true, | ||
value: [ | ||
{ | ||
id: 'game-id', | ||
isPublic: true, | ||
state: { | ||
slots: [], | ||
status: 'nonStarted', | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
gamesSpecsV1Result = { | ||
isRight: true, | ||
value: [ | ||
{ | ||
cardSpecs: [], | ||
gameId: 'game-id', | ||
gameSlotsAmount: 2, | ||
options: { | ||
chainDraw2Draw2Cards: false, | ||
chainDraw2Draw4Cards: false, | ||
chainDraw4Draw2Cards: false, | ||
chainDraw4Draw4Cards: false, | ||
playCardIsMandatory: false, | ||
playMultipleSameCards: false, | ||
playWildDraw4IfNoOtherAlternative: true, | ||
}, | ||
}, | ||
], | ||
}; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
result = buildGameWithSpecPairArrayResult( | ||
gamesV1Result, | ||
gamesSpecsV1Result, | ||
); | ||
}); | ||
|
||
it('should return Right', () => { | ||
const [game]: [apiModels.GameV1] = gamesV1Result.value; | ||
const [spec]: [apiModels.GameSpecV1] = gamesSpecsV1Result.value; | ||
|
||
const expected: Right<GameWithSpecPair[]> = { | ||
isRight: true, | ||
value: [ | ||
{ | ||
game, | ||
spec, | ||
}, | ||
], | ||
}; | ||
|
||
expect(result).toStrictEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
packages/frontend/web-ui/src/game/helpers/buildGameWithSpecPairArrayResult.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,52 @@ | ||
import { models as apiModels } from '@cornie-js/api-models'; | ||
|
||
import { Either, Left } from '../../common/models/Either'; | ||
import { GameWithSpecPair } from '../models/GameWithSpecPair'; | ||
|
||
export function buildGameWithSpecPairArrayResult( | ||
gamesV1Result: Either<string, apiModels.GameArrayV1> | null, | ||
gamesSpecsV1Result: Either<string, apiModels.GameSpecArrayV1> | null, | ||
): Either<string, GameWithSpecPair[]> | null { | ||
if (gamesSpecsV1Result === null || gamesV1Result === null) { | ||
return null; | ||
} | ||
|
||
if (!gamesSpecsV1Result.isRight || !gamesV1Result.isRight) { | ||
const leftovers: string[] = [gamesV1Result, gamesSpecsV1Result] | ||
.filter( | ||
(result: Either<string, unknown>): result is Left<string> => | ||
!result.isRight, | ||
) | ||
.map((result: Left<string>): string => result.value); | ||
|
||
return { | ||
isRight: false, | ||
value: leftovers.join('\n'), | ||
}; | ||
} | ||
|
||
if (gamesV1Result.value.length !== gamesSpecsV1Result.value.length) { | ||
return { | ||
isRight: false, | ||
value: 'Unable to fetch games data', | ||
}; | ||
} | ||
|
||
const gameWithSpecPairArray: GameWithSpecPair[] = gamesV1Result.value.map( | ||
(gameV1: apiModels.GameV1, index: number): GameWithSpecPair => { | ||
const gameSpecV1: apiModels.GameSpecV1 = gamesSpecsV1Result.value[ | ||
index | ||
] as apiModels.GameSpecV1; | ||
|
||
return { | ||
game: gameV1, | ||
spec: gameSpecV1, | ||
}; | ||
}, | ||
); | ||
|
||
return { | ||
isRight: true, | ||
value: gameWithSpecPairArray, | ||
}; | ||
} |
Oops, something went wrong.