Skip to content

Commit

Permalink
unit-test: use angular client (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
esteban-gs authored Jan 7, 2023
1 parent 2b251ef commit d9e1b2c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
36 changes: 15 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"file-saver": "^1.3.8",
"jquery": "^3.6.0",
"luxon": "^2.0.2",
"machete-client": "^1.0.2",
"machete-client": "1.15.92",
"nanoscroller": "0.8.7",
"ngx-print": "^1.2.1",
"npm": "^9.1.2",
Expand Down
40 changes: 28 additions & 12 deletions src/app/online-orders/transport-providers.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import { TestBed } from "@angular/core/testing";

import { TransportProvidersService } from "./transport-providers.service";
import { HttpClient, HttpClientModule } from "@angular/common/http";
import {
TransportProvidersService as TransportProvidersClient,
TransportProviderVM,
} from "machete-client";
import { HttpClientModule } from "@angular/common/http";
import {
HttpClientTestingModule,
HttpTestingController,
Expand All @@ -14,19 +18,20 @@ import { of } from "rxjs";

describe("TransportProviderService", () => {
let service: TransportProvidersService;
let clientSpy: jasmine.SpyObj<HttpClient>;
let messageSpy: jasmine.SpyObj<MessagesService>;
let httpClientSpy: jasmine.SpyObj<HttpClient>;
let tpClient: jasmine.SpyObj<TransportProvidersClient>;

beforeEach(() => {
httpClientSpy = jasmine.createSpyObj("HttpClient", ["get"]);
tpClient = jasmine.createSpyObj("TransportProvidersClient", [
"apiTransportProvidersGet",
]);

TestBed.configureTestingModule({
providers: [
TransportProvidersService,
{
provide: HttpClient,
useValue: httpClientSpy,
provide: TransportProvidersClient,
useValue: tpClient,
},
{
provide: MessagesService,
Expand All @@ -36,7 +41,9 @@ describe("TransportProviderService", () => {
imports: [],
});
service = TestBed.inject(TransportProvidersService);
httpClientSpy = TestBed.inject(HttpClient) as jasmine.SpyObj<HttpClient>;
tpClient = TestBed.inject(
TransportProvidersClient
) as jasmine.SpyObj<TransportProvidersClient>;
messageSpy = TestBed.inject(
MessagesService
) as jasmine.SpyObj<MessagesService>;
Expand All @@ -48,10 +55,19 @@ describe("TransportProviderService", () => {

describe("chaching", () => {
beforeEach(() => {
spyOn(HttpClient.prototype, "get").calls.reset();
httpClientSpy.get.and.returnValue(
const tpVm: TransportProviderVM = {};
spyOn(
TransportProvidersClient.prototype,
"apiTransportProvidersGet"
).calls.reset();
// ah! typescript
(
(tpClient as jasmine.SpyObj<TransportProviderVM>)[
"apiTransportProvidersGet"
] as jasmine.Spy
).and.returnValue(
of({
data: [new TransportProvider()],
data: [tpVm],
})
);
});
Expand All @@ -71,7 +87,7 @@ describe("TransportProviderService", () => {

it("when none, should callApi", (done: DoneFn) => {
service.getTransportProviders().subscribe(() => {
expect(httpClientSpy.get.calls.count()).toBe(1);
expect(tpClient["apiTransportProvidersGet"].calls.count()).toBe(1);
done();
});
});
Expand All @@ -80,7 +96,7 @@ describe("TransportProviderService", () => {
service.providersAge = Date.now() - 300 * 999; // age logic is in service

service.getTransportProviders().subscribe(() => {
expect(httpClientSpy.get.calls.count()).toBe(0);
expect(tpClient["apiTransportProvidersGet"].calls.count()).toBe(0);
done();
});
});
Expand Down

0 comments on commit d9e1b2c

Please sign in to comment.