TestIntent
TestIntent inspects an intent descriptor’s configuration: which commands it references and what execution mode it uses. It answers the question “when this intent is dispatched, what will the store execute?”
import { TestIntent } from '@hurum/core/testing'import { PurchaseIntents } from './intents'
const intent = TestIntent(PurchaseIntents.submitClicked)TestIntent takes an intent descriptor (from your Intents() definition) and returns a read-only object with commands and mode.
commands
Section titled “commands”The list of commands the intent will execute:
const intent = TestIntent(PurchaseIntents.submitClicked)
expect(intent.commands).toEqual([ValidateCommand, SavePurchaseCommand])This verifies that the intent is wired to the correct commands. If you refactor an intent to add or remove a command, this test catches the change.
The execution mode — how the commands are run:
// Sequential (default): commands run one after anotherconst sequential = TestIntent(PurchaseIntents.submitClicked)expect(sequential.mode).toBe('sequential')
// All (fail-fast parallel): commands run concurrently, abort all on first failureconst failFast = TestIntent(PurchaseIntents.loadAll)expect(failFast.mode).toBe('all')
// AllSettled (independent parallel): commands run concurrently, all complete independentlyconst independent = TestIntent(PurchaseIntents.initialize)expect(independent.mode).toBe('allSettled')| Mode | Created with | Behavior |
|---|---|---|
sequential | Intent(cmd1, cmd2) | Run commands one after another |
all | Intent.all(cmd1, cmd2) | Run concurrently, fail-fast on error |
allSettled | Intent.allSettled(cmd1, cmd2) | Run concurrently, all complete independently |
Full example
Section titled “Full example”import { describe, it, expect } from 'vitest'import { TestIntent } from '@hurum/core/testing'import { PurchaseIntents } from './intents'import { ValidateCommand, SavePurchaseCommand, LoadPricesCommand, LoadInventoryCommand,} from './executors'
describe('PurchaseIntents', () => { it('submitClicked runs validation then save', () => { const intent = TestIntent(PurchaseIntents.submitClicked)
expect(intent.commands).toEqual([ValidateCommand, SavePurchaseCommand]) expect(intent.mode).toBe('sequential') })
it('initialize loads prices and inventory concurrently', () => { const intent = TestIntent(PurchaseIntents.initialize)
expect(intent.commands).toEqual([LoadPricesCommand, LoadInventoryCommand]) expect(intent.mode).toBe('allSettled') })
it('plusClicked runs a single command', () => { const intent = TestIntent(CounterIntents.plusClicked)
expect(intent.commands).toHaveLength(1) expect(intent.commands).toEqual([IncrementCommand]) expect(intent.mode).toBe('sequential') })})When to use TestIntent
Section titled “When to use TestIntent”TestIntent is a lightweight sanity check. Use it when:
- An intent wires multiple commands. Verify the commands are correct and in the right order.
- An intent uses parallel execution. Confirm the mode is
allorallSettled, not accidentallysequential. - You refactor command structure. A TestIntent test breaks immediately if a command is added, removed, or reordered.
For single-command intents with sequential mode, the test is simple but still valuable as documentation of intent:
expect(intent.commands).toEqual([IncrementCommand])expect(intent.mode).toBe('sequential')Next steps
Section titled “Next steps”- TestExecutor — Test the executors that commands reference
- TestStore — Integration test the full flow
- Testing Overview — All five utilities at a glance