Skip to content

Commit

Permalink
test(strategies 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 fc58a9e commit 802518d
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 22 deletions.
5 changes: 4 additions & 1 deletion projects/bellumgens/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ module.exports = function (config) {
'@angular-devkit/build-angular/plugins/karma'
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
clearContext: false, // leave Jasmine Spec Runner output visible in browser
jasmine: {
random: false
}
},
coverageReporter: {
dir: require('path').join(__dirname, '../../coverage'),
Expand Down
5 changes: 4 additions & 1 deletion projects/common/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ module.exports = function (config) {
'@angular-devkit/build-angular/plugins/karma'
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
clearContext: false, // leave Jasmine Spec Runner output visible in browser
jasmine: {
random: false
}
},
coverageReporter: {
dir: require('path').join(__dirname, '../../coverage'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('ApiShopService', () => {
service.deleteOrder(orderId).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/shop/order?orderId=${orderId}`);
expect(req.request.method).toBe('DELETE');
expect(req.request.withCredentials).toEqual(true);
req.flush({});
});
});
Expand All @@ -62,6 +63,7 @@ describe('ApiShopService', () => {
service.getOrders().subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/shop/orders`);
expect(req.request.method).toBe('GET');
expect(req.request.withCredentials).toEqual(true);
req.flush([]);
});
});
Expand All @@ -73,6 +75,7 @@ describe('ApiShopService', () => {
const req = httpMock.expectOne(`${service['_apiEndpoint']}/shop/edit?orderId=${order.id}`);
expect(req.request.method).toBe('PUT');
expect(req.request.body).toEqual(order);
expect(req.request.withCredentials).toEqual(true);
req.flush({});
});
});
Expand Down
4 changes: 2 additions & 2 deletions projects/common/src/services/bellumgens-api.shop.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class ApiShopService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);;
}
Expand All @@ -47,7 +47,7 @@ export class ApiShopService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand Down
204 changes: 202 additions & 2 deletions projects/common/src/services/bellumgens-api.strategies.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,217 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';

import { CSGOStrategy, Side, StrategyComment, VoteDirection } from '../models/csgostrategy';
import { ApiStrategiesService } from './bellumgens-api.strategies.service';
import { CSGOMap } from '../public_api';

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

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

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

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

it('should load strategies page on `strategies` getter', () => {
const page = 0;
const sub = service.strategies.subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/strategies?page=${page}`);
expect(req.request.method).toEqual('GET');
req.flush([]);
expect(service['_strategies'].value).toEqual([]);
sub.unsubscribe();
});

it('should load strategies page', () => {
const page = 0;
service.loadStrategiesPage(page);
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/strategies?page=${page}`);
expect(req.request.method).toEqual('GET');
req.flush([]);
expect(service['_strategies'].value).toEqual([]);
});

it('should get user strategies', () => {
const userId = '123';
service.getUserStrategies(userId).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/userstrats?userid=${userId}`);
expect(req.request.method).toEqual('GET');
expect(req.request.withCredentials).toEqual(true);
req.flush([]);
expect(service['_strategies'].value).toEqual([]);
});

it('should get team strat', () => {
const stratId = '012';
service.getTeamStrat(stratId).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/strat?stratId=${stratId}`);
expect(req.request.method).toEqual('GET');
expect(req.request.withCredentials).toEqual(true);
req.flush({});
});

it('should get team strats', () => {
const teamId = '123';
service.getTeamStrats(teamId).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/teamstrats?teamid=${teamId}`);
expect(req.request.method).toEqual('GET');
expect(req.request.withCredentials).toEqual(true);
req.flush([]);
expect(service['_strategies'].value).toEqual([]);
});

it('should get team map pool', () => {
const teamId = '123';
service.getTeamMapPool(teamId).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/teams/mapPool?teamId=${teamId}`);
expect(req.request.method).toEqual('GET');
expect(req.request.withCredentials).toEqual(true);
req.flush([]);
expect(service['_strategies'].value).toEqual([]);
});

it('should get strategy', () => {
const stratId = '123456789';
service.getStrategy(stratId);
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/strat?stratId=${stratId}`);
expect(req.request.method).toEqual('GET');
expect(req.request.withCredentials).toEqual(true);
req.flush({
id: '123456789',
title: 'Test',
description: 'Test',
map: CSGOMap.Dust2,
side: Side.TSide,
teamId: '123',
url: 'test'
});
expect(service['_strategyCache'].get(stratId).value).toEqual({
id: '123456789',
title: 'Test',
description: 'Test',
map: CSGOMap.Dust2,
side: Side.TSide,
teamId: '123',
url: 'test'
});
});

it('should submit strategy', () => {
const strat: CSGOStrategy = {
id: '123456',
title: 'Test 1',
description: 'Test 2',
map: CSGOMap.Dust2,
side: Side.TSide,
teamId: '123',
url: 'test.com'
};
service.submitStrategy(strat).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/strategy`);
expect(req.request.method).toEqual('POST');
expect(req.request.body).toEqual(strat);
expect(req.request.withCredentials).toEqual(true);
req.flush(strat);
expect(service['_strategyCache'].get(strat.id).value).toEqual({
id: '123456',
title: 'Test 1',
description: 'Test 2',
map: CSGOMap.Dust2,
side: Side.TSide,
teamId: '123',
url: 'test.com'
});
});

