Skip to content

Commit

Permalink
test(tournament service): generating more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kdinev committed Nov 14, 2023
1 parent 802518d commit 4a6c336
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TestBed } from '@angular/core/testing';

import { ApiTournamentsService } from './bellumgens-api.tournaments.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TournamentCSGOMatch } from '../models/tournament-schedule';

describe('ApiTournamentsService', () => {
beforeEach(() => TestBed.configureTestingModule({
Expand All @@ -12,4 +13,107 @@ describe('ApiTournamentsService', () => {
const service: ApiTournamentsService = TestBed.inject(ApiTournamentsService);
expect(service).toBeTruthy();
});

});
describe('ApiTournamentsService', () => {
let service: ApiTournamentsService;
let httpMock: HttpTestingController;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
providers: [ ApiTournamentsService ]
});
service = TestBed.inject(ApiTournamentsService);
httpMock = TestBed.inject(HttpTestingController);
});

afterEach(() => {
httpMock.verify();
});

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

it('should get all tournaments', () => {
const mockTournaments = [
{ id: '1', name: 'Tournament 1' },
{ id: '2', name: 'Tournament 2' }
];

service.tournaments.subscribe();

const req = httpMock.expectOne(`${service['_apiEndpoint']}/tournament/tournaments`);
expect(req.request.method).toBe('GET');
req.flush(mockTournaments);
expect(service['_tournaments'].value).toEqual(mockTournaments);
});

it('should get a tournament by id', () => {
const mockTournament = { id: '123', name: 'Tournament 1' };

service.getTournament('123').subscribe();

const req = httpMock.expectOne(`${service['_apiEndpoint']}/tournament?id=${mockTournament.id}`);
expect(req.request.method).toBe('GET');
req.flush(mockTournament);
expect(service['_tournament'].value).toEqual(mockTournament);
});

it('should create a tournament', () => {
const mockTournament = { name: 'New Tournament' };

service.createTournament(mockTournament).subscribe(tournament => {
expect(tournament).toEqual({ id: '1', ...mockTournament });
});

const req = httpMock.expectOne(`${service['_apiEndpoint']}/tournament/create`);
expect(req.request.method).toBe('PUT');
expect(req.request.body).toEqual(mockTournament);
expect(req.request.withCredentials).toBe(true);
req.flush({ id: '1', ...mockTournament });
});

it('should delete a registration', () => {
const mockRegistrationId = '1';

service.deleteRegistration(mockRegistrationId).subscribe(() => {
expect(service.allRegistrations.value).not.toContain(jasmine.objectContaining({ id: mockRegistrationId }));
});

let req = httpMock.expectOne(`${service['_apiEndpoint']}/tournament/delete?id=${mockRegistrationId}`);
expect(req.request.method).toBe('DELETE');
expect(req.request.withCredentials).toBe(true);
req.flush({});
req = httpMock.expectOne(`${service['_apiEndpoint']}/tournament/allregistrations`);
expect(req.request.method).toBe('GET');
expect(req.request.withCredentials).toBe(true);
req.flush([]);
});

it('should submit a CS:GO match', () => {
const mockMatch: TournamentCSGOMatch = { tournamentId: '1', team1Id: 'Team A', team2Id: 'Team B' };

service.submitCSGOMatch(mockMatch).subscribe(match => {
expect(match).toEqual({ id: '1', ...mockMatch });
});

const req = httpMock.expectOne(`${service['_apiEndpoint']}/tournament/csgomatch`);
expect(req.request.method).toBe('PUT');
expect(req.request.body).toEqual(mockMatch);
req.flush({ id: '1', ...mockMatch });
});

it('should delete a CS:GO match', () => {
const mockMatchId = '1';

service.deleteCSGOMatch({ id: mockMatchId } as TournamentCSGOMatch).subscribe(() => {
expect(service['_csgoMatches'].get(mockMatchId)).toBeUndefined();
});

const req = httpMock.expectOne(`${service['_apiEndpoint']}/tournament/csgomatch?id=${mockMatchId}`);
expect(req.request.method).toBe('DELETE');
req.flush({});
});
});
74 changes: 40 additions & 34 deletions projects/common/src/services/bellumgens-api.tournaments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ export class ApiTournamentsService {
if (!this._csgoRegistrations.has(id)) {
this.loadingCSGORegistrations.next(true);
this._csgoRegistrations.set(id, new BehaviorSubject<TournamentParticipant []>(null));
this.getCSGORegistrations(id).subscribe(data => {
this.getCSGORegistrations(id).subscribe({
next: data => {
this._csgoRegistrations.get(id).next(data);
this.loadingCSGORegistrations.next(false);
},
() => this.loadingCSGORegistrations.next(false)
);
complete: () => this.loadingCSGORegistrations.next(false)
});
}
return this._csgoRegistrations.get(id);
}
Expand All @@ -108,12 +109,13 @@ export class ApiTournamentsService {
if (!this._sc2Registrations.has(id)) {
this.loadingSC2Registrations.next(true);
this._sc2Registrations.set(id, new BehaviorSubject<TournamentParticipant []>(null));
this.getSC2Registrations(id).subscribe(data => {
this.getSC2Registrations(id).subscribe({
next: (data) => {
this._sc2Registrations.get(id).next(data);
this.loadingSC2Registrations.next(false);
},
() => this.loadingSC2Registrations.next(false)
);
complete: () => this.loadingSC2Registrations.next(false)
});
}
return this._sc2Registrations.get(id);
}
Expand All @@ -122,12 +124,13 @@ export class ApiTournamentsService {
if (!this._csgoMatches.has(id)) {
this.loadingCSGOMatches.next(true);
this._csgoMatches.set(id, new BehaviorSubject<TournamentCSGOMatch []>(null));
this.getCSGOMatches(id).subscribe(data => {
this.getCSGOMatches(id).subscribe({
next: data => {
this._csgoMatches.get(id).next(data);
this.loadingCSGOMatches.next(false);
},
() => this.loadingCSGOMatches.next(false)
);
complete: () => this.loadingCSGOMatches.next(false)
});
}
return this._csgoMatches.get(id);
}
Expand All @@ -136,12 +139,13 @@ export class ApiTournamentsService {
if (!this._sc2Matches.has(id)) {
this.loadingSC2Matches.next(true);
this._sc2Matches.set(id, new BehaviorSubject<TournamentSC2Match []>(null));
this.getSC2Matches(id).subscribe(data => {
this.getSC2Matches(id).subscribe({
next: (data) => {
this._sc2Matches.get(id).next(data);
this.loadingSC2Matches.next(false);
},
() => this.loadingSC2Matches.next(false)
);
complete: () => this.loadingSC2Matches.next(false)
});
}
return this._sc2Matches.get(id);
}
Expand All @@ -150,12 +154,13 @@ export class ApiTournamentsService {
if (!this._csgoGroups.has(id)) {
this.loadingCSGORegistrations.next(true);
this._csgoGroups.set(id, new BehaviorSubject<TournamentCSGOGroup []>(null));
this.getCSGOGroups(id).subscribe(data => {
this.getCSGOGroups(id).subscribe({
next: (data) => {
this._csgoGroups.get(id).next(data);
this.loadingCSGORegistrations.next(false);
},
() => this.loadingCSGORegistrations.next(false)
);
complete: () => this.loadingCSGORegistrations.next(false)
});
}
return this._csgoGroups.get(id);
}
Expand All @@ -164,12 +169,13 @@ export class ApiTournamentsService {
if (!this._sc2Groups.has(id)) {
this.loadingSC2Registrations.next(true);
this._sc2Groups.set(id, new BehaviorSubject<TournamentSC2Group []>(null));
this.getSC2Groups(id).subscribe(data => {
this.getSC2Groups(id).subscribe({
next: (data) => {
this._sc2Groups.get(id).next(data);
this.loadingSC2Registrations.next(false);
},
() => this.loadingSC2Registrations.next(false)
);
complete: () => this.loadingSC2Registrations.next(false)
});
}
return this._sc2Groups.get(id);
}
Expand All @@ -178,7 +184,7 @@ export class ApiTournamentsService {
return this.http.post<TournamentApplication>(`${this._apiEndpoint}/tournament/register`, application, { withCredentials: true }).pipe(
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -191,7 +197,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -204,7 +210,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -217,7 +223,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -231,7 +237,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -248,7 +254,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -261,7 +267,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -275,7 +281,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -299,7 +305,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -313,7 +319,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -327,7 +333,7 @@ export class ApiTournamentsService {
// }),
// catchError(error => {
// this.commService.emitError(error.error);
// return throwError(error);
// return throwError(() => error);
// })
// );
// }
Expand All @@ -341,7 +347,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -355,7 +361,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -369,7 +375,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -383,7 +389,7 @@ export class ApiTournamentsService {
// }),
// catchError(error => {
// this.commService.emitError(error.error);
// return throwError(error);
// return throwError(() => error);
// })
// );
// }
Expand All @@ -397,7 +403,7 @@ export class ApiTournamentsService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand Down
Loading

0 comments on commit 4a6c336

Please sign in to comment.