Untitled
import { TestBed, ComponentFixture } from '@angular/core/testing'; import { of, BehaviorSubject } from 'rxjs'; import { VgsComponent } from './vgs.component'; // Mock or partial stubs of the services import { AuthenticationService } from '@portal/sdk'; import { VpsApiEndpointService, VpsModelLapTime, } from '@tosis/dal-api-client'; import { ComputationDataService, SetupDataService, ComputationApiResponseModel, DAL_API_VERSION, } from '@tosis/domain'; import { ActivatedRoute, Router } from '@angular/router'; import { Location } from '@angular/common'; import { MessageService } from 'primeng/api'; describe('VgsComponent (Mocked Services)', () => { let component: VgsComponent; let fixture: ComponentFixture<VgsComponent>; // We'll define spies/mocks here so we can control their return values let authServiceMock: Partial<AuthenticationService>; let vpsApiEndpointServiceMock: Partial<VpsApiEndpointService>; let setupDataServiceMock: Partial<SetupDataService>; let computationDataServiceMock: Partial<ComputationDataService>; let activatedRouteMock: Partial<ActivatedRoute>; let routerMock: Partial<Router>; let locationMock: Partial<Location>; let messageServiceMock: Partial<MessageService>; beforeEach(async () => { // // 1) Setup the basic mocks // authServiceMock = { // Typically, your component uses isAuthenticated$ in combineLatest. isAuthenticated$: new BehaviorSubject<boolean>(true).asObservable(), }; vpsApiEndpointServiceMock = { // We'll define what getVpsLaptime returns later in each test, // or in a shared mock return for the entire suite. getVpsLaptime: jest.fn(), }; setupDataServiceMock = { // We'll mock getWithQuery to return a list of setups getWithQuery: jest.fn(), }; computationDataServiceMock = { // We'll mock getVGSResults to return computed data getVGSResults: jest.fn(), }; // We'll simulate some queryParams that the component expects activatedRouteMock = { queryParams: of({ setups: '1,2', versionCar: 'Car_v1', 'constant_speed': '1', 'limit_acceleration': '1', 'limit_braking': '1', 'limit_cornering': '1', 'corner_distances': '1', 'vmin': '0', 'vmax': '200', 'stepv': '10', // Example plot params 'xAxis-0': 'time', 'yAxis-0': 'speed,acceleration', }), }; routerMock = { parseUrl: jest.fn().mockReturnValue({ queryParams: {} }), url: '/test', createUrlTree: jest.fn(), navigateByUrl: jest.fn(), navigate: jest.fn(), }; locationMock = { replaceState: jest.fn(), path: jest.fn().mockReturnValue('/test'), }; messageServiceMock = { add: jest.fn(), }; // // 2) Configure the test module with the component and the mocks // await TestBed.configureTestingModule({ declarations: [VgsComponent], providers: [ { provide: AuthenticationService, useValue: authServiceMock }, { provide: VpsApiEndpointService, useValue: vpsApiEndpointServiceMock }, { provide: SetupDataService, useValue: setupDataServiceMock }, { provide: ComputationDataService, useValue: computationDataServiceMock }, { provide: ActivatedRoute, useValue: activatedRouteMock }, { provide: Router, useValue: routerMock }, { provide: Location, useValue: locationMock }, { provide: MessageService, useValue: messageServiceMock }, { provide: DAL_API_VERSION, useValue: 'v1' }, ], }).compileComponents(); }); beforeEach(() => { // 3) Create the component fixture fixture = TestBed.createComponent(VgsComponent); component = fixture.componentInstance; }); it('should load data, run computations, and populate `this.results`', () => { // // Arrange // // Mock data for getVpsLaptime calls const fakeLapTimeData1: VpsModelLapTime = { vehicleParamStruct: { name: 'VehicleParamA' } as any, externalCondition: { track: 'TrackA' } as any, }; const fakeLapTimeData2: VpsModelLapTime = { vehicleParamStruct: { name: 'VehicleParamB' } as any, externalCondition: { track: 'TrackB' } as any, }; // We expect getVpsLaptime to be called for each setup ID (1,2) (vpsApiEndpointServiceMock.getVpsLaptime as jest.Mock) .mockReturnValueOnce(of(fakeLapTimeData1)) .mockReturnValueOnce(of(fakeLapTimeData2)); // Mock data for getWithQuery // The service is called to retrieve "Setup" information (like name). (setupDataServiceMock.getWithQuery as jest.Mock).mockReturnValueOnce( of([ { id: 1, nameForUser: 'Setup #1 from DB' }, { id: 2, nameForUser: 'Setup #2 from DB' }, ]) ); // Mock data for getVGSResults (the computation step) const fakeComputationResult1: ComputationApiResponseModel = { results: { VgsResultA: { data: { State: { speed: [10, 20, 30], acceleration: [1.0, 1.2, 1.4], }, }, }, }, }; const fakeComputationResult2: ComputationApiResponseModel = { results: { VgsResultB: { data: { State: { speed: [40, 50, 60], acceleration: [0.8, 1.1, 1.3], }, }, }, }, }; (computationDataServiceMock.getVGSResults as jest.Mock) .mockReturnValueOnce(of(fakeComputationResult1)) .mockReturnValueOnce(of(fakeComputationResult2)); // // Act // // Trigger the component's ngOnInit by detecting changes fixture.detectChanges(); // // Assert // // The component's `results` should eventually be populated expect(component.results.length).toBe(2); // The first element of results expect(component.results[0]).toEqual({ id: 1, name: 'Setup #1 from DB', dataset: [ { title: 'VgsResultA', data: [ { id: 'State.speed', name: 'State.speed', value: [10, 20, 30], }, { id: 'State.acceleration', name: 'State.acceleration', value: [1.0, 1.2, 1.4], }, ], }, ], }); // The second element of results expect(component.results[1]).toEqual({ id: 2, name: 'Setup #2 from DB', dataset: [ { title: 'VgsResultB', data: [ { id: 'State.speed', name: 'State.speed', value: [40, 50, 60], }, { id: 'State.acceleration', name: 'State.acceleration', value: [0.8, 1.1, 1.3], }, ], }, ], }); // Also verify that the mocks were called the correct number of times expect(vpsApiEndpointServiceMock.getVpsLaptime).toHaveBeenCalledTimes(2); expect(setupDataServiceMock.getWithQuery).toHaveBeenCalledTimes(1); expect(computationDataServiceMock.getVGSResults).toHaveBeenCalledTimes(2); }); });
Leave a Comment