Skip to content

Commit c140d27

Browse files
authored
Merge pull request #315 from Service-Soft/dev
Release 20.0.7
2 parents c67bab5 + b00e78b commit c140d27

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

projects/ngx-material-entity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-material-entity",
3-
"version": "20.0.6",
3+
"version": "20.0.7",
44
"license": "MIT",
55
"keywords": [
66
"angular",

projects/ngx-material-entity/src/mocks/http-client.mock.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22
/* eslint-disable typescript/no-unsafe-assignment */
33
/* eslint-disable typescript/no-unsafe-member-access */
44
/* eslint-disable typescript/no-explicit-any */
5-
5+
import { HttpClient } from '@angular/common/http';
66
import { Observable, of } from 'rxjs';
77

88
/**
99
* A Mock for the angular http-client. Is needed for testing crud inside a ngx-mat-entity-table.
1010
*/
11-
export class HttpClientMock {
11+
export class HttpClientMock implements Pick<HttpClient, 'get' | 'post' | 'patch'> {
1212
exampleData: any[];
13-
constructor(exampleData: any[]) {
14-
this.exampleData = exampleData;
15-
}
16-
post(url: string, body: any): Observable<any> {
17-
body.id = '1';
18-
this.exampleData.push(body);
19-
return of(body);
20-
}
21-
get(url: string): Observable<any> {
13+
14+
get: HttpClient['get'] = jest.fn((url) => {
2215
if (url.charAt(url.length - 2) == '/') {
2316
return of(this.exampleData[0]);
2417
}
2518
return of(this.exampleData);
26-
}
27-
patch(url: string, body: any): Observable<any> {
19+
});
20+
21+
post: HttpClient['post'] = jest.fn((url, body) => {
22+
body.id = '1';
23+
this.exampleData.push(body);
24+
return of(body);
25+
});
26+
27+
patch: HttpClient['patch'] = jest.fn((url, body) => {
2828
const id: string = this.getIdFromUrl(url);
2929
const res: any = this.exampleData[this.exampleData.findIndex((e) => e.id === id)];
3030
for (const key in body) {
@@ -33,10 +33,15 @@ export class HttpClientMock {
3333
}
3434
}
3535
return of(res);
36-
}
37-
delete(url: string): Observable<any> {
36+
});
37+
38+
delete: (url: string) => Observable<undefined> = jest.fn((url) => {
3839
this.exampleData.splice(this.exampleData.findIndex((e) => e.id === this.getIdFromUrl(url)), 1);
3940
return of(undefined);
41+
});
42+
43+
constructor(exampleData: any[]) {
44+
this.exampleData = exampleData;
4045
}
4146

4247
/**

projects/ngx-material-entity/src/services/entity.service.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const simpleTestEntityApiData: SimpleTestEntity[] = [];
3737
const testEntityApiData: TestEntityWithoutCustomProperties[] = [];
3838

3939
test('should request TestEntities', async () => {
40-
const service: SimpleTestEntityService = new SimpleTestEntityService(new HttpClientMock(simpleTestEntityApiData) as unknown as HttpClient, mockInjector);
40+
const http: HttpClientMock = new HttpClientMock(simpleTestEntityApiData);
41+
const service: SimpleTestEntityService = new SimpleTestEntityService(http as unknown as HttpClient, mockInjector);
4142
const simpleTestEntity: SimpleTestEntity = new SimpleTestEntity({ id: '1', name: 'John Smith' });
4243
expect(service.baseUrl).toBe('http://api/test');
4344
expect(service.entities).toEqual([]);
@@ -57,6 +58,9 @@ test('should request TestEntities', async () => {
5758
await service.create(new SimpleTestEntity({ id: '2', name: 'Jane Smith' }));
5859
const entitiesFoundByRead: SimpleTestEntity[] = await service.read();
5960
expect(entitiesFoundByRead.length).toBe(2);
61+
// should hit cache on second read
62+
await service.read();
63+
expect(http.get).toHaveBeenCalledTimes(1);
6064
// findById
6165
const findByIdService: SimpleTestEntityService = new SimpleTestEntityService(new HttpClientMock([]) as unknown as HttpClient, mockInjector);
6266
await findByIdService.create(new SimpleTestEntity({ id: '1', name: 'John Smith' }));

projects/ngx-material-entity/src/services/entity.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ export abstract class EntityService<EntityType extends BaseEntityType<EntityType
188188
* @returns A Promise of all received Entities.
189189
*/
190190
async read(baseUrl = this.baseUrl): Promise<EntityType[]> {
191+
if (this.lastRead != undefined && (Date.now() - this.lastRead.getTime()) <= this.READ_EXPIRATION_IN_MS) {
192+
return this.entitiesSubject.value;
193+
}
191194
const e: EntityType[] = await firstValueFrom(this.http.get<EntityType[]>(baseUrl));
192195
this.entitiesSubject.next(e);
193196
this.lastRead = new Date();

0 commit comments

Comments
 (0)