Skip to content

Commit

Permalink
Merge pull request #1624 from notaphplover/refactor/extract-use-get-g…
Browse files Browse the repository at this point in the history
…ames-v1-mine

Extract useGetGamesMineWithSpecsV1
  • Loading branch information
notaphplover authored Sep 6, 2024
2 parents 37f8941 + 7dcb5cc commit a1b8ac4
Show file tree
Hide file tree
Showing 9 changed files with 952 additions and 349 deletions.
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);
});
});
});
});
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,
};
}
Loading

0 comments on commit a1b8ac4

Please sign in to comment.