From ce3a96dc8b540d870773e0bcb2f6204f2767ba68 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 10 Oct 2025 16:05:22 +0200 Subject: [PATCH] feat: Extracted configurable properties --- src/app/app.component.spec.ts | 4 ++++ src/app/app.config.ts | 5 ++++- src/app/app.tokens.ts | 8 ++++++++ src/app/chatroom/chatroom.component.spec.ts | 4 ++++ src/app/chatroom/chatroom.service.spec.ts | 4 ++++ src/app/chatroom/chatroom.service.ts | 11 ++++++++--- src/app/chatrooms/chatrooms.component.spec.ts | 4 ++++ src/app/user/user.component.spec.ts | 4 ++++ 8 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 src/app/app.tokens.ts diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index de0b395e..6f89fcbc 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -1,6 +1,7 @@ import { TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { AppComponent } from './app.component'; +import { APP_PROPS } from './app.tokens'; describe('AppComponent', () => { beforeEach(async () => { @@ -9,6 +10,9 @@ describe('AppComponent', () => { RouterTestingModule, AppComponent ], + providers: [ + { provide: APP_PROPS, useValue: {} }, + ], }).compileComponents(); }); diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 0de483b1..b1cfe97c 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -9,6 +9,7 @@ import { ReactiveFormsModule } from '@angular/forms'; import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; +import { APP_PROPS } from './app.tokens'; export const appConfig: ApplicationConfig = { providers: [ @@ -20,6 +21,8 @@ export const appConfig: ApplicationConfig = { provideHttpClient( withInterceptorsFromDi(), ), - provideRouter(routes) + provideRouter(routes), + { provide: APP_PROPS, useValue: { backendUri: 'http://localhost:8080/' } }, ] }; + diff --git a/src/app/app.tokens.ts b/src/app/app.tokens.ts new file mode 100644 index 00000000..bc1ce99e --- /dev/null +++ b/src/app/app.tokens.ts @@ -0,0 +1,8 @@ +import { InjectionToken } from '@angular/core'; + + +export interface ApplicationProperties { + backendUri: string; +} + +export const APP_PROPS = new InjectionToken('configurable application properties'); diff --git a/src/app/chatroom/chatroom.component.spec.ts b/src/app/chatroom/chatroom.component.spec.ts index e00563de..e6911d9b 100644 --- a/src/app/chatroom/chatroom.component.spec.ts +++ b/src/app/chatroom/chatroom.component.spec.ts @@ -2,6 +2,7 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { ChatroomComponent } from './index'; +import { APP_PROPS } from '../app.tokens'; describe('ChatroomComponent', () => { let component: ChatroomComponent; @@ -14,6 +15,9 @@ describe('ChatroomComponent', () => { HttpClientTestingModule, RouterTestingModule, ], + providers: [ + { provide: APP_PROPS, useValue: {} }, + ], }) .compileComponents(); diff --git a/src/app/chatroom/chatroom.service.spec.ts b/src/app/chatroom/chatroom.service.spec.ts index 493ab0c0..20868eb0 100644 --- a/src/app/chatroom/chatroom.service.spec.ts +++ b/src/app/chatroom/chatroom.service.spec.ts @@ -2,6 +2,7 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { ChatroomService } from './index'; +import { APP_PROPS } from '../app.tokens'; describe('ChatroomService', () => { let service: ChatroomService; @@ -12,6 +13,9 @@ describe('ChatroomService', () => { HttpClientTestingModule, RouterTestingModule, ], + providers: [ + { provide: APP_PROPS, useValue: {} }, + ], }); service = TestBed.inject(ChatroomService); }); diff --git a/src/app/chatroom/chatroom.service.ts b/src/app/chatroom/chatroom.service.ts index ac81cddc..a526e86c 100644 --- a/src/app/chatroom/chatroom.service.ts +++ b/src/app/chatroom/chatroom.service.ts @@ -1,8 +1,9 @@ -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { EventSourceMessage, fetchEventSource } from '@microsoft/fetch-event-source'; import { Observable, Subscriber } from 'rxjs'; import { Chatroom, Message } from './index'; +import { APP_PROPS } from '../app.tokens'; class RetriableError extends Error { } class CanceledError extends Error { } @@ -13,13 +14,17 @@ class FatalError extends Error { } }) export class ChatroomService { - private backendUri = 'http://localhost:8080/'; + private http = inject(HttpClient); + private backendUri: string; private channel: Subscriber = new Subscriber(); private uri: string = "CLOSED"; private canceled: boolean = false; - constructor(private http: HttpClient) { } + constructor() { + const props = inject(APP_PROPS); + this.backendUri = props.backendUri; + } getChatrooms(): Observable { return this.http.get(this.backendUri + 'list'); diff --git a/src/app/chatrooms/chatrooms.component.spec.ts b/src/app/chatrooms/chatrooms.component.spec.ts index a835b15d..d8712104 100644 --- a/src/app/chatrooms/chatrooms.component.spec.ts +++ b/src/app/chatrooms/chatrooms.component.spec.ts @@ -1,6 +1,7 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ChatroomsComponent } from './index'; +import { APP_PROPS } from '../app.tokens'; describe('ChatroomsComponent', () => { let component: ChatroomsComponent; @@ -12,6 +13,9 @@ describe('ChatroomsComponent', () => { HttpClientTestingModule, ChatroomsComponent, ], + providers: [ + { provide: APP_PROPS, useValue: {} }, + ], }) .compileComponents(); diff --git a/src/app/user/user.component.spec.ts b/src/app/user/user.component.spec.ts index 09f0994c..b0a097d2 100644 --- a/src/app/user/user.component.spec.ts +++ b/src/app/user/user.component.spec.ts @@ -1,6 +1,7 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ReactiveFormsModule } from '@angular/forms'; import { UserComponent } from './index'; +import { APP_PROPS } from '../app.tokens'; describe('UserComponent', () => { let component: UserComponent; @@ -12,6 +13,9 @@ describe('UserComponent', () => { ReactiveFormsModule, UserComponent, ], + providers: [ + { provide: APP_PROPS, useValue: {} }, + ], }) .compileComponents(); -- 2.39.5