Skip to content
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
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"root": true,
"plugins": ["@typescript-eslint", "prettier", "react", "react-hooks"],
"extends": [
"next/core-web-vitals",
Expand All @@ -18,6 +19,8 @@
"prettier/prettier": "error",
"react/react-in-jsx-scope": "off",
"react-hooks/exhaustive-deps": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-expressions": "off",
"@typescript-eslint/no-unused-vars": "off",
"@next/next/no-html-link-for-pages": "off",
"import/no-named-as-default": "off",
Expand Down
66 changes: 38 additions & 28 deletions __tests__/lib/favorites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
})

// Mock the API functions to test fallback to localStorage
jest.mock('src/api/analysis/analysis', () => ({
updateGameMetadata: jest
.fn()
.mockRejectedValue(new Error('API not available')),
getAnalysisGameList: jest
.fn()
.mockRejectedValue(new Error('API not available')),
}))

describe('favorites', () => {
beforeEach(() => {
localStorageMock.clear()
Expand All @@ -44,74 +54,74 @@ describe('favorites', () => {
}

describe('addFavoriteGame', () => {
it('should add a game to favorites with default name', () => {
const favorite = addFavoriteGame(mockGame)
it('should add a game to favorites with default name', async () => {
const favorite = await addFavoriteGame(mockGame)

expect(favorite.id).toBe(mockGame.id)
expect(favorite.customName).toBe(mockGame.label)
expect(favorite.originalLabel).toBe(mockGame.label)
expect(isFavoriteGame(mockGame.id)).toBe(true)
expect(await isFavoriteGame(mockGame.id)).toBe(true)
})

it('should add a game to favorites with custom name', () => {
it('should add a game to favorites with custom name', async () => {
const customName = 'My Best Game'
const favorite = addFavoriteGame(mockGame, customName)
const favorite = await addFavoriteGame(mockGame, customName)

expect(favorite.customName).toBe(customName)
expect(favorite.originalLabel).toBe(mockGame.label)
})

it('should update existing favorite when added again', () => {
addFavoriteGame(mockGame, 'First Name')
addFavoriteGame(mockGame, 'Updated Name')
it('should update existing favorite when added again', async () => {
await addFavoriteGame(mockGame, 'First Name')
await addFavoriteGame(mockGame, 'Updated Name')

const favorites = getFavoriteGames()
const favorites = await getFavoriteGames()
expect(favorites).toHaveLength(1)
expect(favorites[0].customName).toBe('Updated Name')
})
})

describe('removeFavoriteGame', () => {
it('should remove a game from favorites', () => {
addFavoriteGame(mockGame)
expect(isFavoriteGame(mockGame.id)).toBe(true)
it('should remove a game from favorites', async () => {
await addFavoriteGame(mockGame)
expect(await isFavoriteGame(mockGame.id)).toBe(true)

removeFavoriteGame(mockGame.id)
expect(isFavoriteGame(mockGame.id)).toBe(false)
await removeFavoriteGame(mockGame.id, mockGame.type)
expect(await isFavoriteGame(mockGame.id)).toBe(false)
})
})

describe('updateFavoriteName', () => {
it('should update favorite name', () => {
addFavoriteGame(mockGame, 'Original Name')
updateFavoriteName(mockGame.id, 'New Name')
it('should update favorite name', async () => {
await addFavoriteGame(mockGame, 'Original Name')
await updateFavoriteName(mockGame.id, 'New Name', mockGame.type)

const favorite = getFavoriteGame(mockGame.id)
const favorite = await getFavoriteGame(mockGame.id)
expect(favorite?.customName).toBe('New Name')
})

it('should do nothing if favorite does not exist', () => {
const initialFavorites = getFavoriteGames()
updateFavoriteName('non-existent', 'New Name')
it('should do nothing if favorite does not exist', async () => {
const initialFavorites = await getFavoriteGames()
await updateFavoriteName('non-existent', 'New Name')

expect(getFavoriteGames()).toEqual(initialFavorites)
expect(await getFavoriteGames()).toEqual(initialFavorites)
})
})

describe('getFavoritesAsWebGames', () => {
it('should convert favorites to web games', () => {
it('should convert favorites to web games', async () => {
const customName = 'Custom Game Name'
addFavoriteGame(mockGame, customName)
await addFavoriteGame(mockGame, customName)

const webGames = getFavoritesAsWebGames()
const webGames = await getFavoritesAsWebGames()
expect(webGames).toHaveLength(1)
expect(webGames[0].label).toBe(customName)
expect(webGames[0].id).toBe(mockGame.id)
})
})

describe('storage limits', () => {
it('should limit favorites to 100 entries', () => {
it('should limit favorites to 100 entries', async () => {
// Add 101 favorites
for (let i = 0; i < 101; i++) {
const game: AnalysisWebGame = {
Expand All @@ -120,10 +130,10 @@ describe('favorites', () => {
label: `Game ${i}`,
result: '1-0',
}
addFavoriteGame(game)
await addFavoriteGame(game)
}

const favorites = getFavoriteGames()
const favorites = await getFavoriteGames()
expect(favorites).toHaveLength(100)
// Latest should be at the top
expect(favorites[0].id).toBe('game-100')
Expand Down
Loading