Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tu-01-26): stations api #51

Merged
merged 2 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/api/interceptors/http.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TestBed } from '@angular/core/testing';
import { LocalStorageService } from '../../core/services/local-storage/local-storage.service';
import { httpInterceptor } from './http.interceptor';

describe('httpInterceptorInterceptor', () => {
describe('httpInterceptor', () => {
const interceptor: HttpInterceptorFn = (req, next) => TestBed.runInInjectionContext(() => httpInterceptor(req, next));
let httpTestingController: HttpTestingController;
let httpClient: HttpClient;
Expand Down
19 changes: 19 additions & 0 deletions src/app/api/models/stations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
interface ConnectedStation {
id: number;
distance: number;
}

export interface Station {
id: number;
city: string;
latitude: number;
longitude: number;
connectedTo: ConnectedStation[];
}

export interface NewStation {
city: string;
latitude: number;
longitude: number;
relations: number[];
}
80 changes: 80 additions & 0 deletions src/app/api/stationsService/stations.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';

import { NewStation, Station } from '../models/stations';
import { StationsService } from './stations.service';

describe('StationsService', () => {
let service: StationsService;
let httpTestController: HttpTestingController;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [provideHttpClient(), provideHttpClientTesting()],
});
service = TestBed.inject(StationsService);
httpTestController = TestBed.inject(HttpTestingController);
});

afterEach(() => {
httpTestController.verify();
});
it('should be created', () => {
expect(service).toBeTruthy();
});

it('should correctly get all stations', (done) => {
const mockStationsArray: Station[] = [
{
id: 1,
city: 'Test',
latitude: 12345,
longitude: 54321,
connectedTo: [{ id: 1, distance: 2 }],
},
];

service.getStations().subscribe((res) => {
expect(res).toEqual(mockStationsArray);
done();
});

const req = httpTestController.expectOne('station');
expect(req.request.method).toBe('GET');
req.flush(mockStationsArray);
});

it('should correctly create new station', (done) => {
const mockNewStation: NewStation = {
city: 'testCity',
latitude: 1423,
longitude: 4132,
relations: [5, 3, 1],
};
const mockResponse = { id: 1 };

service.createNewStation(mockNewStation).subscribe((res) => {
expect(res).toEqual(mockResponse);
done();
});

const req = httpTestController.expectOne('station');
expect(req.request.method).toBe('POST');
expect(req.request.body).toEqual(mockNewStation);
req.flush(mockResponse);
});

it('should correctly delete station', (done) => {
const mockStationId = 1;

service.deleteStation(mockStationId).subscribe((res) => {
expect(res).toBeNull();
done();
});

const req = httpTestController.expectOne(`station/${mockStationId}`);
expect(req.request.method).toBe('DELETE');
req.flush(null);
});
});
26 changes: 26 additions & 0 deletions src/app/api/stationsService/stations.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';

import { Observable } from 'rxjs';

import { NewStation, Station } from '../models/stations';

@Injectable({
providedIn: 'root',
})
export class StationsService {
private httpClient = inject(HttpClient);
private STATION_ENDPOINT = 'station';

public getStations(): Observable<Station[]> {
return this.httpClient.get<Station[]>(this.STATION_ENDPOINT);
}

public createNewStation(newStation: NewStation): Observable<{ id: number }> {
return this.httpClient.post<{ id: number }>(this.STATION_ENDPOINT, newStation);
}

public deleteStation(id: number): Observable<void> {
return this.httpClient.delete<void>(`${this.STATION_ENDPOINT}/${id}`);
}
}
Loading