From cec90b4665c3cde4cd210d7f6aaeb425dd436974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Obernd=C3=B6rfer?= Date: Sat, 21 Dec 2024 16:33:05 +0100 Subject: [PATCH] PR changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Florian Oberndörfer --- .../backups/backups/backups.component.html | 4 +- .../component/data-stores.component.html | 9 ++-- .../component/data-stores.component.spec.ts | 49 +++++++++---------- .../component/data-stores.component.ts | 30 +++++------- 4 files changed, 44 insertions(+), 48 deletions(-) diff --git a/apps/frontend/src/app/backups-overview/backups/backups/backups.component.html b/apps/frontend/src/app/backups-overview/backups/backups/backups.component.html index 9685ee5..ed3609d 100644 --- a/apps/frontend/src/app/backups-overview/backups/backups/backups.component.html +++ b/apps/frontend/src/app/backups-overview/backups/backups/backups.component.html @@ -35,8 +35,8 @@

Backups Overview

-
- +
+
diff --git a/apps/frontend/src/app/data-stores/component/data-stores.component.html b/apps/frontend/src/app/data-stores/component/data-stores.component.html index dbaf97d..5719a41 100644 --- a/apps/frontend/src/app/data-stores/component/data-stores.component.html +++ b/apps/frontend/src/app/data-stores/component/data-stores.component.html @@ -3,7 +3,8 @@ Data Stores
-
+
{{ dataStore.displayName }}
-
\ No newline at end of file diff --git a/apps/frontend/src/app/data-stores/component/data-stores.component.spec.ts b/apps/frontend/src/app/data-stores/component/data-stores.component.spec.ts index 96a9186..4ace022 100644 --- a/apps/frontend/src/app/data-stores/component/data-stores.component.spec.ts +++ b/apps/frontend/src/app/data-stores/component/data-stores.component.spec.ts @@ -4,6 +4,23 @@ import { randomUUID } from 'crypto'; import { DataStoresComponent } from './data-stores.component'; import { DataStore } from '../../shared/types/data-store'; +const dataStores: DataStore[] = [ + { + id: randomUUID().toString(), + displayName: 'test', + capacity: 100, + filled: 50, + highWaterMark: 80, + }, + { + id: randomUUID().toString(), + displayName: 'test2', + capacity: 100, + filled: 80, + highWaterMark: 80, + }, +]; + describe('DataStoresComponent', () => { let component: DataStoresComponent; let mockDataStoresService: { @@ -12,37 +29,19 @@ describe('DataStoresComponent', () => { beforeEach(() => { mockDataStoresService = { - getAllDataStores: vi.fn(), + getAllDataStores: vi.fn().mockReturnValue(of(dataStores)), }; - component = new DataStoresComponent(mockDataStoresService as any); + component = new DataStoresComponent(mockDataStoresService); }); - describe('loadDataStores', () => { - it('should load data stores correctly', () => { - const dataStores: DataStore[] = [ - { - id: randomUUID().toString(), - displayName: 'test', - capacity: 100, - filled: 50, - highWaterMark: 80, - }, - { - id: randomUUID().toString(), - displayName: 'test2', - capacity: 100, - filled: 80, - highWaterMark: 80, - }, - ]; - + describe('dataStores$', () => { + it('should load data stores correctly', (done) => { mockDataStoresService.getAllDataStores.mockReturnValue(of(dataStores)); - component.loadDataStores(); - - expect(component.dataStores).toEqual(dataStores); - expect(component.showAll).toBeFalsy(); + component.dataStores$.subscribe((result) => { + expect(result).toEqual(dataStores); + }); }); }); diff --git a/apps/frontend/src/app/data-stores/component/data-stores.component.ts b/apps/frontend/src/app/data-stores/component/data-stores.component.ts index ebd605d..bc92900 100644 --- a/apps/frontend/src/app/data-stores/component/data-stores.component.ts +++ b/apps/frontend/src/app/data-stores/component/data-stores.component.ts @@ -1,6 +1,6 @@ -import { Component, OnDestroy, OnInit } from '@angular/core'; +import { Component, OnDestroy } from '@angular/core'; import { DataStoresService } from '../service/data-stores-service.service'; -import { Subject, takeUntil } from 'rxjs'; +import { map, Observable, shareReplay, Subject, takeUntil } from 'rxjs'; import { DataStore } from '../../shared/types/data-store'; @Component({ @@ -8,15 +8,18 @@ import { DataStore } from '../../shared/types/data-store'; templateUrl: './data-stores.component.html', styleUrl: './data-stores.component.css', }) -export class DataStoresComponent implements OnInit, OnDestroy { +export class DataStoresComponent implements OnDestroy { private readonly destroy$ = new Subject(); - dataStores: DataStore[] = []; - showAll = false; + dataStores$: Observable; - constructor(private readonly dataStoresService: DataStoresService) {} + showAll = false; - ngOnInit(): void { - this.loadDataStores(); + constructor(private readonly dataStoresService: DataStoresService) { + this.dataStores$ = this.dataStoresService + .getAllDataStores() + .pipe(takeUntil(this.destroy$), + map(dataStores => this.sortDataStores(dataStores)), + shareReplay(1)); } ngOnDestroy(): void { @@ -40,14 +43,7 @@ export class DataStoresComponent implements OnInit, OnDestroy { this.showAll = !this.showAll; } - loadDataStores() { - this.dataStoresService - .getAllDataStores() - .pipe(takeUntil(this.destroy$)) - .subscribe((dataStores) => { - this.dataStores = dataStores.sort( - (a, b) => this.getFilledPercentage(b) - this.getFilledPercentage(a) - ); - }); + private sortDataStores(dataStores: DataStore[]): DataStore[] { + return dataStores.sort((a, b) => this.getFilledPercentage(b) - this.getFilledPercentage(a)); } }