Skip to content

Commit

Permalink
Addes tests for data-stores.component.ts
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Oberndörfer <florian8751t@gmail.com>
  • Loading branch information
flo0852 committed Dec 20, 2024
1 parent f43a80e commit af24837
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 54 deletions.
112 changes: 67 additions & 45 deletions apps/frontend/src/app/alert/component/alert.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { AlertComponent } from './alert.component';
import { AlertServiceService } from '../service/alert-service.service';
import { DatePipe } from '@angular/common';
import { of } from 'rxjs';
import { Alert, SizeAlert } from '../../shared/types/alert';
import { randomUUID } from 'crypto';
Expand All @@ -11,8 +9,6 @@ describe('AlertComponent', () => {
let component: AlertComponent;
let mockAlertService: {
getAllAlerts: Mock;
//getAllAlerts: vi.Mock;
//getAllAlerts: vi.Mock;
};
let mockDatePipe: {
transform: Mock;
Expand All @@ -39,40 +35,49 @@ describe('AlertComponent', () => {
{
id: randomUUID().toString(),
alertType: {
id: randomUUID().toString(), name: 'test',
id: randomUUID().toString(),
name: 'test',
severity: SeverityType.CRITICAL,
user_active: false,
master_active: false
master_active: false,
},
backup: {
id: randomUUID().toString(), sizeMB: 0, creationDate: new Date(),
saveset: 'saveset'
id: randomUUID().toString(),
sizeMB: 0,
creationDate: new Date(),
saveset: 'saveset',
},
},
{
id: randomUUID().toString(),
alertType: {
id: randomUUID().toString(), name: 'test',
id: randomUUID().toString(),
name: 'test',
severity: SeverityType.INFO,
user_active: false,
master_active: false
master_active: false,
},
backup: {
id: randomUUID().toString(), sizeMB: 0, creationDate: new Date(),
saveset: 'saveset'
id: randomUUID().toString(),
sizeMB: 0,
creationDate: new Date(),
saveset: 'saveset',
},
},
{
id: randomUUID().toString(),
alertType: {
id: randomUUID().toString(), name: 'test',
id: randomUUID().toString(),
name: 'test',
severity: SeverityType.WARNING,
user_active: false,
master_active: false
master_active: false,
},
backup: {
id: randomUUID().toString(), sizeMB: 0, creationDate: new Date(),
saveset: 'saveset'
id: randomUUID().toString(),
sizeMB: 0,
creationDate: new Date(),
saveset: 'saveset',
},
},
];
Expand Down Expand Up @@ -108,14 +113,17 @@ describe('AlertComponent', () => {
{
id: randomUUID().toString(),
alertType: {
id: randomUUID().toString(), name: 'test',
id: randomUUID().toString(),
name: 'test',
severity: SeverityType.CRITICAL,
user_active: false,
master_active: false
master_active: false,
},
backup: {
id: randomUUID().toString(), sizeMB: 0, creationDate: new Date(),
saveset: 'saveset'
id: randomUUID().toString(),
sizeMB: 0,
creationDate: new Date(),
saveset: 'saveset',
},
},
];
Expand All @@ -127,14 +135,17 @@ describe('AlertComponent', () => {
{
id: randomUUID().toString(),
alertType: {
id: randomUUID().toString(), name: 'test',
id: randomUUID().toString(),
name: 'test',
severity: SeverityType.WARNING,
user_active: false,
master_active: false
master_active: false,
},
backup: {
id: randomUUID().toString(), sizeMB: 0, creationDate: new Date(),
saveset: 'saveset'
id: randomUUID().toString(),
sizeMB: 0,
creationDate: new Date(),
saveset: 'saveset',
},
},
];
Expand All @@ -157,32 +168,37 @@ describe('AlertComponent', () => {

describe('getAlertClass', () => {
it('should return correct alert class based on alert type', () => {

const criticalAlert: Alert = {
id: randomUUID().toString(),
alertType: {
id: randomUUID().toString(), name: 'test',
id: randomUUID().toString(),
name: 'test',
severity: SeverityType.CRITICAL,
user_active: false,
master_active: false
master_active: false,
},
backup: {
id: randomUUID().toString(), sizeMB: 0, creationDate: new Date(),
saveset: 'saveset'
id: randomUUID().toString(),
sizeMB: 0,
creationDate: new Date(),
saveset: 'saveset',
},
};

const nonCriticalAlert: Alert = {
const nonCriticalAlert: Alert = {
id: randomUUID().toString(),
alertType: {
id: randomUUID().toString(), name: 'test',
id: randomUUID().toString(),
name: 'test',
severity: SeverityType.WARNING,
user_active: false,
master_active: false
master_active: false,
},
backup: {
id: randomUUID().toString(), sizeMB: 0, creationDate: new Date(),
saveset: 'saveset'
id: randomUUID().toString(),
sizeMB: 0,
creationDate: new Date(),
saveset: 'saveset',
},
};

Expand All @@ -196,17 +212,20 @@ describe('AlertComponent', () => {
const alert: SizeAlert = {
id: randomUUID().toString(),
alertType: {
id: randomUUID().toString(), name: 'SIZE_ALERT',
id: randomUUID().toString(),
name: 'SIZE_ALERT',
severity: SeverityType.WARNING,
user_active: false,
master_active: false
master_active: false,
},
backup: {
id: randomUUID().toString(), sizeMB: 20, creationDate: new Date(),
saveset: 'saveset'
id: randomUUID().toString(),
sizeMB: 20,
creationDate: new Date(),
saveset: 'saveset',
},
size: 20,
referenceSize: 100
referenceSize: 100,
};

expect(component.getAlertReason(alert)).toBe('Size of backup decreased');
Expand All @@ -216,17 +235,20 @@ describe('AlertComponent', () => {
const alert: SizeAlert = {
id: randomUUID().toString(),
alertType: {
id: randomUUID().toString(), name: 'SIZE_ALERT',
id: randomUUID().toString(),
name: 'SIZE_ALERT',
severity: SeverityType.WARNING,
user_active: false,
master_active: false
master_active: false,
},
backup: {
id: randomUUID().toString(), sizeMB: 100, creationDate: new Date(),
saveset: 'saveset'
id: randomUUID().toString(),
sizeMB: 100,
creationDate: new Date(),
saveset: 'saveset',
},
size: 100,
referenceSize: 20
referenceSize: 20,
};

expect(component.getAlertReason(alert)).toBe('Size of backup increased');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { of } from 'rxjs';
import { randomUUID } from 'crypto';
import { DataStoresComponent } from './data-stores.component';
import { DataStore } from '../../shared/types/data-store';

describe('DataStoresComponent', () => {
let component: DataStoresComponent;
let mockDataStoresService: {
getAllDataStores: Mock;
};

beforeEach(() => {
mockDataStoresService = {
getAllDataStores: vi.fn(),
};

component = new DataStoresComponent(mockDataStoresService as any);
});

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,
},
];

mockDataStoresService.getAllDataStores.mockReturnValue(of(dataStores));

component.loadDataStores();

expect(component.dataStores).toEqual(dataStores);
expect(component.showAll).toBeFalsy();
});
});

describe('getFilledPercentage', () => {
it('should return the filled percentage correctly', () => {
const dataStore: DataStore = {
id: randomUUID().toString(),
displayName: 'test',
capacity: 100,
filled: 50,
highWaterMark: 80,
};

expect(component.getFilledPercentage(dataStore)).toBe(50);
});
});

describe('getHighWaterMarkPercentage', () => {
it('should return the high water mark percentage correctly', () => {
const dataStore: DataStore = {
id: randomUUID().toString(),
displayName: 'test',
capacity: 100,
filled: 50,
highWaterMark: 80,
};

expect(component.getHighWaterMarkPercentage(dataStore)).toBe(80);
});
});

describe('toggleShowAll', () => {
it('should toggle showAll correctly', () => {
component.showAll = false;

component.toggleShowAll();

expect(component.showAll).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ export class DataStoresComponent implements OnInit, OnDestroy {
constructor(private readonly dataStoresService: DataStoresService) {}

ngOnInit(): void {
this.dataStoresService
.getAllDataStores()
.pipe(takeUntil(this.destroy$))
.subscribe((dataStores) => {
this.dataStores = dataStores.sort(
(a, b) => this.getFilledPercentage(b) - this.getFilledPercentage(a)
);
});
this.loadDataStores();
}

ngOnDestroy(): void {
Expand All @@ -46,4 +39,15 @@ export class DataStoresComponent implements OnInit, OnDestroy {
toggleShowAll(): void {
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)
);
});
}
}

0 comments on commit af24837

Please sign in to comment.