Skip to content

Commit e2bcbcd

Browse files
Merge pull request #208 from CSSLab/feature/migrate-favorites-to-backend
Migrate favourite game storage to backend API
2 parents 4edda92 + 2d5067a commit e2bcbcd

11 files changed

Lines changed: 1081 additions & 602 deletions

File tree

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"root": true,
23
"plugins": ["@typescript-eslint", "prettier", "react", "react-hooks"],
34
"extends": [
45
"next/core-web-vitals",
@@ -18,6 +19,8 @@
1819
"prettier/prettier": "error",
1920
"react/react-in-jsx-scope": "off",
2021
"react-hooks/exhaustive-deps": "off",
22+
"@typescript-eslint/no-explicit-any": "off",
23+
"@typescript-eslint/no-unused-expressions": "off",
2124
"@typescript-eslint/no-unused-vars": "off",
2225
"@next/next/no-html-link-for-pages": "off",
2326
"import/no-named-as-default": "off",

__tests__/lib/favorites.test.ts

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ Object.defineProperty(window, 'localStorage', {
3131
value: localStorageMock,
3232
})
3333

34+
// Mock the API functions to test fallback to localStorage
35+
jest.mock('src/api/analysis/analysis', () => ({
36+
updateGameMetadata: jest
37+
.fn()
38+
.mockRejectedValue(new Error('API not available')),
39+
getAnalysisGameList: jest
40+
.fn()
41+
.mockRejectedValue(new Error('API not available')),
42+
}))
43+
3444
describe('favorites', () => {
3545
beforeEach(() => {
3646
localStorageMock.clear()
@@ -44,74 +54,74 @@ describe('favorites', () => {
4454
}
4555

4656
describe('addFavoriteGame', () => {
47-
it('should add a game to favorites with default name', () => {
48-
const favorite = addFavoriteGame(mockGame)
57+
it('should add a game to favorites with default name', async () => {
58+
const favorite = await addFavoriteGame(mockGame)
4959

5060
expect(favorite.id).toBe(mockGame.id)
5161
expect(favorite.customName).toBe(mockGame.label)
5262
expect(favorite.originalLabel).toBe(mockGame.label)
53-
expect(isFavoriteGame(mockGame.id)).toBe(true)
63+
expect(await isFavoriteGame(mockGame.id)).toBe(true)
5464
})
5565

56-
it('should add a game to favorites with custom name', () => {
66+
it('should add a game to favorites with custom name', async () => {
5767
const customName = 'My Best Game'
58-
const favorite = addFavoriteGame(mockGame, customName)
68+
const favorite = await addFavoriteGame(mockGame, customName)
5969

6070
expect(favorite.customName).toBe(customName)
6171
expect(favorite.originalLabel).toBe(mockGame.label)
6272
})
6373

64-
it('should update existing favorite when added again', () => {
65-
addFavoriteGame(mockGame, 'First Name')
66-
addFavoriteGame(mockGame, 'Updated Name')
74+
it('should update existing favorite when added again', async () => {
75+
await addFavoriteGame(mockGame, 'First Name')
76+
await addFavoriteGame(mockGame, 'Updated Name')
6777

68-
const favorites = getFavoriteGames()
78+
const favorites = await getFavoriteGames()
6979
expect(favorites).toHaveLength(1)
7080
expect(favorites[0].customName).toBe('Updated Name')
7181
})
7282
})
7383

7484
describe('removeFavoriteGame', () => {
75-
it('should remove a game from favorites', () => {
76-
addFavoriteGame(mockGame)
77-
expect(isFavoriteGame(mockGame.id)).toBe(true)
85+
it('should remove a game from favorites', async () => {
86+
await addFavoriteGame(mockGame)
87+
expect(await isFavoriteGame(mockGame.id)).toBe(true)
7888

79-
removeFavoriteGame(mockGame.id)
80-
expect(isFavoriteGame(mockGame.id)).toBe(false)
89+
await removeFavoriteGame(mockGame.id, mockGame.type)
90+
expect(await isFavoriteGame(mockGame.id)).toBe(false)
8191
})
8292
})
8393

8494
describe('updateFavoriteName', () => {
85-
it('should update favorite name', () => {
86-
addFavoriteGame(mockGame, 'Original Name')
87-
updateFavoriteName(mockGame.id, 'New Name')
95+
it('should update favorite name', async () => {
96+
await addFavoriteGame(mockGame, 'Original Name')
97+
await updateFavoriteName(mockGame.id, 'New Name', mockGame.type)
8898

89-
const favorite = getFavoriteGame(mockGame.id)
99+
const favorite = await getFavoriteGame(mockGame.id)
90100
expect(favorite?.customName).toBe('New Name')
91101
})
92102

93-
it('should do nothing if favorite does not exist', () => {
94-
const initialFavorites = getFavoriteGames()
95-
updateFavoriteName('non-existent', 'New Name')
103+
it('should do nothing if favorite does not exist', async () => {
104+
const initialFavorites = await getFavoriteGames()
105+
await updateFavoriteName('non-existent', 'New Name')
96106

97-
expect(getFavoriteGames()).toEqual(initialFavorites)
107+
expect(await getFavoriteGames()).toEqual(initialFavorites)
98108
})
99109
})
100110

101111
describe('getFavoritesAsWebGames', () => {
102-
it('should convert favorites to web games', () => {
112+
it('should convert favorites to web games', async () => {
103113
const customName = 'Custom Game Name'
104-
addFavoriteGame(mockGame, customName)
114+
await addFavoriteGame(mockGame, customName)
105115

106-
const webGames = getFavoritesAsWebGames()
116+
const webGames = await getFavoritesAsWebGames()
107117
expect(webGames).toHaveLength(1)
108118
expect(webGames[0].label).toBe(customName)
109119
expect(webGames[0].id).toBe(mockGame.id)
110120
})
111121
})
112122

113123
describe('storage limits', () => {
114-
it('should limit favorites to 100 entries', () => {
124+
it('should limit favorites to 100 entries', async () => {
115125
// Add 101 favorites
116126
for (let i = 0; i < 101; i++) {
117127
const game: AnalysisWebGame = {
@@ -120,10 +130,10 @@ describe('favorites', () => {
120130
label: `Game ${i}`,
121131
result: '1-0',
122132
}
123-
addFavoriteGame(game)
133+
await addFavoriteGame(game)
124134
}
125135

126-
const favorites = getFavoriteGames()
136+
const favorites = await getFavoriteGames()
127137
expect(favorites).toHaveLength(100)
128138
// Latest should be at the top
129139
expect(favorites[0].id).toBe('game-100')

0 commit comments

Comments
 (0)