From d9e1b2cbb2f0b736a9747cfe3746be1ce2a42fa5 Mon Sep 17 00:00:00 2001 From: Esteban <26440947+esteban-gs@users.noreply.github.com> Date: Fri, 6 Jan 2023 21:19:38 -0800 Subject: [PATCH] unit-test: use angular client (#140) --- package-lock.json | 36 +++++++---------- package.json | 2 +- .../transport-providers.service.spec.ts | 40 +++++++++++++------ 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index f780c4d..915e630 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5447,7 +5447,8 @@ "buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", - "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", + "dev": true }, "buffer-indexof": { "version": "1.1.1", @@ -10632,12 +10633,18 @@ "integrity": "sha512-ZRioYLCgRHrtTORaZX1mx+jtxKtKuI5ZDvHNAmqpUzGqSrR+tL4FVLn/CUGMA3h0+AKD1MAxGI5GnCqR5txNqg==" }, "machete-client": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/machete-client/-/machete-client-1.0.2.tgz", - "integrity": "sha512-V4591e8mZQIM5kvFUEIjNwpkyLSbAyetSboILwQMqTeGOQ+rYcvcPiyhaxQbbWgnPORP/C2CIoO9AAat08vd/A==", + "version": "1.15.92", + "resolved": "https://registry.npmjs.org/machete-client/-/machete-client-1.15.92.tgz", + "integrity": "sha512-pBkQFqs9/YaBTxVZcp9U/iwHgPUKjMwQBPe8ceTPlpCec9LVm0nysaU8v6/w+mZ6mYiptrgb+QZvW+TEgeCO2A==", "requires": { - "@angular/common": "^12.2.16", - "@angular/core": "^12.2.16" + "tslib": "^2.2.0" + }, + "dependencies": { + "tslib": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz", + "integrity": "sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==" + } } }, "magic-string": { @@ -17135,11 +17142,6 @@ "terser": "^5.7.2" }, "dependencies": { - "acorn": { - "version": "8.7.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.7.0.tgz", - "integrity": "sha512-V/LGr1APy+PXIwKebEWrkZPwoeoF+w1jiOBUmuxuiUIaOHtob8Qc9BTrYo7VuI5fR8tqsy+buA2WFooR5olqvQ==" - }, "ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -17189,16 +17191,8 @@ "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" - }, - "source-map-support": { - "version": "0.5.21", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", - "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", - "requires": { - "buffer-from": "^1.0.0", - "source-map": "^0.6.0" - } + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true }, "supports-color": { "version": "8.1.1", diff --git a/package.json b/package.json index 96dcefd..8d0c28e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app/online-orders/transport-providers.service.spec.ts b/src/app/online-orders/transport-providers.service.spec.ts index 3f9bd8a..70137e0 100644 --- a/src/app/online-orders/transport-providers.service.spec.ts +++ b/src/app/online-orders/transport-providers.service.spec.ts @@ -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, @@ -14,19 +18,20 @@ import { of } from "rxjs"; describe("TransportProviderService", () => { let service: TransportProvidersService; - let clientSpy: jasmine.SpyObj; let messageSpy: jasmine.SpyObj; - let httpClientSpy: jasmine.SpyObj; + let tpClient: jasmine.SpyObj; 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, @@ -36,7 +41,9 @@ describe("TransportProviderService", () => { imports: [], }); service = TestBed.inject(TransportProvidersService); - httpClientSpy = TestBed.inject(HttpClient) as jasmine.SpyObj; + tpClient = TestBed.inject( + TransportProvidersClient + ) as jasmine.SpyObj; messageSpy = TestBed.inject( MessagesService ) as jasmine.SpyObj; @@ -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)[ + "apiTransportProvidersGet" + ] as jasmine.Spy + ).and.returnValue( of({ - data: [new TransportProvider()], + data: [tpVm], }) ); }); @@ -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(); }); }); @@ -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(); }); });