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
9 changes: 8 additions & 1 deletion client/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -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(),
],
};
7 changes: 0 additions & 7 deletions client/src/app/app.html
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
<main class="placeholder">
<h1>GameRoom</h1>
<p>
The new GameRoom client is being built here. Until it is ready, play at
<a href="/">the GameRoom</a>.
</p>
</main>
<router-outlet />
3 changes: 2 additions & 1 deletion client/src/app/app.routes.ts
Original file line number Diff line number Diff line change
@@ -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 }];
9 changes: 0 additions & 9 deletions client/src/app/app.scss

This file was deleted.

5 changes: 2 additions & 3 deletions client/src/app/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
1 change: 0 additions & 1 deletion client/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { RouterOutlet } from '@angular/router';
@Component({
imports: [RouterOutlet],
selector: 'app-root',
styleUrl: './app.scss',
templateUrl: './app.html',
})
export class App {}
43 changes: 43 additions & 0 deletions client/src/app/lobby/lobby-rooms.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
13 changes: 13 additions & 0 deletions client/src/app/lobby/lobby-rooms.service.ts
Original file line number Diff line number Diff line change
@@ -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<RoomSummary[]> {
return this.api.getRooms();
}
}
10 changes: 10 additions & 0 deletions client/src/app/lobby/lobby.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<main class="lobby">
<h1>Game Lobby</h1>
<ul class="room-list">
@for (room of rooms(); track room.id) {
<li>{{ room.name }}</li>
} @empty {
<li>No rooms yet</li>
}
</ul>
</main>
51 changes: 51 additions & 0 deletions client/src/app/lobby/lobby.spec.ts
Original file line number Diff line number Diff line change
@@ -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> = {}): 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<HTMLElement> {
const fakeRooms: Pick<LobbyRoomsService, 'rooms'> = {
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');
});
});
13 changes: 13 additions & 0 deletions client/src/app/lobby/lobby.ts
Original file line number Diff line number Diff line change
@@ -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: [] });
}
Loading