fix: make remote-data-service return promises, fix side-panel tests

This commit is contained in:
2025-02-13 10:37:15 +01:00
parent 6fc1e0ec58
commit 036e6151a0
5 changed files with 39 additions and 22 deletions

View File

@ -1,7 +1,7 @@
export interface Session {
name: string;
deployment_id: string;
deployment_id?: string;
_id: string;
owner_groups: string[];
access_groups: []; // This should probably be string[] as well
owner_groups?: string[];
access_groups?: []; // This should probably be string[] as well
}

View File

@ -1,5 +1,6 @@
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { firstValueFrom } from 'rxjs';
import { Session } from './model/session';
import { ServerSettingsService } from '../server-settings.service';
import { ScanDataResponse } from './model/scan-data';
@ -167,7 +168,7 @@ export class ScanDataService extends RemoteDataService {
) {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json; charset=utf-8');
return this.get<Array<ScanDataResponse>>(
return firstValueFrom(this.get<Array<ScanDataResponse>>(
'scans/session',
{
session_id: sessionId,
@ -178,7 +179,7 @@ export class ScanDataService extends RemoteDataService {
includeUserData: includeUserData.toString(),
},
headers
);
));
}
/**
* Method for getting the scan count
@ -207,7 +208,7 @@ export class ScanDataService extends RemoteDataService {
if (datasetNumber !== null) {
filters['dataset_number'] = datasetNumber;
}
return this.get<ScanCountResponse>('scans/count', filters, headers);
return firstValueFrom(this.get<ScanCountResponse>('scans/count', filters, headers));
}
/**
@ -222,12 +223,12 @@ export class ScanDataService extends RemoteDataService {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json; charset=utf-8');
console.log('Updating user data', userData);
return this.patch<string>(
return firstValueFrom(this.patch<string>(
'scans/user_data',
{ scan_id: scanId },
userData,
headers
);
));
}
}
@ -246,10 +247,10 @@ export class SessionDataService extends RemoteDataService {
getSessions(offset: number = 0, limit: number = 100) {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json; charset=utf-8');
return this.get<Session[]>(
return firstValueFrom(this.get<Session[]>(
'sessions',
{ offset: offset.toString(), limit: limit.toString() },
headers
);
));
}
}

View File

@ -24,7 +24,6 @@ import {
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { StarRatingModule } from 'angular-star-rating';
import { MatSort } from '@angular/material/sort';
import { firstValueFrom, Observable } from 'rxjs';
import { ScanCountResponse } from '../core/model/scan-count';
import { MatMenuModule } from '@angular/material/menu';
import { MatCheckboxModule } from '@angular/material/checkbox';
@ -155,19 +154,17 @@ export class ScanTableComponent {
? 'user_data'
: element
);
columns.push('scan_id'); // always include scan_id
columns.includes('scan_id') ? null : columns.push('scan_id'); // always include scan_id
let sessionId = request.session ? request.session._id : '';
console.log('Columns', columns);
return firstValueFrom(
this.scanData.getScanData(
return this.scanData.getScanData(
sessionId,
request.offset,
request.limit,
columns,
false,
{ scan_number: this.sorting }
)
);
);
},
});
@ -176,7 +173,7 @@ export class ScanTableComponent {
request: () => this.reloadCriteria(),
loader: ({ request, abortSignal }): Promise<ScanCountResponse> => {
let sessionId = request.session ? request.session._id : '';
return firstValueFrom(this.scanData.getScanCount(sessionId));
return this.scanData.getScanCount(sessionId);
},
});
@ -209,6 +206,10 @@ export class ScanTableComponent {
return data.count;
}
// -----------------------------------------
// -------------Constructor-----------------
// ----------------------------------------
constructor(private scanData: ScanDataService) {
this.tableData = computed(() =>
this.handleScanData(this.loadScanDataResource.value() || [])
@ -268,7 +269,7 @@ export class ScanTableComponent {
console.log('Scan ID', scanId);
if (scanId) {
console.log('Updating user data', userData);
await firstValueFrom(this.scanData.updateUserData(scanId, userData));
await this.scanData.updateUserData(scanId, userData);
}
}
}

View File

@ -5,18 +5,21 @@ import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { AppConfigService } from '../../app-config.service';
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
import { SessionDataService } from '../../core/remote-data.service';
describe('SidePanelComponent', () => {
let component: SidePanelComponent;
let fixture: ComponentFixture<SidePanelComponent>;
let sessionDataServiceMock: any;
sessionDataServiceMock = jasmine.createSpyObj('SessionDataService', ['getSessions']);
sessionDataServiceMock.getSessions.and.returnValue(Promise.resolve([{ _id: '1', name: 'test'}]));
beforeEach(async () => {
await TestBed.configureTestingModule({
providers: [
provideHttpClient(),
provideHttpClientTesting(),
provideAnimationsAsync(),
AppConfigService,
{provide: SessionDataService, useValue: sessionDataServiceMock},
],
imports: [SidePanelComponent],
}).compileComponents();
@ -29,4 +32,14 @@ describe('SidePanelComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
it('should select sessionId', async () => {
spyOn(component.sessionChanged, 'emit');
component.onSessionChange(null);
expect(component.selectedSession).toBeNull();
expect(component.sessionChanged.emit).toHaveBeenCalledWith(null);
component.onSessionChange({ _id: '1', name: 'test'});
expect(component.selectedSession).toEqual({ _id: '1', name: 'test'});
expect(component.sessionChanged.emit).toHaveBeenCalledWith({ _id: '1', name: 'test'});
});
});

View File

@ -22,8 +22,10 @@ export class SidePanelComponent {
constructor(private sessionDataService: SessionDataService) {}
ngOnInit(): void {
this.sessionDataService.getSessions().subscribe((sessions) => {
this.sessionDataService.getSessions().then((sessions) => {
this.sessions = sessions;
}).catch((error) => {
console.error('Failed to load sessions', error);
});
}