Skip to content

Commit

Permalink
adapt mockEventStore to new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Aribart committed Nov 17, 2023
1 parent 6ca6c4b commit b2a6d5c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
8 changes: 7 additions & 1 deletion packages/lib-test-tools/src/mockEventStore.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
import type {
EventStore,
EventStoreId,
EventStoreEventTypes,
EventStoreEventDetails,
EventStoreReducer,
EventStoreReducers,
EventStoreCurrentReducerVersion,
EventStoreAggregate,
} from '@castore/core';

Expand All @@ -17,6 +19,8 @@ export const mockEventStore = <EVENT_STORE extends EventStore = EventStore>(
EventStoreEventTypes<EVENT_STORE>,
EventStoreEventDetails<EVENT_STORE>,
EventStoreEventDetails<EVENT_STORE>,
EventStoreReducers<EVENT_STORE>,
EventStoreCurrentReducerVersion<EVENT_STORE>,
EventStoreReducer<EVENT_STORE>,
EventStoreAggregate<EVENT_STORE>
> =>
Expand All @@ -25,6 +29,8 @@ export const mockEventStore = <EVENT_STORE extends EventStore = EventStore>(
EventStoreEventTypes<EVENT_STORE>,
EventStoreEventDetails<EVENT_STORE>,
EventStoreEventDetails<EVENT_STORE>,
EventStoreReducers<EVENT_STORE>,
EventStoreCurrentReducerVersion<EVENT_STORE>,
EventStoreReducer<EVENT_STORE>,
EventStoreAggregate<EVENT_STORE>
>({ eventStore, initialEvents });
54 changes: 42 additions & 12 deletions packages/lib-test-tools/src/mockedEventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,33 @@ import { InMemoryEventStorageAdapter } from '@castore/event-storage-adapter-in-m
export class MockedEventStore<
EVENT_STORE_ID extends string = string,
EVENT_TYPES extends EventType[] = EventType[],
EVENT_DETAIL extends EventDetail = EventTypeDetails<EVENT_TYPES>,
$EVENT_DETAIL extends EventDetail = $Contravariant<EVENT_DETAIL, EventDetail>,
REDUCER extends Reducer<Aggregate, $EVENT_DETAIL> = Reducer<
Aggregate,
$EVENT_DETAIL
EVENT_DETAILS extends EventDetail = EventTypeDetails<EVENT_TYPES>,
$EVENT_DETAILS extends EventDetail = $Contravariant<
EVENT_DETAILS,
EventDetail
>,
REDUCERS extends Record<string, Reducer<Aggregate, $EVENT_DETAILS>> = Record<
string,
Reducer<Aggregate, $EVENT_DETAILS>
>,
CURRENT_REDUCER_VERSION extends keyof REDUCERS & string = keyof REDUCERS &
string,
REDUCER extends Reducer<
Aggregate,
$EVENT_DETAILS
> = REDUCERS[CURRENT_REDUCER_VERSION],
AGGREGATE extends Aggregate = ReturnType<REDUCER>,
> extends EventStore<
EVENT_STORE_ID,
EVENT_TYPES,
EVENT_DETAIL,
$EVENT_DETAIL,
EVENT_DETAILS,
$EVENT_DETAILS,
REDUCERS,
CURRENT_REDUCER_VERSION,
REDUCER,
AGGREGATE
> {
initialEvents: EVENT_DETAIL[];
initialEvents: EVENT_DETAILS[];
reset: () => void;

constructor({
Expand All @@ -37,17 +48,36 @@ export class MockedEventStore<
eventStore: EventStore<
EVENT_STORE_ID,
EVENT_TYPES,
EVENT_DETAIL,
$EVENT_DETAIL,
EVENT_DETAILS,
$EVENT_DETAILS,
REDUCERS,
CURRENT_REDUCER_VERSION,
REDUCER,
AGGREGATE
>;
initialEvents?: EVENT_DETAIL[];
initialEvents?: EVENT_DETAILS[];
}) {
super({
eventStoreId: eventStore.eventStoreId,
eventTypes: eventStore.eventTypes,
reducer: eventStore.reducer,
...(eventStore.snapshotMode === 'custom'
? {
snapshotMode: 'custom',
reducers: eventStore.reducers,
currentReducerVersion: eventStore.currentReducerVersion,
}
: eventStore.snapshotMode === 'auto'
? {
snapshotMode: 'auto',
autoSnapshotPeriodVersions:
eventStore.autoSnapshotPeriodVersions as number,
currentReducerVersion: eventStore.currentReducerVersion,
reducer: eventStore.reducer,
}
: {
snapshotMode: 'none',
reducer: eventStore.reducer,
}),
simulateSideEffect: eventStore.simulateSideEffect,
eventStorageAdapter: new InMemoryEventStorageAdapter({ initialEvents }),
});
Expand Down

0 comments on commit b2a6d5c

Please sign in to comment.