Skip to content

Commit

Permalink
test(dataService): make flight factory function
Browse files Browse the repository at this point in the history
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>): Flight. As you see, you then only need to provide those values of Flight which differ from the default one.

Change:

Made same function.
  • Loading branch information
michael-small committed Sep 23, 2024
1 parent 11e181e commit c502361
Showing 1 changed file with 24 additions and 86 deletions.
110 changes: 24 additions & 86 deletions libs/ngrx-toolkit/src/lib/with-data-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,22 @@ describe('withDataService', () => {
// TODO 3B: setting error state (with named collection)
});

// Test helpers
let currentFlightId = 0;
const createFlight = (flight: Partial<Flight> = {}) => ({
...{
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;
Expand All @@ -716,22 +732,7 @@ class MockFlightService implements DataService<Flight, FlightFilter> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
updateAll(entity: Flight[]): Promise<Flight[]> {
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})])
);
}

Expand All @@ -744,35 +745,15 @@ class MockFlightService implements DataService<Flight, FlightFilter> {
}

private find(from: string, to: string, urgent = false): Observable<Flight[]> {
return of([
{
id: 1,
from: 'Paris',
to: 'New York',
date: new Date().toDateString(),
delayed: false,
},
]);
return of([createFlight()]);
}

private findById(id: string): Observable<Flight> {
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<Flight> {
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<void> {
Expand Down Expand Up @@ -800,22 +781,7 @@ class MockFlightServiceForLoading implements DataService<Flight, FlightFilter> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
updateAll(entity: Flight[]): Promise<Flight[]> {
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))
);
}

Expand All @@ -828,50 +794,22 @@ class MockFlightServiceForLoading implements DataService<Flight, FlightFilter> {
}

private find(from: string, to: string, urgent = false): Observable<Flight[]> {
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<Flight> {
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<Flight> {
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<void> {
return of(undefined).pipe(delay(3));
}
}

type Flight = {
id: number;
from: string;
to: string;
date: string;
delayed: boolean;
};

const Store = signalStore(
withCallState(),
withEntities<Flight>(),
Expand Down

0 comments on commit c502361

Please sign in to comment.