diff --git a/client/src/app/app.config.ts b/client/src/app/app.config.ts index 2261369..874278d 100644 --- a/client/src/app/app.config.ts +++ b/client/src/app/app.config.ts @@ -1,7 +1,14 @@ import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core'; import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; +import { provideApi } from '../generated'; +import { provideHttpClient } from '@angular/common/http'; export const appConfig: ApplicationConfig = { - providers: [provideBrowserGlobalErrorListeners(), provideRouter(routes)], + providers: [ + provideBrowserGlobalErrorListeners(), + provideRouter(routes), + provideApi(''), + provideHttpClient(), + ], }; diff --git a/client/src/app/app.html b/client/src/app/app.html index 98a69ad..67e7bd4 100644 --- a/client/src/app/app.html +++ b/client/src/app/app.html @@ -1,8 +1 @@ -
-

GameRoom

-

- The new GameRoom client is being built here. Until it is ready, play at - the GameRoom. -

-
diff --git a/client/src/app/app.routes.ts b/client/src/app/app.routes.ts index dc39edb..f618bf3 100644 --- a/client/src/app/app.routes.ts +++ b/client/src/app/app.routes.ts @@ -1,3 +1,4 @@ import { Routes } from '@angular/router'; +import { Lobby } from './lobby/lobby'; -export const routes: Routes = []; +export const routes: Routes = [{ path: '', component: Lobby }]; diff --git a/client/src/app/app.scss b/client/src/app/app.scss deleted file mode 100644 index 476b0be..0000000 --- a/client/src/app/app.scss +++ /dev/null @@ -1,9 +0,0 @@ -.placeholder { - max-width: 40rem; - margin: 0 auto; - padding: 3rem 1rem; -} - -a { - color: #c4b5fd; -} diff --git a/client/src/app/app.spec.ts b/client/src/app/app.spec.ts index 0c13610..b3a2a32 100644 --- a/client/src/app/app.spec.ts +++ b/client/src/app/app.spec.ts @@ -6,12 +6,11 @@ describe('App', () => { await TestBed.configureTestingModule({ imports: [App] }).compileComponents(); }); - it('names the GameRoom and links to the current client', async () => { + it('checks router-outlet is present', async () => { const fixture = TestBed.createComponent(App); await fixture.whenStable(); const page = fixture.nativeElement as HTMLElement; - expect(page.querySelector('h1')?.textContent).toBe('GameRoom'); - expect(page.querySelector('a')?.getAttribute('href')).toBe('/'); + expect(page.querySelector('router-outlet')).not.toBeNull(); }); }); diff --git a/client/src/app/app.ts b/client/src/app/app.ts index e696cd8..2d3b026 100644 --- a/client/src/app/app.ts +++ b/client/src/app/app.ts @@ -5,7 +5,6 @@ import { RouterOutlet } from '@angular/router'; @Component({ imports: [RouterOutlet], selector: 'app-root', - styleUrl: './app.scss', templateUrl: './app.html', }) export class App {} diff --git a/client/src/app/lobby/lobby-rooms.service.spec.ts b/client/src/app/lobby/lobby-rooms.service.spec.ts new file mode 100644 index 0000000..87a5022 --- /dev/null +++ b/client/src/app/lobby/lobby-rooms.service.spec.ts @@ -0,0 +1,43 @@ +import { provideHttpClient } from '@angular/common/http'; +import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; +import { RoomSummary } from '../../generated/model/models'; +import { provideApi } from '../../generated/provide-api'; +import { LobbyRoomsService } from './lobby-rooms.service'; + +describe('LobbyRoomsService', () => { + let service: LobbyRoomsService; + let http: HttpTestingController; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [provideHttpClient(), provideHttpClientTesting(), provideApi('')], + }); + service = TestBed.inject(LobbyRoomsService); + http = TestBed.inject(HttpTestingController); + }); + + // Fails the test if a request was made that no test expected. + afterEach(() => http.verify()); + + it('fetches the lobby rooms from GET /rooms', () => { + const rooms: RoomSummary[] = [ + { + id: 'a', + name: 'Kitchen', + status: 'WAITING', + playerCount: 1, + maxPlayers: 4, + hasPassword: false, + }, + ]; + let received: RoomSummary[] | undefined; + + service.rooms().subscribe((value) => (received = value)); + const request = http.expectOne('/rooms'); + request.flush(rooms); + + expect(request.request.method).toBe('GET'); + expect(received).toEqual(rooms); + }); +}); diff --git a/client/src/app/lobby/lobby-rooms.service.ts b/client/src/app/lobby/lobby-rooms.service.ts new file mode 100644 index 0000000..9b8c586 --- /dev/null +++ b/client/src/app/lobby/lobby-rooms.service.ts @@ -0,0 +1,13 @@ +import { RoomsService } from '../../generated/api/api'; +import { RoomSummary } from '../../generated/model/models'; +import { Observable } from 'rxjs'; +import { inject, Service } from '@angular/core'; + +@Service() +export class LobbyRoomsService { + private readonly api = inject(RoomsService); + + rooms(): Observable { + return this.api.getRooms(); + } +} diff --git a/client/src/app/lobby/lobby.html b/client/src/app/lobby/lobby.html new file mode 100644 index 0000000..84893c4 --- /dev/null +++ b/client/src/app/lobby/lobby.html @@ -0,0 +1,10 @@ +
+

Game Lobby

+
    + @for (room of rooms(); track room.id) { +
  • {{ room.name }}
  • + } @empty { +
  • No rooms yet
  • + } +
+
diff --git a/client/src/app/lobby/lobby.spec.ts b/client/src/app/lobby/lobby.spec.ts new file mode 100644 index 0000000..187159e --- /dev/null +++ b/client/src/app/lobby/lobby.spec.ts @@ -0,0 +1,51 @@ +import { TestBed } from '@angular/core/testing'; +import { of } from 'rxjs'; +import { RoomSummary } from '../../generated/model/models'; +import { Lobby } from './lobby'; +import { LobbyRoomsService } from './lobby-rooms.service'; + +/** A room with valid defaults; a test only spells out what it's about. */ +function room(overrides: Partial = {}): RoomSummary { + return { + id: 'r1', + name: 'Room 1', + status: 'WAITING', + playerCount: 1, + maxPlayers: 4, + hasPassword: false, + ...overrides, + }; +} + +/** Renders the lobby with a fake service that answers with these rooms. */ +async function renderLobby(rooms: RoomSummary[]): Promise { + const fakeRooms: Pick = { + rooms: () => of(rooms), + }; + await TestBed.configureTestingModule({ + imports: [Lobby], + providers: [{ provide: LobbyRoomsService, useValue: fakeRooms }], + }).compileComponents(); + + const fixture = TestBed.createComponent(Lobby); + await fixture.whenStable(); + return fixture.nativeElement as HTMLElement; +} + +describe('Lobby', () => { + it('lists the room names', async () => { + const page = await renderLobby([ + room({ id: 'a', name: 'Kitchen' }), + room({ id: 'b', name: 'Attic' }), + ]); + + const names = [...page.querySelectorAll('li')].map((li) => li.textContent?.trim()); + expect(names).toEqual(['Kitchen', 'Attic']); + }); + + it('says so when there are no rooms', async () => { + const page = await renderLobby([]); + + expect(page.textContent).toContain('No rooms yet'); + }); +}); diff --git a/client/src/app/lobby/lobby.ts b/client/src/app/lobby/lobby.ts new file mode 100644 index 0000000..b06e181 --- /dev/null +++ b/client/src/app/lobby/lobby.ts @@ -0,0 +1,13 @@ +import { Component, inject } from '@angular/core'; +import { toSignal } from '@angular/core/rxjs-interop'; +import { LobbyRoomsService } from './lobby-rooms.service'; + +@Component({ + imports: [], + selector: 'app-lobby', + templateUrl: './lobby.html', +}) +export class Lobby { + private readonly roomsService = inject(LobbyRoomsService); + readonly rooms = toSignal(this.roomsService.rooms(), { initialValue: [] }); +}