Skip to content

Commit

Permalink
test(*): bumping coverage more
Browse files Browse the repository at this point in the history
  • Loading branch information
kdinev committed Dec 6, 2023
1 parent b841351 commit b21b503
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 31 deletions.
27 changes: 25 additions & 2 deletions projects/common/src/services/bellumgens-api.search.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ describe('ApiSearchService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
providers: [ ApiSearchService ]
imports: [ HttpClientTestingModule ]
});
service = TestBed.inject(ApiSearchService);
commService = TestBed.inject(CommunicationService);
Expand Down Expand Up @@ -52,6 +51,12 @@ describe('ApiSearchService', () => {
const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/search?name=${name}`);
expect(req2.request.method).toBe('GET');
req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not retrieve search results!' });

// Should return the search result from the cache if the name is already cached
name = 'testName';
service.quickSearch(name);
expect(service.loadingQuickSearch.value).toBeFalsy();
expect(service.searchResult.value).toEqual(result);
});

it('searchTeams should make a GET request to the API endpoint with the provided query', () => {
Expand Down Expand Up @@ -94,6 +99,12 @@ describe('ApiSearchService', () => {
req3.flush(result);
service.searchTeams(query);
expect(service.teamSearchResult.value).toEqual(result.teams);

// Should return the team search result from the cache if the query is already cached
query = 'role=1&overlap=1';
service.searchTeams(query);
expect(service.loadingSearch.value).toBeFalsy();
expect(service.teamSearchResult.value).toEqual(teams);
});

it('searchPlayers should make a GET request to the API endpoint with the provided query', () => {
Expand Down Expand Up @@ -155,6 +166,12 @@ describe('ApiSearchService', () => {
req3.flush(result);
service.searchPlayers(query);
expect(service.playerSearchResult.value).toEqual(result.players);

// Should return the player search result from the cache if the query is already cached
query = 'role=1&overlap=1';
service.searchPlayers(query);
expect(service.loadingSearch.value).toBeFalsy();
expect(service.playerSearchResult.value).toEqual(players);
});

it('searchStrategies should make a GET request to the API endpoint with the provided query', () => {
Expand Down Expand Up @@ -200,5 +217,11 @@ describe('ApiSearchService', () => {
req3.flush(result);
service.searchStrategies(query);
expect(service.strategySearchResult.value).toEqual(result.strategies);

// Should return the strategy search result from the cache if the query is already cached
query = 'testQuery';
service.searchStrategies(query);
expect(service.loadingSearch.value).toBeFalsy();
expect(service.strategySearchResult.value).toEqual([]);
});
});
3 changes: 1 addition & 2 deletions projects/common/src/services/bellumgens-api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ describe('BellumgensApiService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
providers: [ BellumgensApiService ]
imports: [ HttpClientTestingModule ]
});
service = TestBed.inject(BellumgensApiService);
httpMock = TestBed.inject(HttpTestingController);
Expand Down
36 changes: 32 additions & 4 deletions projects/common/src/services/bellumgens-api.shop.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { ApiShopService } from './bellumgens-api.shop.service';
import { JerseyCut, JerseyOrder, JerseySize } from '../models/jerseyorder';
import { JerseyCut, JerseyOrder, JerseySize, Promo } from '../models/jerseyorder';
import { CommunicationService } from './communication.service';



describe('ApiShopService', () => {
let service: ApiShopService;
let httpMock: HttpTestingController;
let commsService: CommunicationService;

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

afterEach(() => {
Expand All @@ -40,21 +42,34 @@ describe('ApiShopService', () => {
describe('deleteOrder', () => {
it('should send a DELETE request to the API with the order ID', () => {
const orderId = '123';
commsService.success.subscribe(message => expect(message).toEqual('Order deleted successfully!'));
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({});

const errorMessage = `Http failure response for ${service['_apiEndpoint']}/shop/order?orderId=${orderId}: 500 Could not delete order!`;
commsService.error.subscribe(message => expect(message).toEqual(errorMessage));
service.deleteOrder(orderId).subscribe({
next: () => fail('Should not succeed'),
error: error => expect(error.message).toEqual(errorMessage)
});
const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/shop/order?orderId=${orderId}`);
expect(req2.request.method).toBe('DELETE');
expect(req2.request.withCredentials).toEqual(true);
req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not delete order!' });
});
});

describe('checkForPromo', () => {
it('should send a GET request to the API with the promo code', () => {
const code = 'SUMMER21';
const promo: Promo = { code: code, discount: .1, expiration: new Date() };
service.checkForPromo(code).subscribe();
const req = httpMock.expectOne(`${service['_apiEndpoint']}/shop/promo?code=${code}`);
expect(req.request.method).toBe('GET');
req.flush({});
req.flush(promo);
});
});

Expand All @@ -71,12 +86,25 @@ describe('ApiShopService', () => {
describe('confirmOrder', () => {
it('should send a PUT request to the API with the order data', () => {
const order: JerseyOrder = { id: '123', jerseys: [{ cut: JerseyCut.Male, size: JerseySize.L }] };
commsService.success.subscribe(message => expect(message).toEqual('Order confirmed successfully!'));
service.confirmOrder(order).subscribe();
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({});

const errorMessage = `Http failure response for ${service['_apiEndpoint']}/shop/edit?orderId=${order.id}: 500 Could not confirm order!`;
commsService.error.subscribe(message => expect(message).toEqual(errorMessage));
service.confirmOrder(order).subscribe({
next: () => fail('Should not succeed'),
error: error => expect(error.message).toEqual(errorMessage)
});
const req2 = httpMock.expectOne(`${service['_apiEndpoint']}/shop/edit?orderId=${order.id}`);
expect(req2.request.method).toBe('PUT');
expect(req2.request.body).toEqual(order);
expect(req2.request.withCredentials).toEqual(true);
req2.error(new ProgressEvent('Server Error'), { status: 500, statusText: 'Could not confirm order!' });
});
});
});
Loading

0 comments on commit b21b503

Please sign in to comment.