diff --git a/projects/ngx-material-entity/package.json b/projects/ngx-material-entity/package.json index c44cce6..4f19a21 100644 --- a/projects/ngx-material-entity/package.json +++ b/projects/ngx-material-entity/package.json @@ -1,6 +1,6 @@ { "name": "ngx-material-entity", - "version": "20.0.6", + "version": "20.0.7", "license": "MIT", "keywords": [ "angular", diff --git a/projects/ngx-material-entity/src/mocks/http-client.mock.ts b/projects/ngx-material-entity/src/mocks/http-client.mock.ts index e5ddd0b..dc95b83 100644 --- a/projects/ngx-material-entity/src/mocks/http-client.mock.ts +++ b/projects/ngx-material-entity/src/mocks/http-client.mock.ts @@ -2,29 +2,29 @@ /* eslint-disable typescript/no-unsafe-assignment */ /* eslint-disable typescript/no-unsafe-member-access */ /* eslint-disable typescript/no-explicit-any */ - +import { HttpClient } from '@angular/common/http'; import { Observable, of } from 'rxjs'; /** * A Mock for the angular http-client. Is needed for testing crud inside a ngx-mat-entity-table. */ -export class HttpClientMock { +export class HttpClientMock implements Pick { exampleData: any[]; - constructor(exampleData: any[]) { - this.exampleData = exampleData; - } - post(url: string, body: any): Observable { - body.id = '1'; - this.exampleData.push(body); - return of(body); - } - get(url: string): Observable { + + get: HttpClient['get'] = jest.fn((url) => { if (url.charAt(url.length - 2) == '/') { return of(this.exampleData[0]); } return of(this.exampleData); - } - patch(url: string, body: any): Observable { + }); + + post: HttpClient['post'] = jest.fn((url, body) => { + body.id = '1'; + this.exampleData.push(body); + return of(body); + }); + + patch: HttpClient['patch'] = jest.fn((url, body) => { const id: string = this.getIdFromUrl(url); const res: any = this.exampleData[this.exampleData.findIndex((e) => e.id === id)]; for (const key in body) { @@ -33,10 +33,15 @@ export class HttpClientMock { } } return of(res); - } - delete(url: string): Observable { + }); + + delete: (url: string) => Observable = jest.fn((url) => { this.exampleData.splice(this.exampleData.findIndex((e) => e.id === this.getIdFromUrl(url)), 1); return of(undefined); + }); + + constructor(exampleData: any[]) { + this.exampleData = exampleData; } /** diff --git a/projects/ngx-material-entity/src/services/entity.service.test.ts b/projects/ngx-material-entity/src/services/entity.service.test.ts index f05a368..66d3c6f 100644 --- a/projects/ngx-material-entity/src/services/entity.service.test.ts +++ b/projects/ngx-material-entity/src/services/entity.service.test.ts @@ -37,7 +37,8 @@ const simpleTestEntityApiData: SimpleTestEntity[] = []; const testEntityApiData: TestEntityWithoutCustomProperties[] = []; test('should request TestEntities', async () => { - const service: SimpleTestEntityService = new SimpleTestEntityService(new HttpClientMock(simpleTestEntityApiData) as unknown as HttpClient, mockInjector); + const http: HttpClientMock = new HttpClientMock(simpleTestEntityApiData); + const service: SimpleTestEntityService = new SimpleTestEntityService(http as unknown as HttpClient, mockInjector); const simpleTestEntity: SimpleTestEntity = new SimpleTestEntity({ id: '1', name: 'John Smith' }); expect(service.baseUrl).toBe('http://api/test'); expect(service.entities).toEqual([]); @@ -57,6 +58,9 @@ test('should request TestEntities', async () => { await service.create(new SimpleTestEntity({ id: '2', name: 'Jane Smith' })); const entitiesFoundByRead: SimpleTestEntity[] = await service.read(); expect(entitiesFoundByRead.length).toBe(2); + // should hit cache on second read + await service.read(); + expect(http.get).toHaveBeenCalledTimes(1); // findById const findByIdService: SimpleTestEntityService = new SimpleTestEntityService(new HttpClientMock([]) as unknown as HttpClient, mockInjector); await findByIdService.create(new SimpleTestEntity({ id: '1', name: 'John Smith' })); diff --git a/projects/ngx-material-entity/src/services/entity.service.ts b/projects/ngx-material-entity/src/services/entity.service.ts index 2450f9a..c49e56f 100644 --- a/projects/ngx-material-entity/src/services/entity.service.ts +++ b/projects/ngx-material-entity/src/services/entity.service.ts @@ -188,6 +188,9 @@ export abstract class EntityService { + if (this.lastRead != undefined && (Date.now() - this.lastRead.getTime()) <= this.READ_EXPIRATION_IN_MS) { + return this.entitiesSubject.value; + } const e: EntityType[] = await firstValueFrom(this.http.get(baseUrl)); this.entitiesSubject.next(e); this.lastRead = new Date();