diff --git a/Backend/Schema.ts b/Backend/Schema.ts index 398d8ac..02e551f 100644 --- a/Backend/Schema.ts +++ b/Backend/Schema.ts @@ -65,7 +65,6 @@ export const typeDefs = `#graphql game(appid: Int!): Game genres: [String!]! profile(userId: Int!): Profile - gameOptions: [GameOption!]! totalGameCount( search: String genres: [String!] diff --git a/Backend/resolvers/GameResolvers.ts b/Backend/resolvers/GameResolvers.ts index 143d514..9ce697f 100644 --- a/Backend/resolvers/GameResolvers.ts +++ b/Backend/resolvers/GameResolvers.ts @@ -124,13 +124,6 @@ export const gameQueryResolvers = { genres: () => FetchGenres(), - gameOptions: async () => { - const { rows } = await pool.query( - `SELECT game_id AS appid, game_name AS name FROM games ORDER BY game_name` - ) - return rows - }, - top3Games: () => FetchTop3Games(), topGamesByGenre: (_parent: unknown, { genre }: { genre: string }) => FetchTop10GamesPerGenre(genre), diff --git a/Frontend/src/components/ProfileEditor/EditProfile.css b/Frontend/src/components/ProfileEditor/EditProfile.css index bbd4d4e..6c92185 100644 --- a/Frontend/src/components/ProfileEditor/EditProfile.css +++ b/Frontend/src/components/ProfileEditor/EditProfile.css @@ -57,6 +57,12 @@ width: 100%; } +.profile-game-listbox.MuiAutocomplete-listbox + .profile-game-option.MuiAutocomplete-option[aria-selected='true'], +.MuiMenu-list .profile-setup-option.MuiMenuItem-root.Mui-selected { + background-color: var(--secondary-component-color); +} + /* Field borders and the focused label colour are styled in Global.css */ .page-shell .profile-card .MuiInputLabel-root { color: var(--primary-text-color); diff --git a/Frontend/src/components/ProfileEditor/ProfileEditor.tsx b/Frontend/src/components/ProfileEditor/ProfileEditor.tsx index bbda504..c3c2fab 100644 --- a/Frontend/src/components/ProfileEditor/ProfileEditor.tsx +++ b/Frontend/src/components/ProfileEditor/ProfileEditor.tsx @@ -24,7 +24,7 @@ interface ProfileCardProps { onSave: (input: { description: string | null setup: string | null - favouriteGame: number | null + favouriteGame: GameOption | null }) => Promise } @@ -124,7 +124,7 @@ export default function ProfileCard({ profile, gameOptions, onSave }: ProfileCar await onSave({ description: description.trim() || null, setup: setup || null, - favouriteGame: game?.appid ?? null + favouriteGame: game }) setMessage({ type: 'success', text: 'Profile saved.' }) } catch (err) { @@ -195,6 +195,7 @@ export default function ProfileCard({ profile, gameOptions, onSave }: ProfileCar filterOptions={(options) => options} slotProps={{ listbox: { + className: 'profile-game-listbox', onScroll: (event) => { const listbox = event.currentTarget if ( @@ -211,7 +212,11 @@ export default function ProfileCard({ profile, gameOptions, onSave }: ProfileCar renderOption={(props, option) => { const { key: _key, ...rest } = props return ( -
  • +
  • {option.name}
  • ) @@ -234,9 +239,11 @@ export default function ProfileCard({ profile, gameOptions, onSave }: ProfileCar value={setup} onChange={(e) => setSetup(e.target.value)} > - None + + None + {SETUP_OPTIONS.map((option) => ( - + {option} ))} diff --git a/Frontend/src/pages/ProfilePage/ProfilePage.tsx b/Frontend/src/pages/ProfilePage/ProfilePage.tsx index 5f7c31b..eb3b329 100644 --- a/Frontend/src/pages/ProfilePage/ProfilePage.tsx +++ b/Frontend/src/pages/ProfilePage/ProfilePage.tsx @@ -77,10 +77,22 @@ export default function ProfilePage() { async function HandleSave(input: { description: string | null setup: string | null - favouriteGame: number | null + favouriteGame: GameOption | null }) { if (userId === undefined) return - setProfile(await UpdateProfileFrontend(userId, input)) + + const updatedProfile = await UpdateProfileFrontend(userId, { + description: input.description, + setup: input.setup, + favouriteGame: input.favouriteGame?.appid ?? null + }) + + setProfile(updatedProfile) + + const savedGame = + updatedProfile.favouriteGame === input.favouriteGame?.appid ? input.favouriteGame : null + + setGameOptions(savedGame ? [savedGame] : []) setIsEditing(false) } diff --git a/Frontend/src/tests/ProfilePage.test.tsx b/Frontend/src/tests/ProfilePage.test.tsx new file mode 100644 index 0000000..2dbcb1a --- /dev/null +++ b/Frontend/src/tests/ProfilePage.test.tsx @@ -0,0 +1,138 @@ +// @vitest-environment jsdom +import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type { Profile } from '../../../Backend/logic/ProfileLogicBackend' +import type { GameOption } from '../FrontendLogic/ProfileLogicFrontend' + +type ProfileUpdate = { + description: string | null + setup: string | null + favouriteGame: number | null +} + +type ProfileSave = Omit & { + favouriteGame: GameOption | null +} + +const { FetchGameOptions, FetchProfileFrontend, UpdateProfileFrontend, UseAuth } = vi.hoisted( + () => ({ + FetchGameOptions: + vi.fn<(options?: { appid?: number; limit?: number }) => Promise>(), + FetchProfileFrontend: vi.fn<(userId: number) => Promise>(), + UpdateProfileFrontend: vi.fn<(userId: number, input: ProfileUpdate) => Promise>(), + UseAuth: vi.fn<() => { user: { id: number } | null }>() + }) +) + +vi.mock('../FrontendLogic/ProfileLogicFrontend', () => ({ + FetchGameOptions, + FetchProfileFrontend, + UpdateProfileFrontend +})) +vi.mock('../context/AuthContext', () => ({ UseAuth })) +vi.mock('../components/Header/Header', () => ({ default: () => null })) +vi.mock('../components/Footer/Footer', () => ({ default: () => null })) +vi.mock('../components/PageHeading/PageHeading', () => ({ + default: ({ title, actions }: { title: string; actions?: React.ReactNode }) => ( +
    +

    {title}

    + {actions} +
    + ) +})) +vi.mock('../components/ProfileCard', () => ({ + default: ({ favouriteGame }: { favouriteGame: GameOption | null }) => ( +

    {favouriteGame?.name ?? 'No favourite game provided'}

    + ) +})) +vi.mock('../components/ProfileEditor/ProfileEditor', () => ({ + default: ({ onSave }: { onSave: (input: ProfileSave) => Promise }) => ( + <> + + + + ) +})) +vi.mock('../components/UserReviews/UserReviews', () => ({ default: () => null })) + +import ProfilePage from '../pages/ProfilePage/ProfilePage' + +const INITIAL_PROFILE: Profile = { + username: 'test-user', + description: 'Original profile', + setup: 'PC', + favouriteGame: 570 +} + +describe('ProfilePage favorite game saving', () => { + beforeEach(() => { + FetchGameOptions.mockReset() + FetchProfileFrontend.mockReset() + UpdateProfileFrontend.mockReset() + UseAuth.mockReset() + + UseAuth.mockReturnValue({ user: { id: 7 } }) + FetchProfileFrontend.mockResolvedValue(INITIAL_PROFILE) + FetchGameOptions.mockResolvedValue([{ appid: 570, name: 'Dota 2' }]) + UpdateProfileFrontend.mockImplementation(async (_userId, input) => ({ + ...INITIAL_PROFILE, + ...input + })) + }) + + afterEach(() => cleanup()) + + it('saves the selected game id and immediately displays its name', async () => { + render() + + expect(await screen.findByText('Dota 2')).toBeTruthy() + fireEvent.click(screen.getByRole('button', { name: 'Edit profile' })) + fireEvent.click(screen.getByRole('button', { name: 'Save selected game' })) + + await waitFor(() => { + expect(UpdateProfileFrontend).toHaveBeenCalledWith(7, { + description: 'Updated profile', + setup: 'PC', + favouriteGame: 730 + }) + }) + expect(await screen.findByText('Counter-Strike 2')).toBeTruthy() + }) + + it('clears the displayed favorite game when the selection is removed', async () => { + render() + + expect(await screen.findByText('Dota 2')).toBeTruthy() + fireEvent.click(screen.getByRole('button', { name: 'Edit profile' })) + fireEvent.click(screen.getByRole('button', { name: 'Clear favorite game' })) + + await waitFor(() => { + expect(UpdateProfileFrontend).toHaveBeenCalledWith(7, { + description: 'Updated profile', + setup: 'PC', + favouriteGame: null + }) + }) + expect(await screen.findByText('No favourite game provided')).toBeTruthy() + }) +}) diff --git a/tsconfig.app.json b/tsconfig.app.json index d209b68..06b9374 100644 --- a/tsconfig.app.json +++ b/tsconfig.app.json @@ -38,6 +38,7 @@ "Frontend/src/tests/TokenFrontend.test.ts", "Frontend/src/tests/PageHeading.test.tsx", "Frontend/src/tests/ProfileLogicFrontend.test.ts", + "Frontend/src/tests/ProfilePage.test.tsx", "Frontend/src/tests/PassiveGameCard.test.tsx", "Frontend/src/tests/ReviewCard.test.tsx", "Frontend/src/tests/ReviewLogicFrontend.test.ts",