mockAction
Stable · since 0.1.0
A method on the TestRuntime returned by createTestRuntime. Registers an action handler for a trigger. The test-time equivalent of useAction — same registration mechanism, no React.
The handler is typically a vi.fn() or jest.fn() so you can assert call counts and arguments directly.
Import
Section titled “Import”import { createTestRuntime } from '@triggery/testing';
const rt = createTestRuntime();
rt.mockAction(/* ... */);Signature
Section titled “Signature”mockAction<S extends TriggerSchema, K extends ActionKey<S>>(
trigger: Trigger<S>,
name: K,
handler: ActionFn<ActionMap<S>[K]>,
): RegistrationToken;
type ActionFn<P> = [P] extends [void] ? () => void : (payload: P) => void;Parameters
Section titled “Parameters”| Param | Type | Description |
|---|---|---|
trigger | Trigger<S> | The trigger whose action you’re stubbing. |
name | K extends ActionKey<S> | An action declared in the trigger’s schema. |
handler | ActionFn<P> | The mock implementation. Void-payload actions take () => void. |
Returns
Section titled “Returns”A RegistrationToken with { unregister(): void }.
Examples
Section titled “Examples”Assert call args
Section titled “Assert call args”import { createTrigger function createTrigger<S extends TriggerSchema>(config: CreateTriggerConfig<S>, runtime?: Runtime): Trigger<S>Create a trigger and register it in a runtime (the default runtime if none is passed). } from '@triggery/core';
import { createTestRuntime function createTestRuntime(options?: TestRuntimeOptions): TestRuntime } from '@triggery/testing';
import { expect const expect: ExpectStatic , it const it: TestAPIDefines a test case with a given name and test function. The test function can optionally be configured with test options. , vi const vi: VitestUtils } from 'vitest';
it it<object>(name: string | Function, fn?: TestFunction<object> | undefined, options?: number): void (+1 overload)Defines a test case with a given name and test function. The test function can optionally be configured with test options. ('calls showToast with the right payload', () => {
const rt const rt: TestRuntime = createTestRuntime function createTestRuntime(options?: TestRuntimeOptions): TestRuntime ();
const t const t: Trigger<{
events: {
"new-message": {
author: string;
text: string;
};
};
actions: {
showToast: {
title: string;
};
};
}>
= createTrigger createTrigger<{
events: {
"new-message": {
author: string;
text: string;
};
};
actions: {
showToast: {
title: string;
};
};
}>(config: CreateTriggerConfig<{
events: {
"new-message": {
author: string;
text: string;
};
};
actions: {
showToast: {
title: string;
};
};
}>, runtime?: Runtime): Trigger<{
events: {
"new-message": {
author: string;
text: string;
};
};
actions: {
showToast: {
title: string;
};
};
}>
Create a trigger and register it in a runtime (the default runtime if none is passed). <{
events events: {
'new-message': {
author: string;
text: string;
};
}
: { 'new-message': { author author: string : string; text text: string : string } };
actions actions: {
showToast: {
title: string;
};
}
: { showToast showToast: {
title: string;
}
: { title title: string : string } };
}>(
{
id id: string : 'demo',
events events: readonly "new-message"[] : ['new-message'],
handler handler: TriggerHandler<{
events: {
"new-message": {
author: string;
text: string;
};
};
actions: {
showToast: {
title: string;
};
};
}, never>
({ event event: {
readonly name: "new-message";
readonly payload: {
author: string;
text: string;
};
}
, actions actions: ActionsCtx<{
showToast: {
title: string;
};
}>
}) {
actions actions: ActionsCtx<{
showToast: {
title: string;
};
}>
.showToast showToast?: ((payload: {
title: string;
}) => void) | undefined
?.({ title title: string : event event: {
readonly name: "new-message";
readonly payload: {
author: string;
text: string;
};
}
.payload payload: {
author: string;
text: string;
}
.author author: string });
},
},
rt const rt: TestRuntime ,
);
const showToast const showToast: Mock<Procedure> = vi const vi: VitestUtils .fn VitestUtils.fn: <Procedure>(originalImplementation?: Procedure | undefined) => Mock<Procedure>Creates a spy on a function, though can be initiated without one. Every time a function is invoked, it stores its call arguments, returns, and instances. Also, you can manipulate its behavior with [methods](https://vitest.dev/api/mock).
If no function is given, mock will return `undefined`, when invoked. ();
rt const rt: TestRuntime .mockAction mockAction<{
events: {
"new-message": {
author: string;
text: string;
};
};
actions: {
showToast: {
title: string;
};
};
}, "showToast">(trigger: Trigger<{
events: {
"new-message": {
author: string;
text: string;
};
};
actions: {
showToast: {
title: string;
};
};
}>, name: "showToast", handler: (payload: {
title: string;
}) => void): RegistrationToken
Register an action handler — typically a `vi.fn()`. (t const t: Trigger<{
events: {
"new-message": {
author: string;
text: string;
};
};
actions: {
showToast: {
title: string;
};
};
}>
, 'showToast', showToast const showToast: Mock<Procedure> );
rt const rt: TestRuntime .fireSync function fireSync(eventName: string, payload?: unknown): voidRun dispatch synchronously (for tests and benchmarks). ('new-message', { author author: string : 'Alice', text text: string : 'hi' });
expect expect<Mock<Procedure>>(actual: Mock<Procedure>, message?: string): Assertion<Mock<Procedure>> (+1 overload) (showToast const showToast: Mock<Procedure> ).toHaveBeenCalledExactlyOnceWith Assertion<Mock<Procedure>>.toHaveBeenCalledExactlyOnceWith: <[{
title: string;
}]>(args_0: {
title: string;
}) => void
Ensure that a mock function is called with specific arguments and called
exactly once. ({ title title: string : 'Alice' });
});Void payload
Section titled “Void payload”import { createTrigger function createTrigger<S extends TriggerSchema>(config: CreateTriggerConfig<S>, runtime?: Runtime): Trigger<S>Create a trigger and register it in a runtime (the default runtime if none is passed). } from '@triggery/core';
import { createTestRuntime function createTestRuntime(options?: TestRuntimeOptions): TestRuntime } from '@triggery/testing';
import { vi const vi: VitestUtils } from 'vitest';
const rt const rt: TestRuntime = createTestRuntime function createTestRuntime(options?: TestRuntimeOptions): TestRuntime ();
const t const t: Trigger<{
events: {
close: void;
};
actions: {
onClose: void;
};
}>
= createTrigger createTrigger<{
events: {
close: void;
};
actions: {
onClose: void;
};
}>(config: CreateTriggerConfig<{
events: {
close: void;
};
actions: {
onClose: void;
};
}>, runtime?: Runtime): Trigger<{
events: {
close: void;
};
actions: {
onClose: void;
};
}>
Create a trigger and register it in a runtime (the default runtime if none is passed). <{
events events: {
close: void;
}
: { close close: void : void };
actions actions: {
onClose: void;
}
: { onClose onClose: void : void };
}>(
{
id id: string : 'demo',
events events: readonly "close"[] : ['close'],
handler handler: TriggerHandler<{
events: {
close: void;
};
actions: {
onClose: void;
};
}, never>
({ actions actions: ActionsCtx<{
onClose: void;
}>
}) {
actions actions: ActionsCtx<{
onClose: void;
}>
.onClose onClose?: (() => void) | undefined ?.();
},
},
rt const rt: TestRuntime ,
);
const onClose const onClose: Mock<Procedure> = vi const vi: VitestUtils .fn VitestUtils.fn: <Procedure>(originalImplementation?: Procedure | undefined) => Mock<Procedure>Creates a spy on a function, though can be initiated without one. Every time a function is invoked, it stores its call arguments, returns, and instances. Also, you can manipulate its behavior with [methods](https://vitest.dev/api/mock).
If no function is given, mock will return `undefined`, when invoked. ();
rt const rt: TestRuntime .mockAction mockAction<{
events: {
close: void;
};
actions: {
onClose: void;
};
}, "onClose">(trigger: Trigger<{
events: {
close: void;
};
actions: {
onClose: void;
};
}>, name: "onClose", handler: () => void): RegistrationToken
Register an action handler — typically a `vi.fn()`. (t const t: Trigger<{
events: {
close: void;
};
actions: {
onClose: void;
};
}>
, 'onClose', onClose const onClose: Mock<Procedure> );
rt const rt: TestRuntime .fireSync function fireSync(eventName: string, payload?: unknown): voidRun dispatch synchronously (for tests and benchmarks). ('close');Async handler
Section titled “Async handler”import { createTrigger function createTrigger<S extends TriggerSchema>(config: CreateTriggerConfig<S>, runtime?: Runtime): Trigger<S>Create a trigger and register it in a runtime (the default runtime if none is passed). } from '@triggery/core';
import { createTestRuntime function createTestRuntime(options?: TestRuntimeOptions): TestRuntime } from '@triggery/testing';
import { vi const vi: VitestUtils } from 'vitest';
const rt const rt: TestRuntime = createTestRuntime function createTestRuntime(options?: TestRuntimeOptions): TestRuntime ();
const t const t: Trigger<{
events: {
save: {
id: string;
};
};
actions: {
persist: {
id: string;
};
};
}>
= createTrigger createTrigger<{
events: {
save: {
id: string;
};
};
actions: {
persist: {
id: string;
};
};
}>(config: CreateTriggerConfig<{
events: {
save: {
id: string;
};
};
actions: {
persist: {
id: string;
};
};
}>, runtime?: Runtime): Trigger<{
events: {
save: {
id: string;
};
};
actions: {
persist: {
id: string;
};
};
}>
Create a trigger and register it in a runtime (the default runtime if none is passed). <{
events events: {
save: {
id: string;
};
}
: { save save: {
id: string;
}
: { id id: string : string } };
actions actions: {
persist: {
id: string;
};
}
: { persist persist: {
id: string;
}
: { id id: string : string } };
}>(
{
id id: string : 'demo',
events events: readonly "save"[] : ['save'],
async handler handler: TriggerHandler<{
events: {
save: {
id: string;
};
};
actions: {
persist: {
id: string;
};
};
}, never>
({ event event: {
readonly name: "save";
readonly payload: {
id: string;
};
}
, actions actions: ActionsCtx<{
persist: {
id: string;
};
}>
}) {
await actions actions: ActionsCtx<{
persist: {
id: string;
};
}>
.persist persist?: ((payload: {
id: string;
}) => void) | undefined
?.(event event: {
readonly name: "save";
readonly payload: {
id: string;
};
}
.payload payload: {
id: string;
}
);
},
},
rt const rt: TestRuntime ,
);
const persist const persist: Mock<(_payload: {
id: string;
}) => Promise<void>>
= vi const vi: VitestUtils .fn VitestUtils.fn: <(_payload: {
id: string;
}) => Promise<void>>(originalImplementation?: ((_payload: {
id: string;
}) => Promise<void>) | undefined) => Mock<(_payload: {
id: string;
}) => Promise<void>>
Creates a spy on a function, though can be initiated without one. Every time a function is invoked, it stores its call arguments, returns, and instances. Also, you can manipulate its behavior with [methods](https://vitest.dev/api/mock).
If no function is given, mock will return `undefined`, when invoked. (async (_payload _payload: {
id: string;
}
: { id id: string : string }) => {
await Promise var Promise: PromiseConstructorRepresents the completion of an asynchronous operation .resolve PromiseConstructor.resolve(): Promise<void> (+2 overloads)Creates a new resolved promise. ();
});
rt const rt: TestRuntime .mockAction mockAction<{
events: {
save: {
id: string;
};
};
actions: {
persist: {
id: string;
};
};
}, "persist">(trigger: Trigger<{
events: {
save: {
id: string;
};
};
actions: {
persist: {
id: string;
};
};
}>, name: "persist", handler: (payload: {
id: string;
}) => void): RegistrationToken
Register an action handler — typically a `vi.fn()`. (t const t: Trigger<{
events: {
save: {
id: string;
};
};
actions: {
persist: {
id: string;
};
};
}>
, 'persist', persist const persist: Mock<(_payload: {
id: string;
}) => Promise<void>>
);Per-test cleanup with unregister
Section titled “Per-test cleanup with unregister”import { createTrigger function createTrigger<S extends TriggerSchema>(config: CreateTriggerConfig<S>, runtime?: Runtime): Trigger<S>Create a trigger and register it in a runtime (the default runtime if none is passed). } from '@triggery/core';
import { createTestRuntime function createTestRuntime(options?: TestRuntimeOptions): TestRuntime } from '@triggery/testing';
const rt const rt: TestRuntime = createTestRuntime function createTestRuntime(options?: TestRuntimeOptions): TestRuntime ();
const t const t: Trigger<{
events: {
ping: void;
};
actions: {
log: void;
};
}>
= createTrigger createTrigger<{
events: {
ping: void;
};
actions: {
log: void;
};
}>(config: CreateTriggerConfig<{
events: {
ping: void;
};
actions: {
log: void;
};
}>, runtime?: Runtime): Trigger<{
events: {
ping: void;
};
actions: {
log: void;
};
}>
Create a trigger and register it in a runtime (the default runtime if none is passed). <{
events events: {
ping: void;
}
: { ping ping: void : void };
actions actions: {
log: void;
}
: { log log: void : void };
}>(
{ id id: string : 'demo', events events: readonly "ping"[] : ['ping'], handler handler: TriggerHandler<{
events: {
ping: void;
};
actions: {
log: void;
};
}, never>
({ actions actions: ActionsCtx<{
log: void;
}>
}) { actions actions: ActionsCtx<{
log: void;
}>
.log log?: (() => void) | undefined ?.(); } },
rt const rt: TestRuntime ,
);
const first const first: RegistrationToken = rt const rt: TestRuntime .mockAction mockAction<{
events: {
ping: void;
};
actions: {
log: void;
};
}, "log">(trigger: Trigger<{
events: {
ping: void;
};
actions: {
log: void;
};
}>, name: "log", handler: () => void): RegistrationToken
Register an action handler — typically a `vi.fn()`. (t const t: Trigger<{
events: {
ping: void;
};
actions: {
log: void;
};
}>
, 'log', () => console var console: Console .log Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static) ('first'));
rt const rt: TestRuntime .fireSync function fireSync(eventName: string, payload?: unknown): voidRun dispatch synchronously (for tests and benchmarks). ('ping'); // "first"
first const first: RegistrationToken .unregister function unregister(): voidIdempotent unregister. ();
rt const rt: TestRuntime .mockAction mockAction<{
events: {
ping: void;
};
actions: {
log: void;
};
}, "log">(trigger: Trigger<{
events: {
ping: void;
};
actions: {
log: void;
};
}>, name: "log", handler: () => void): RegistrationToken
Register an action handler — typically a `vi.fn()`. (t const t: Trigger<{
events: {
ping: void;
};
actions: {
log: void;
};
}>
, 'log', () => console var console: Console .log Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static) ('second'));
rt const rt: TestRuntime .fireSync function fireSync(eventName: string, payload?: unknown): voidRun dispatch synchronously (for tests and benchmarks). ('ping'); // "second"Related
Section titled “Related” createTestRuntime The runtime that exposes this method.
mockCondition Stub a condition — the other half of test wiring.
useAction Runtime equivalent in React.
Testing guide Trigger-first test patterns.