From 66e3c6e9f2b5f94042f7c0699874c42e4fed89c3 Mon Sep 17 00:00:00 2001 From: Konstantin Dinev Date: Thu, 30 Nov 2023 18:46:09 +0200 Subject: [PATCH] test(api search): adding more tests --- .../bellumgens-api.search.service.spec.ts | 169 +++++++++++++++--- .../services/bellumgens-api.search.service.ts | 27 +-- .../services/bellumgens-api.service.spec.ts | 148 +++++---------- 3 files changed, 205 insertions(+), 139 deletions(-) diff --git a/projects/common/src/services/bellumgens-api.search.service.spec.ts b/projects/common/src/services/bellumgens-api.search.service.spec.ts index 50ba0845..e582280e 100644 --- a/projects/common/src/services/bellumgens-api.search.service.spec.ts +++ b/projects/common/src/services/bellumgens-api.search.service.spec.ts @@ -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', () => { @@ -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 = { @@ -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); }); }); diff --git a/projects/common/src/services/bellumgens-api.search.service.ts b/projects/common/src/services/bellumgens-api.search.service.ts index 1a992785..0a28545b 100644 --- a/projects/common/src/services/bellumgens-api.search.service.ts +++ b/projects/common/src/services/bellumgens-api.search.service.ts @@ -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) + }); } } } @@ -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) + }); } } } @@ -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) + }); } } } diff --git a/projects/common/src/services/bellumgens-api.service.spec.ts b/projects/common/src/services/bellumgens-api.service.spec.ts index 53cdf98f..47d76328 100644 --- a/projects/common/src/services/bellumgens-api.service.spec.ts +++ b/projects/common/src/services/bellumgens-api.service.spec.ts @@ -546,121 +546,67 @@ describe('BellumgensApiService', () => { req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not update availability!' }); }); - it('setPrimaryRole should send a PUT request to the correct URL', () => { - const role: Role = { id: PlaystyleRole.Awper, name: 'Awper' }; - commsService.success.subscribe(success => expect(success).toBe(`Primary role set to ${role.name}`)); - service.setPrimaryRole(role).subscribe(); - const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/primaryrole?id=${role.id}`); - expect(req.request.method).toBe('PUT'); - expect(req.request.body).toEqual(role); - expect(req.request.withCredentials).toBe(true); - req.flush({}); - - const errorMessage = `Http failure response for ${service['_apiEndpoint']}/users/primaryrole?id=${role.id}: 500 Could not update primary role!`; - commsService.error.subscribe(error => expect(error).toBe(errorMessage)); - service.setPrimaryRole(role).subscribe({ - error: err => expect(err.message).toBe(errorMessage) + describe('setPrimaryRole', () => { + it('should send a PUT request to the correct URL', () => { + const role: Role = { id: PlaystyleRole.Awper, name: 'Awper' }; + service.setPrimaryRole(role).subscribe(); + const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/primaryrole?id=${role.id}`); + expect(req.request.method).toBe('PUT'); + expect(req.request.body).toEqual(role); + expect(req.request.withCredentials).toBe(true); }); - const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/users/primaryrole?id=${role.id}`); - expect(req2.request.method).toBe('PUT'); - expect(req2.request.body).toEqual(role); - expect(req2.request.withCredentials).toBe(true); - req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not update primary role!' }); }); - it('setSecondaryRole should send a PUT request to the correct URL', () => { - const role: Role = { id: PlaystyleRole.IGL, name: 'Ingame Leader' }; - commsService.success.subscribe(success => expect(success).toBe(`Secondary role set to ${role.name}`)); - service.setSecondaryRole(role).subscribe(); - const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/secondaryrole?id=${role.id}`); - expect(req.request.method).toBe('PUT'); - expect(req.request.body).toEqual(role); - expect(req.request.withCredentials).toBe(true); - req.flush({}); - - const errorMessage = `Http failure response for ${service['_apiEndpoint']}/users/secondaryrole?id=${role.id}: 500 Could not update secondary role!`; - commsService.error.subscribe(error => expect(error).toBe(errorMessage)); - service.setSecondaryRole(role).subscribe({ - error: err => expect(err.message).toBe(errorMessage) + describe('setSecondaryRole', () => { + it('should send a PUT request to the correct URL', () => { + const role: Role = { id: PlaystyleRole.IGL, name: 'Ingame Leader' }; + service.setSecondaryRole(role).subscribe(); + const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/secondaryrole?id=${role.id}`); + expect(req.request.method).toBe('PUT'); + expect(req.request.body).toEqual(role); + expect(req.request.withCredentials).toBe(true); }); - const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/users/secondaryrole?id=${role.id}`); - expect(req2.request.method).toBe('PUT'); - expect(req2.request.body).toEqual(role); - expect(req2.request.withCredentials).toBe(true); - req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not update secondary role!' }); }); - it('getMapPool should send a GET request to the correct URL', () => { - const userId = '234'; - service.getMapPool(userId).subscribe(); - const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/mapPool?userid=${userId}`); - expect(req.request.method).toBe('GET'); - req.flush({}); + describe('getMapPool', () => { + it('should send a GET request to the correct URL', () => { + const userId = '234'; + service.getMapPool(userId).subscribe(); + const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/mapPool?userid=${userId}`); + expect(req.request.method).toBe('GET'); + }); }); - it('setMapPool should send a PUT request to the correct URL', () => { - const mapstatus: CSGOMapPool = { mapId: CSGOMap.Dust2, isPlayed: true }; - commsService.success.subscribe(success => expect(success).toBe('Map pool updated!')); - service.setMapPool(mapstatus).subscribe(); - const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/mapPool`); - expect(req.request.method).toBe('PUT'); - expect(req.request.body).toEqual(mapstatus); - expect(req.request.withCredentials).toBe(true); - req.flush({}); - - const errorMessage = `Http failure response for ${service['_apiEndpoint']}/users/mapPool: 500 Could not update map pool!`; - commsService.error.subscribe(error => expect(error).toBe(errorMessage)); - service.setMapPool(mapstatus).subscribe({ - error: err => expect(err.message).toBe(errorMessage) + describe('setMapPool', () => { + it('should send a PUT request to the correct URL', () => { + const mapstatus: CSGOMapPool = { mapId: CSGOMap.Dust2, isPlayed: true }; + service.setMapPool(mapstatus).subscribe(); + const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/mapPool`); + expect(req.request.method).toBe('PUT'); + expect(req.request.body).toEqual(mapstatus); + expect(req.request.withCredentials).toBe(true); }); - const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/users/mapPool`); - expect(req2.request.method).toBe('PUT'); - expect(req2.request.body).toEqual(mapstatus); - expect(req2.request.withCredentials).toBe(true); - req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not update map pool!' }); }); - it('acceptInvite should send a PUT request to the correct URL', () => { - const notification: UserNotification = { state: NotificationState.NotSeen, teamInfo: null, invitingUser: null, sent: '' }; - commsService.success.subscribe(success => expect(success).toBe('Team invite accepted!')); - service.acceptInvite(notification).subscribe(); - const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/acceptTeamInvite`); - expect(req.request.method).toBe('PUT'); - expect(req.request.body).toEqual(notification); - expect(req.request.withCredentials).toBe(true); - req.flush({}); - - const errorMessage = `Http failure response for ${service['_apiEndpoint']}/users/acceptTeamInvite: 500 Could not accept team invite!`; - commsService.error.subscribe(error => expect(error).toBe(errorMessage)); - service.acceptInvite(notification).subscribe({ - error: err => expect(err.message).toBe(errorMessage) + describe('acceptInvite', () => { + it('should send a PUT request to the correct URL', () => { + const notification: UserNotification = { state: NotificationState.NotSeen, teamInfo: null, invitingUser: null, sent: '' }; + service.acceptInvite(notification).subscribe(); + const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/acceptTeamInvite`); + expect(req.request.method).toBe('PUT'); + expect(req.request.body).toEqual(notification); + expect(req.request.withCredentials).toBe(true); }); - const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/users/acceptTeamInvite`); - expect(req2.request.method).toBe('PUT'); - expect(req2.request.body).toEqual(notification); - expect(req2.request.withCredentials).toBe(true); - req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not accept team invite!' }); }); - it('rejectInvite should send a PUT request to the correct URL', () => { - const notification: UserNotification = { state: NotificationState.NotSeen, teamInfo: null, invitingUser: null, sent: '' }; - commsService.success.subscribe(success => expect(success).toBe('Team invite rejected!')); - service.rejectInvite(notification).subscribe(); - const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/rejectTeamInvite`); - expect(req.request.method).toBe('PUT'); - expect(req.request.body).toEqual(notification); - expect(req.request.withCredentials).toBe(true); - req.flush({}); - - const errorMessage = `Http failure response for ${service['_apiEndpoint']}/users/rejectTeamInvite: 500 Could not reject team invite!`; - commsService.error.subscribe(error => expect(error).toBe(errorMessage)); - service.rejectInvite(notification).subscribe({ - error: err => expect(err.message).toBe(errorMessage) + describe('rejectInvite', () => { + it('should send a PUT request to the correct URL', () => { + const notification: UserNotification = { state: NotificationState.NotSeen, teamInfo: null, invitingUser: null, sent: '' }; + service.rejectInvite(notification).subscribe(); + const req = httpMock.expectOne(`${service['_apiEndpoint']}/users/rejectTeamInvite`); + expect(req.request.method).toBe('PUT'); + expect(req.request.body).toEqual(notification); + expect(req.request.withCredentials).toBe(true); }); - const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/users/rejectTeamInvite`); - expect(req2.request.method).toBe('PUT'); - expect(req2.request.body).toEqual(notification); - expect(req2.request.withCredentials).toBe(true); - req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not reject team invite!' }); }); });