]> juplo.de Git - demos/kafka/chat/commitdiff
feat: Extracted configurable properties
authorKai Moritz <kai@juplo.de>
Fri, 10 Oct 2025 14:05:22 +0000 (16:05 +0200)
committerKai Moritz <kai@juplo.de>
Fri, 10 Oct 2025 20:16:13 +0000 (22:16 +0200)
src/app/app.component.spec.ts
src/app/app.config.ts
src/app/app.tokens.ts [new file with mode: 0644]
src/app/chatroom/chatroom.component.spec.ts
src/app/chatroom/chatroom.service.spec.ts
src/app/chatroom/chatroom.service.ts
src/app/chatrooms/chatrooms.component.spec.ts
src/app/user/user.component.spec.ts

index de0b395e3e93bd00ada040c0afc8f2b0b1be5a1e..6f89fcbc8b97ca4f7b573eb7bc138fa84e71f9f4 100644 (file)
@@ -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();
   });
 
index 0de483b145d39763460a4c7de4d88093f500632a..b1cfe97ce59bbd25a8d96136124e995428b3d31b 100644 (file)
@@ -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 (file)
index 0000000..bc1ce99
--- /dev/null
@@ -0,0 +1,8 @@
+import { InjectionToken } from '@angular/core';
+
+
+export interface ApplicationProperties {
+  backendUri: string;
+}
+
+export const APP_PROPS = new InjectionToken<ApplicationProperties>('configurable application properties');
index e00563dee6ec0f27601ef0da38c480c2ee06ee8d..e6911d9b537db1c81193607f5c7a455ccce51431 100644 (file)
@@ -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();
 
index 493ab0c0461fc2ca4c10ec21287e1d9ff632ecff..20868eb0806e8e4e73054091327f11e7874d1b1f 100644 (file)
@@ -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);
   });
index ac81cddc788f4410ebd9b18f445a21256342e2d4..a526e86cce8fe4284417bf1bce77051b28d22134 100644 (file)
@@ -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<Message> = new Subscriber<Message>();
   private uri: string = "CLOSED";
   private canceled: boolean = false;
 
-  constructor(private http: HttpClient) { }
+  constructor() {
+    const props = inject(APP_PROPS);
+    this.backendUri = props.backendUri;
+  }
 
   getChatrooms(): Observable<Chatroom[]> {
     return this.http.get<Chatroom[]>(this.backendUri + 'list');
index a835b15d00f77e001e4da75410a5402e29765f19..d8712104589cd190858c408559be0fea3d088d7b 100644 (file)
@@ -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();
 
index 09f0994c6a417962f43176871cd5251417c76db0..b0a097d235dca7ddc04ac2ea416db802fcb15bbf 100644 (file)
@@ -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();