it('should submit strat vote', () => {
const strat: CSGOStrategy = {
id: '123',
title: 'Test',
description: 'Test',
map: CSGOMap.Dust2,
side: Side.TSide,
teamId: '123',
url: 'test',
votes: []
};
const direction = VoteDirection.Up;
const userId = '123';
service.submitStratVote(strat, direction, userId).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/vote`);
expect(req.request.method).toEqual('POST');
expect(req.request.body).toEqual({ id: strat.id, direction });
expect(req.request.withCredentials).toEqual(true);
req.flush({});
});

it('should submit strat comment', () => {
const comment: StrategyComment = { id: '123', stratId: '456', comment: 'Test', userId: '123', published: new Date() };
const strat: CSGOStrategy = {
id: '456',
title: 'Test',
description: 'Test',
map: CSGOMap.Dust2,
side: Side.TSide,
teamId: '123',
url: 'test',
comments: []
};
service.submitStratComment(comment, strat).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/comment`);
expect(req.request.method).toEqual('POST');
expect(req.request.body).toEqual(comment);
expect(req.request.withCredentials).toEqual(true);
req.flush({});
});

it('should delete strat comment', () => {
const comment: StrategyComment = { id: '123', stratId: '456', comment: 'Test', userId: '123', published: new Date() };
const strat: CSGOStrategy = {
id: '456',
title: 'Test',
description: 'Test',
map: CSGOMap.Dust2,
side: Side.TSide,
teamId: '123',
url: 'test',
comments: [
{ id: '123', stratId: '456', comment: 'Test', userId: '123', published: new Date() }
]
};
service.deleteStratComment(comment, strat).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/comment?id=${comment.id}`);
expect(req.request.method).toEqual('DELETE');
expect(req.request.withCredentials).toEqual(true);
req.flush({});
});

it('should delete strategy', () => {
const stratId = '345';
service.deleteStrategy(stratId).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/strat?id=${stratId}`);
expect(req.request.method).toEqual('DELETE');
expect(req.request.withCredentials).toEqual(true);
req.flush({});
});

it('should handle errors', () => {
const stratId = '234';
service.getTeamStrat(stratId).subscribe({
next: () => fail('Should not succeed'),
error: (error) => expect(error.status).toEqual(404)
});
const req = httpMock.expectOne(`${service['_apiEndpoint']}/strategy/strat?stratId=${stratId}`);
expect(req.request.method).toEqual('GET');
req.error(new ProgressEvent('Not Found'), { status: 404, statusText: 'Not Found' });
});
});
33 changes: 18 additions & 15 deletions projects/common/src/services/bellumgens-api.strategies.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ export class ApiStrategiesService {
this._strategies.next(data);
this.loadingStrategies.next(false);
this.hasMoreStrats.next(data.length === PAGE_SIZE);
},
error => {
this.loadingStrategies.next(false);
this.commService.emitError(error.error);
});
}
);
}
return this._strategies;
}
Expand All @@ -44,10 +41,6 @@ export class ApiStrategiesService {
this._strategies.next(this._strategies.value.concat(data));
this.loadingStrategies.next(false);
this.hasMoreStrats.next(data.length === PAGE_SIZE);
},
error => {
this.loadingStrategies.next(false);
this.commService.emitError(error.error);
}
);
}
Expand Down Expand Up @@ -91,7 +84,7 @@ export class ApiStrategiesService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -117,7 +110,7 @@ export class ApiStrategiesService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -139,7 +132,7 @@ export class ApiStrategiesService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -154,7 +147,7 @@ export class ApiStrategiesService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}
Expand All @@ -167,12 +160,22 @@ export class ApiStrategiesService {
}),
catchError(error => {
this.commService.emitError(error.error);
return throwError(error);
return throwError(() => error);
})
);
}

// TODO: Refactor to send page size as well
private getStrategies(page: number = 0) {
return this.http.get<CSGOStrategy []>(`${this._apiEndpoint}/strategy/strategies?page=${page}`);
return this.http.get<CSGOStrategy []>(`${this._apiEndpoint}/strategy/strategies?page=${page}`).pipe(
map(response => {
return response;
}),
catchError(error => {
this.commService.emitError(error.error);
this.loadingStrategies.next(false);
return throwError(() => error);
})
);
}
}
5 changes: 4 additions & 1 deletion projects/ebleague/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ module.exports = function (config) {
'@angular-devkit/build-angular/plugins/karma'
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
clearContext: false, // leave Jasmine Spec Runner output visible in browser
jasmine: {
random: false
}
},
coverageReporter: {
dir: require('path').join(__dirname, '../../coverage'),
Expand Down

0 comments on commit 802518d

Please sign in to comment.