Skip to content

Commit

Permalink
test: added tests for testing required payload
Browse files Browse the repository at this point in the history
  • Loading branch information
teneko committed Nov 25, 2021
1 parent 31146b7 commit 245b7de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ type TypesFromAction<V, P, T extends string, M extends PromiseActionsFromMeta<V,
}
};

type TriggerActionCreator<V, P, T extends string, M extends PromiseActionsFromMeta<V, T>, TPAC extends PayloadActionCreator<any, any>> = ActionCreatorWithPreparedPayloadAndMeta<V, P, T, M, TPAC>;
type TriggerActionCreator<V, P, T extends string, M extends PromiseActionsFromMeta<V, T>, TPAC extends PayloadActionCreator<P, T>> = ActionCreatorWithPreparedPayloadAndMeta<V, P, T, M, TPAC>;

type SagaPromiseActionCreatorBase<V, P, T extends string, M extends PromiseActionsFromMeta<V, T>, TPAC extends PayloadActionCreator<any, any>> = TriggerActionCreator<V, P, T, M, TPAC> & {
type SagaPromiseActionCreatorBase<V, P, T extends string, M extends PromiseActionsFromMeta<V, T>, TPAC extends PayloadActionCreator<P, T>> = TriggerActionCreator<V, P, T, M, TPAC> & {
trigger: SagaPromiseActionCreatorBase<V, P, T, M, TPAC>
resolved: ActionCreatorWithPayload<V, T>;
rejected: ActionCreatorWithPayload<any, `${T}/rejected`>;
} & SagasFromAction<V, P, T> & TypesFromAction<V, P, T, M>;

export type SagaPromiseActionCreator<V, P, T extends string, TPAC extends PayloadActionCreator<any, any>> = SagaPromiseActionCreatorBase<V, P, T, SagaPromiseMeta<V, T>, TPAC>;
export type SagaPromiseActionCreator<V, P, T extends string, TPAC extends PayloadActionCreator<P, T>> = SagaPromiseActionCreatorBase<V, P, T, SagaPromiseMeta<V, T>, TPAC>;

function isTriggerAction(action: SagaPromiseAction<any, any, any>) {
return action?.meta?.promiseActions.resolved != null;
Expand Down Expand Up @@ -138,7 +138,7 @@ function createPromiseActions<V, T extends string>(type: T) {

type TriggerExecutor<RT> = (() => PromiseLike<RT> | RT | Iterator<any, RT, any>);

function wrapTriggerAction<V, P, T extends string, TPAC extends PayloadActionCreator<any, any>>(
function wrapTriggerAction<V, P, T extends string, TPAC extends PayloadActionCreator<P, T>>(
type: T,
triggerAction: TPAC,
): SagaPromiseActionCreator<V, P, T, TPAC> {
Expand Down
7 changes: 7 additions & 0 deletions test/index.types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ declare const typeOfResolvedActionThatGotCreatedFromTheSimpleOrAdvancedActionCre
declare const typeOfRejectedActionThatGotCreatedFromTheSimpleOrAdvancedActionCreator: typeof promiseAction.types.rejectedAction;
declare const typeOfPromiseThatGotCreatedOfPromiseMiddleware: typeof promiseAction.types.promise;
declare const typeOfResolvedValueFromPromiseThatGotCreatedOfPromiseMiddleware: typeof promiseAction.types.resolveValue;

interface Payload {
data: any
}

const mustExpectPayload = promiseActionFactory<number>().simple<Payload>("MY_ACTION")({ data: {} });
const mustExpectInferredPayload = promiseActionFactory<number>().advanced("MY_ACTION", (payload: Payload) => ({ payload }))({ data: {} });

0 comments on commit 245b7de

Please sign in to comment.