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
1 change: 0 additions & 1 deletion Backend/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!]
Expand Down
7 changes: 0 additions & 7 deletions Backend/resolvers/GameResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions Frontend/src/components/ProfileEditor/EditProfile.css
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 12 additions & 5 deletions Frontend/src/components/ProfileEditor/ProfileEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface ProfileCardProps {
onSave: (input: {
description: string | null
setup: string | null
favouriteGame: number | null
favouriteGame: GameOption | null
}) => Promise<void>
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 (
Expand All @@ -211,7 +212,11 @@ export default function ProfileCard({ profile, gameOptions, onSave }: ProfileCar
renderOption={(props, option) => {
const { key: _key, ...rest } = props
return (
<li key={option.appid} {...rest}>
<li
key={option.appid}
{...rest}
className={`profile-game-option ${rest.className ?? ''}`}
>
{option.name}
</li>
)
Expand All @@ -234,9 +239,11 @@ export default function ProfileCard({ profile, gameOptions, onSave }: ProfileCar
value={setup}
onChange={(e) => setSetup(e.target.value)}
>
<MenuItem value="">None</MenuItem>
<MenuItem className="profile-setup-option" value="">
None
</MenuItem>
{SETUP_OPTIONS.map((option) => (
<MenuItem key={option} value={option}>
<MenuItem className="profile-setup-option" key={option} value={option}>
{option}
</MenuItem>
))}
Expand Down
16 changes: 14 additions & 2 deletions Frontend/src/pages/ProfilePage/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
138 changes: 138 additions & 0 deletions Frontend/src/tests/ProfilePage.test.tsx
Original file line number Diff line number Diff line change
@@ -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<ProfileUpdate, 'favouriteGame'> & {
favouriteGame: GameOption | null
}

const { FetchGameOptions, FetchProfileFrontend, UpdateProfileFrontend, UseAuth } = vi.hoisted(
() => ({
FetchGameOptions:
vi.fn<(options?: { appid?: number; limit?: number }) => Promise<GameOption[]>>(),
FetchProfileFrontend: vi.fn<(userId: number) => Promise<Profile | null>>(),
UpdateProfileFrontend: vi.fn<(userId: number, input: ProfileUpdate) => Promise<Profile>>(),
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 }) => (
<div>
<h1>{title}</h1>
{actions}
</div>
)
}))
vi.mock('../components/ProfileCard', () => ({
default: ({ favouriteGame }: { favouriteGame: GameOption | null }) => (
<p>{favouriteGame?.name ?? 'No favourite game provided'}</p>
)
}))
vi.mock('../components/ProfileEditor/ProfileEditor', () => ({
default: ({ onSave }: { onSave: (input: ProfileSave) => Promise<void> }) => (
<>
<button
onClick={() =>
void onSave({
description: 'Updated profile',
setup: 'PC',
favouriteGame: { appid: 730, name: 'Counter-Strike 2' }
})
}
>
Save selected game
</button>
<button
onClick={() =>
void onSave({
description: 'Updated profile',
setup: 'PC',
favouriteGame: null
})
}
>
Clear favorite game
</button>
</>
)
}))
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(<ProfilePage />)

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(<ProfilePage />)

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()
})
})
1 change: 1 addition & 0 deletions tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down