-
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 #1663 from notaphplover/refactor/add-game-hooks
Add game hooks
- Loading branch information
Showing
8 changed files
with
275 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
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
90 changes: 90 additions & 0 deletions
90
packages/frontend/web-ui/src/game/hooks/useGetGamesV1GameId.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,90 @@ | ||
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals'; | ||
|
||
jest.mock('../../common/helpers/mapUseQueryHookResult'); | ||
jest.mock('../../common/http/services/cornieApi'); | ||
|
||
import { models as apiModels } from '@cornie-js/api-models'; | ||
import { renderHook, RenderHookResult } from '@testing-library/react'; | ||
|
||
import { | ||
mapUseQueryHookResult, | ||
UseQueryStateResult, | ||
} from '../../common/helpers/mapUseQueryHookResult'; | ||
import { cornieApi } from '../../common/http/services/cornieApi'; | ||
import { Either } from '../../common/models/Either'; | ||
import { | ||
useGetGamesV1GameId, | ||
UseGetGamesV1GameIdResult, | ||
} from './useGetGamesV1GameId'; | ||
|
||
describe(useGetGamesV1GameId.name, () => { | ||
describe('when called', () => { | ||
let gameIdFixture: string; | ||
let useQueryStateResultFixture: UseQueryStateResult< | ||
apiModels.GameV1 | undefined | ||
> & { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
refetch: () => any; | ||
}; | ||
let mapUseQueryHookResultResult: Either<string, apiModels.GameV1> | null; | ||
|
||
let renderResult: RenderHookResult<UseGetGamesV1GameIdResult, unknown>; | ||
|
||
beforeAll(() => { | ||
gameIdFixture = 'game-id-fixture'; | ||
useQueryStateResultFixture = { | ||
data: undefined, | ||
error: undefined, | ||
isLoading: true, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
refetch: jest.fn<any>(), | ||
}; | ||
mapUseQueryHookResultResult = null; | ||
|
||
( | ||
cornieApi.useGetGamesV1GameIdQuery as jest.Mock< | ||
typeof cornieApi.useGetGamesV1GameIdQuery | ||
> | ||
).mockReturnValueOnce(useQueryStateResultFixture); | ||
|
||
( | ||
mapUseQueryHookResult as jest.Mock<typeof mapUseQueryHookResult> | ||
).mockReturnValueOnce(mapUseQueryHookResultResult); | ||
|
||
renderResult = renderHook(() => useGetGamesV1GameId(gameIdFixture)); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call cornieApi.useGetGamesV1GameIdQuery()', () => { | ||
expect(cornieApi.useGetGamesV1GameIdQuery).toHaveBeenCalledTimes(1); | ||
expect(cornieApi.useGetGamesV1GameIdQuery).toHaveBeenCalledWith( | ||
{ | ||
params: [ | ||
{ | ||
gameId: gameIdFixture, | ||
}, | ||
], | ||
}, | ||
{ skip: false }, | ||
); | ||
}); | ||
|
||
it('should call mapUseQueryHookResult()', () => { | ||
expect(mapUseQueryHookResult).toHaveBeenCalledTimes(1); | ||
expect(mapUseQueryHookResult).toHaveBeenCalledWith( | ||
useQueryStateResultFixture, | ||
); | ||
}); | ||
|
||
it('should return expected result', () => { | ||
const expected: UseGetGamesV1GameIdResult = { | ||
result: mapUseQueryHookResultResult, | ||
}; | ||
|
||
expect(renderResult.result.current).toStrictEqual(expected); | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
packages/frontend/web-ui/src/game/hooks/useGetGamesV1GameId.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,32 @@ | ||
import { models as apiModels } from '@cornie-js/api-models'; | ||
|
||
import { mapUseQueryHookResult } from '../../common/helpers/mapUseQueryHookResult'; | ||
import { cornieApi } from '../../common/http/services/cornieApi'; | ||
import { Either } from '../../common/models/Either'; | ||
|
||
export interface UseGetGamesV1GameIdResult { | ||
result: Either<string, apiModels.GameV1> | null; | ||
} | ||
|
||
export function useGetGamesV1GameId( | ||
gameId: string | null, | ||
): UseGetGamesV1GameIdResult { | ||
const useGetUsersV1MeQueryResult = cornieApi.useGetGamesV1GameIdQuery( | ||
{ | ||
params: [ | ||
{ | ||
gameId: gameId ?? '', | ||
}, | ||
], | ||
}, | ||
{ | ||
skip: gameId === null, | ||
}, | ||
); | ||
|
||
const result: Either<string, apiModels.GameV1> | null = mapUseQueryHookResult( | ||
useGetUsersV1MeQueryResult, | ||
); | ||
|
||
return { result }; | ||
} |
106 changes: 106 additions & 0 deletions
106
packages/frontend/web-ui/src/game/hooks/useGetGamesV1GameIdSlotsSlotIdCards.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,106 @@ | ||
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals'; | ||
|
||
jest.mock('../../common/helpers/mapUseQueryHookResult'); | ||
jest.mock('../../common/http/services/cornieApi'); | ||
|
||
import { models as apiModels } from '@cornie-js/api-models'; | ||
import { renderHook, RenderHookResult } from '@testing-library/react'; | ||
|
||
import { | ||
mapUseQueryHookResult, | ||
UseQueryStateResult, | ||
} from '../../common/helpers/mapUseQueryHookResult'; | ||
import { cornieApi } from '../../common/http/services/cornieApi'; | ||
import { Either } from '../../common/models/Either'; | ||
import { | ||
useGetGamesV1GameIdSlotsSlotIdCards, | ||
UseGetGamesV1GameIdSlotsSlotIdCardsResult, | ||
} from './useGetGamesV1GameIdSlotsSlotIdCards'; | ||
|
||
describe(useGetGamesV1GameIdSlotsSlotIdCards.name, () => { | ||
describe('when called', () => { | ||
let gameIdFixture: string; | ||
let gameSlotIndexFixture: string; | ||
let useQueryStateResultFixture: UseQueryStateResult<apiModels.CardArrayV1> & { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
refetch: () => any; | ||
}; | ||
let mapUseQueryHookResultResult: Either< | ||
string, | ||
apiModels.CardArrayV1 | ||
> | null; | ||
|
||
let renderResult: RenderHookResult< | ||
UseGetGamesV1GameIdSlotsSlotIdCardsResult, | ||
unknown | ||
>; | ||
|
||
beforeAll(() => { | ||
gameIdFixture = 'game-id-fixture'; | ||
gameSlotIndexFixture = '1'; | ||
useQueryStateResultFixture = { | ||
data: undefined, | ||
error: undefined, | ||
isLoading: true, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
refetch: jest.fn<any>(), | ||
}; | ||
mapUseQueryHookResultResult = null; | ||
|
||
( | ||
cornieApi.useGetGamesV1GameIdSlotsSlotIdCardsQuery as jest.Mock< | ||
typeof cornieApi.useGetGamesV1GameIdSlotsSlotIdCardsQuery | ||
> | ||
).mockReturnValueOnce(useQueryStateResultFixture); | ||
|
||
( | ||
mapUseQueryHookResult as jest.Mock<typeof mapUseQueryHookResult> | ||
).mockReturnValueOnce(mapUseQueryHookResultResult); | ||
|
||
renderResult = renderHook(() => | ||
useGetGamesV1GameIdSlotsSlotIdCards( | ||
gameIdFixture, | ||
gameSlotIndexFixture, | ||
), | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call cornieApi.useGetGamesV1GameIdSlotsSlotIdCardsQuery()', () => { | ||
expect( | ||
cornieApi.useGetGamesV1GameIdSlotsSlotIdCardsQuery, | ||
).toHaveBeenCalledTimes(1); | ||
expect( | ||
cornieApi.useGetGamesV1GameIdSlotsSlotIdCardsQuery, | ||
).toHaveBeenCalledWith( | ||
{ | ||
params: [ | ||
{ | ||
gameId: gameIdFixture, | ||
gameSlotIndex: gameSlotIndexFixture, | ||
}, | ||
], | ||
}, | ||
{ skip: false }, | ||
); | ||
}); | ||
|
||
it('should call mapUseQueryHookResult()', () => { | ||
expect(mapUseQueryHookResult).toHaveBeenCalledTimes(1); | ||
expect(mapUseQueryHookResult).toHaveBeenCalledWith( | ||
useQueryStateResultFixture, | ||
); | ||
}); | ||
|
||
it('should return expected result', () => { | ||
const expected: UseGetGamesV1GameIdSlotsSlotIdCardsResult = { | ||
result: mapUseQueryHookResultResult, | ||
}; | ||
|
||
expect(renderResult.result.current).toStrictEqual(expected); | ||
}); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
packages/frontend/web-ui/src/game/hooks/useGetGamesV1GameIdSlotsSlotIdCards.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,34 @@ | ||
import { models as apiModels } from '@cornie-js/api-models'; | ||
|
||
import { mapUseQueryHookResult } from '../../common/helpers/mapUseQueryHookResult'; | ||
import { cornieApi } from '../../common/http/services/cornieApi'; | ||
import { Either } from '../../common/models/Either'; | ||
|
||
export interface UseGetGamesV1GameIdSlotsSlotIdCardsResult { | ||
result: Either<string, apiModels.CardArrayV1> | null; | ||
} | ||
|
||
export const useGetGamesV1GameIdSlotsSlotIdCards = ( | ||
gameId: string | null, | ||
gameSlotIndex: string | null, | ||
): UseGetGamesV1GameIdSlotsSlotIdCardsResult => { | ||
const useGetUsersV1MeQueryResult = | ||
cornieApi.useGetGamesV1GameIdSlotsSlotIdCardsQuery( | ||
{ | ||
params: [ | ||
{ | ||
gameId: gameId ?? '', | ||
gameSlotIndex: gameSlotIndex ?? '0', | ||
}, | ||
], | ||
}, | ||
{ | ||
skip: gameId === null || gameSlotIndex === null, | ||
}, | ||
); | ||
|
||
const result: Either<string, apiModels.CardArrayV1> | null = | ||
mapUseQueryHookResult(useGetUsersV1MeQueryResult); | ||
|
||
return { result }; | ||
}; |