Skip to content

Commit

Permalink
fix(redux): remove parameter from noPayload action functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerhahnekamp committed Apr 19, 2024
1 parent 12d0077 commit 0e83882
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
24 changes: 22 additions & 2 deletions libs/ngrx-toolkit/src/lib/with-redux.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
HttpTestingController,
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { Action } from 'ngrx-toolkit';

interface Flight {
id: number;
Expand Down Expand Up @@ -99,6 +98,27 @@ describe('with redux', () => {
});
});

it('should allow a noPayload action to call without parameters', () => {
const FlightsStore = signalStore(
withState({ flights: [] as Flight[] }),
withRedux({
actions: {
init: noPayload,
},
reducer() {},
effects() {
return {};
},
}),
);

const flightStore = TestBed.configureTestingModule({
providers: [FlightsStore],
}).inject(FlightsStore);

flightStore.init();
});

it('should allow multiple effects listening to the same action', () => {
const FlightsStore = signalStore(
withState({ flights: [] as Flight[], effect1: false, effect2: false }),
Expand Down Expand Up @@ -134,7 +154,7 @@ describe('with redux', () => {
providers: [FlightsStore],
}).inject(FlightsStore);

flightStore.init({});
flightStore.init();

expect(flightStore.effect1()).toBe(true);
expect(flightStore.effect2()).toBe(true);
Expand Down
12 changes: 8 additions & 4 deletions libs/ngrx-toolkit/src/lib/with-redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ type ActionFns = Record<string, ActionFn>;
export type ActionsFnSpecs = Record<string, Payload>;

type ActionFnCreator<Spec extends ActionsFnSpecs> = {
[ActionName in keyof Spec]: ((
payload: Spec[ActionName],
) => Spec[ActionName] & { type: ActionName }) & { type: ActionName & string };
[ActionName in keyof Spec]: (Record<never, never> extends Spec[ActionName]
? () => Spec[ActionName] & { type: ActionName }
: (
payload: Spec[ActionName],
) => Spec[ActionName] & { type: ActionName }) & {
type: ActionName & string;
};
};

type ActionFnPayload<Action> = Action extends (payload: infer Payload) => void
Expand Down Expand Up @@ -262,7 +266,7 @@ export function withRedux<
EmptyFeatureResult & { methods: PublicStoreActionFns }
> {
return (store) => {
const { methods, subscriptions } = processRedux<Spec, PublicStoreActionFns>(
const { methods } = processRedux<Spec, PublicStoreActionFns>(
redux.actions,
redux.reducer as ReducerFactory<ActionFns, unknown>,
redux.effects as EffectsFactory<ActionFns>,
Expand Down

0 comments on commit 0e83882

Please sign in to comment.