Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract useGetGamesMineWithSpecsV1 #1624

Merged
merged 6 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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