From c502361a204b23417eadaee8ce5a585c98b7c902 Mon Sep 17 00:00:00 2001 From: Michael Small Date: Mon, 23 Sep 2024 18:52:09 -0500 Subject: [PATCH] test(dataService): make flight factory function PR comment: I'd create a factory function which generates you a flight. That function should use by a default an default flight object and increments the id automatically with every call. Its signature could be createFlight(flight: Partial): Flight. As you see, you then only need to provide those values of Flight which differ from the default one. Change: Made same function. --- .../src/lib/with-data-service.spec.ts | 110 ++++-------------- 1 file changed, 24 insertions(+), 86 deletions(-) diff --git a/libs/ngrx-toolkit/src/lib/with-data-service.spec.ts b/libs/ngrx-toolkit/src/lib/with-data-service.spec.ts index 9ec9cfe..2ba3b2e 100644 --- a/libs/ngrx-toolkit/src/lib/with-data-service.spec.ts +++ b/libs/ngrx-toolkit/src/lib/with-data-service.spec.ts @@ -691,6 +691,22 @@ describe('withDataService', () => { // TODO 3B: setting error state (with named collection) }); +// Test helpers +let currentFlightId = 0; +const createFlight = (flight: Partial = {}) => ({ + ...{ + id: ++currentFlightId, from: 'Paris', to: 'New York', date: new Date().toDateString(), delayed: false, + }, + ...flight +}); +type Flight = { + id: number; + from: string; + to: string; + date: string; + delayed: boolean; +}; + type FlightFilter = { from: string; to: string; @@ -716,22 +732,7 @@ class MockFlightService implements DataService { // eslint-disable-next-line @typescript-eslint/no-unused-vars updateAll(entity: Flight[]): Promise { return firstValueFrom( - of([ - { - id: 3, - from: 'Paris', - to: 'New York', - date: new Date().toDateString(), - delayed: false, - }, - { - id: 4, - from: 'Paris', - to: 'New York', - date: new Date().toDateString(), - delayed: false, - }, - ]) + of([createFlight({id: 3}), createFlight({id: 4})]) ); } @@ -744,35 +745,15 @@ class MockFlightService implements DataService { } private find(from: string, to: string, urgent = false): Observable { - return of([ - { - id: 1, - from: 'Paris', - to: 'New York', - date: new Date().toDateString(), - delayed: false, - }, - ]); + return of([createFlight()]); } private findById(id: string): Observable { - return of({ - id: 2, - from: 'Paris', - to: 'New York', - date: new Date().toDateString(), - delayed: false, - }); + return of(createFlight({id: 2})); } private save(flight: Flight): Observable { - return of({ - id: 3, - from: 'Paris', - to: 'New York', - date: new Date().toDateString(), - delayed: false, - }); + return of(createFlight({id: 3})); } private remove(flight: Flight): Observable { @@ -800,22 +781,7 @@ class MockFlightServiceForLoading implements DataService { // eslint-disable-next-line @typescript-eslint/no-unused-vars updateAll(entity: Flight[]): Promise { return firstValueFrom( - of([ - { - id: 3, - from: 'Paris', - to: 'New York', - date: new Date().toDateString(), - delayed: false, - }, - { - id: 4, - from: 'Paris', - to: 'New York', - date: new Date().toDateString(), - delayed: false, - }, - ]).pipe(delay(3)) + of([createFlight({id: 3}), createFlight({id: 4})]).pipe(delay(3)) ); } @@ -828,35 +794,15 @@ class MockFlightServiceForLoading implements DataService { } private find(from: string, to: string, urgent = false): Observable { - return of([ - { - id: 1, - from: 'Paris', - to: 'New York', - date: new Date().toDateString(), - delayed: false, - }, - ]).pipe(delay(3)); + return of([createFlight({id: 1})]).pipe(delay(3)); } private findById(id: string): Observable { - return of({ - id: 2, - from: 'Paris', - to: 'New York', - date: new Date().toDateString(), - delayed: false, - }).pipe(delay(3)); + return of(createFlight({id: 2})).pipe(delay(3)); } private save(flight: Flight): Observable { - return of({ - id: 3, - from: 'Paris', - to: 'New York', - date: new Date().toDateString(), - delayed: false, - }).pipe(delay(3)); + return of(createFlight({id: 3})).pipe(delay(3)); } private remove(flight: Flight): Observable { @@ -864,14 +810,6 @@ class MockFlightServiceForLoading implements DataService { } } -type Flight = { - id: number; - from: string; - to: string; - date: string; - delayed: boolean; -}; - const Store = signalStore( withCallState(), withEntities(),