Skip to content

Commit

Permalink
test(api search): adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kdinev committed Nov 30, 2023
1 parent b4f9809 commit 66e3c6e
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 139 deletions.
169 changes: 143 additions & 26 deletions projects/common/src/services/bellumgens-api.search.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HttpClientTestingModule, HttpTestingController } from '@angular/common/
import { TestBed } from '@angular/core/testing';
import { ApiSearchService } from './bellumgens-api.search.service';
import { CommunicationService } from './communication.service';
import { SearchResult } from '../public_api';
import { CSGOMap, CSGOPlayer, CSGOTeam, SearchResult, Side } from '../public_api';


describe('ApiSearchService', () => {
Expand All @@ -28,7 +28,6 @@ describe('ApiSearchService', () => {
expect(service).toBeTruthy();
});

// TODO: Extend each test with req.flush({}) to prevent errors in the console
it('should make a GET request to the API endpoint with the provided name', () => {
let name = 'testName';
const result: SearchResult = {
Expand All @@ -55,33 +54,151 @@ describe('ApiSearchService', () => {
req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not retrieve search results!' });
});

describe('searchTeams', () => {
it('should make a GET request to the API endpoint with the provided query', () => {
const query = 'testQuery';
service.teamSearchResult.subscribe();
service.searchTeams(query);
const req = httpMock.expectOne(`${service['_apiEndpoint']}/search/teams?${query}`);
expect(req.request.method).toBe('GET');
});
it('searchTeams should make a GET request to the API endpoint with the provided query', () => {
let query = 'role=1&overlap=1';
const teams: CSGOTeam[] = [
{ teamName: 'testTeam', teamId: '1', teamAvatar: 'testLogo', visible: true, description: 'testDescription'},
{ teamName: 'testTeam2', teamId: '2', teamAvatar: 'testLogo2', visible: true, description: 'testDescription2'}
];
service.searchTeams(query);
const req = httpMock.expectOne(`${service['_apiEndpoint']}/search/teams?${query}`);
expect(req.request.method).toBe('GET');
expect(service.loadingSearch.value).toBeTruthy();
expect(service.teamSearchResult.value).toEqual([]);
req.flush(teams);
expect(service.loadingSearch.value).toBeFalsy();
expect(service.teamSearchResult.value).toEqual(teams);
expect(service['_teamSearchCache'].get(query)).toEqual(teams);

query = 'role=2&overlap=1';
const errorMessage = `Http failure response for ${service['_apiEndpoint']}/search/teams?${query}: 500 Could not retrieve search results!`;
commService.error.subscribe(message => expect(message).toEqual(errorMessage));
service.searchTeams(query);
const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/search/teams?${query}`);
expect(req2.request.method).toBe('GET');
req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not retrieve search results!' });

// Should return teams from the quick search result cache if the query starts with 'name'
query = 'name=test';
const result: SearchResult = {
players: [],
strategies: [],
teams: [
{ teamName: 'test', teamId: '1', teamAvatar: 'testLogo', visible: true, description: 'testDescription'}
],
steamUser: null
};
service.quickSearch('test');
const req3 = httpMock.expectOne(`${service['_apiEndpoint']}/search?name=test`);
expect(req3.request.method).toBe('GET');
req3.flush(result);
service.searchTeams(query);
expect(service.teamSearchResult.value).toEqual(result.teams);
});

describe('searchPlayers', () => {
it('should make a GET request to the API endpoint with the provided query', () => {
const query = 'testQuery';
service.playerSearchResult.subscribe();
service.searchPlayers(query);
const req = httpMock.expectOne(`${service['_apiEndpoint']}/search/players?${query}`);
expect(req.request.method).toBe('GET');
});
it('searchPlayers should make a GET request to the API endpoint with the provided query', () => {
let query = 'role=1&overlap=1';
const players: CSGOPlayer [] = [
{
id: '1',
steamId: 'test-steam-id',
battleNetId: 'test-battlenet-id',
username: 'test-username',
email: 'test-email',
avatarFull: 'test-avatar',
avatarMedium: 'test-avatar',
avatarIcon: 'test-avatar',
customURL: 'test-url',
realname: 'test-realname',
searchVisible: true,
externalLogins: [],
steamUser: null,
steamUserException: false,
userStats: null,
userStatsException: true,
registered: false,
headshotPercentage: 0,
killDeathRatio: 0,
accuracy: 0,
steamPrivate: true
}
];
service.searchPlayers(query);
const req = httpMock.expectOne(`${service['_apiEndpoint']}/search/players?${query}`);
expect(req.request.method).toBe('GET');
expect(service.loadingSearch.value).toBeTruthy();
req.flush(players);
expect(service.loadingSearch.value).toBeFalsy();
expect(service.playerSearchResult.value).toEqual(players);
expect(service['_playerSearchCache'].get(query)).toEqual(players);

query = 'role=2&overlap=1';
const errorMessage = `Http failure response for ${service['_apiEndpoint']}/search/players?${query}: 500 Could not retrieve search results!`;
commService.error.subscribe(message => expect(message).toEqual(errorMessage));
service.searchPlayers(query);
const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/search/players?${query}`);
expect(req2.request.method).toBe('GET');
req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not retrieve search results!' });

query = 'name=test';
const result: SearchResult = {
players: players,
strategies: [],
teams: [
{ teamName: 'test', teamId: '1', teamAvatar: 'testLogo', visible: true, description: 'testDescription'}
],
steamUser: null
};
service.quickSearch('test');
const req3 = httpMock.expectOne(`${service['_apiEndpoint']}/search?name=test`);
expect(req3.request.method).toBe('GET');
req3.flush(result);
service.searchPlayers(query);
expect(service.playerSearchResult.value).toEqual(result.players);
});

describe('searchStrategies', () => {
it('should make a GET request to the API endpoint with the provided query', () => {
const query = 'testQuery';
service.strategySearchResult.subscribe();
service.searchStrategies(query);
const req = httpMock.expectOne(`${service['_apiEndpoint']}/search/strategies?${query}`);
expect(req.request.method).toBe('GET');
});
it('searchStrategies should make a GET request to the API endpoint with the provided query', () => {
let query = 'testQuery';
service.searchStrategies(query);
const req = httpMock.expectOne(`${service['_apiEndpoint']}/search/strategies?${query}`);
expect(req.request.method).toBe('GET');
expect(service.loadingSearch.value).toBeTruthy();
req.flush([]);
expect(service.loadingSearch.value).toBeFalsy();
expect(service.strategySearchResult.value).toEqual([]);
expect(service['_strategySearchCache'].get(query)).toEqual([]);

query = 'testQuery2';
const errorMessage = `Http failure response for ${service['_apiEndpoint']}/search/strategies?${query}: 500 Could not retrieve search results!`;
commService.error.subscribe(message => expect(message).toEqual(errorMessage));
service.searchStrategies(query);
const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/search/strategies?${query}`);
expect(req2.request.method).toBe('GET');
req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not retrieve search results!' });

// Should return strategies from the quick search result cache if the query starts with 'name'
query = 'name=test';
const result: SearchResult = {
players: [],
strategies: [
{
id: '123456',
title: 'Test 1',
description: 'Test 2',
map: CSGOMap.Dust2,
side: Side.TSide,
teamId: '123',
url: 'test.com'
}
],
teams: [],
steamUser: null
};
service.quickSearch('test');
const req3 = httpMock.expectOne(`${service['_apiEndpoint']}/search?name=test`);
expect(req3.request.method).toBe('GET');
req3.flush(result);
service.searchStrategies(query);
expect(service.strategySearchResult.value).toEqual(result.strategies);
});
});
27 changes: 15 additions & 12 deletions projects/common/src/services/bellumgens-api.search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ export class ApiSearchService {
} else {
this.teamSearchResult.next([]);
this.loadingSearch.next(true);
this.getFilteredTeams(query).subscribe(
teams => {
this.getFilteredTeams(query).subscribe({
next: teams => {
this._teamSearchCache.set(query, teams);
this.teamSearchResult.next(teams);
this.loadingSearch.next(false);
}
);
},
error: () => this.loadingSearch.next(false)
});
}
}
}
Expand All @@ -81,13 +82,14 @@ export class ApiSearchService {
} else {
this.playerSearchResult.next([]);
this.loadingSearch.next(true);
this.getFilteredPlayers(query).subscribe(
players => {
this.getFilteredPlayers(query).subscribe({
next: players => {
this._playerSearchCache.set(query, players);
this.playerSearchResult.next(players);
this.loadingSearch.next(false);
}
);
},
error: () => this.loadingSearch.next(false)
});
}
}
}
Expand All @@ -104,13 +106,14 @@ export class ApiSearchService {
} else {
this.strategySearchResult.next([]);
this.loadingSearch.next(true);
this.getFilteredStrategies(query).subscribe(
strategies => {
this.getFilteredStrategies(query).subscribe({
next: strategies => {
this._strategySearchCache.set(query, strategies);
this.strategySearchResult.next(strategies);
this.loadingSearch.next(false);
}
);
},
error: () => this.loadingSearch.next(false)
});
}
}
}
Expand Down
Loading

0 comments on commit 66e3c6e

Please sign in to comment.