Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion projects/ngx-material-entity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-material-entity",
"version": "20.0.6",
"version": "20.0.7",
"license": "MIT",
"keywords": [
"angular",
Expand Down
35 changes: 20 additions & 15 deletions projects/ngx-material-entity/src/mocks/http-client.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpClient, 'get' | 'post' | 'patch'> {
exampleData: any[];
constructor(exampleData: any[]) {
this.exampleData = exampleData;
}
post(url: string, body: any): Observable<any> {
body.id = '1';
this.exampleData.push(body);
return of(body);
}
get(url: string): Observable<any> {

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<any> {
});

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) {
Expand All @@ -33,10 +33,15 @@ export class HttpClientMock {
}
}
return of(res);
}
delete(url: string): Observable<any> {
});

delete: (url: string) => Observable<undefined> = 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand All @@ -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' }));
Expand Down
3 changes: 3 additions & 0 deletions projects/ngx-material-entity/src/services/entity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ export abstract class EntityService<EntityType extends BaseEntityType<EntityType
* @returns A Promise of all received Entities.
*/
async read(baseUrl = this.baseUrl): Promise<EntityType[]> {
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<EntityType[]>(baseUrl));
this.entitiesSubject.next(e);
this.lastRead = new Date();
Expand Down