Fixtures
Fixtures give context to states that you have not recorded yet, such as an error after a slow request.
Define fixtures
Put them in a file of their own, and give <Kotick /> a loader for that file. Each adapter exports its own defineFixtures:
defineFixtures from kotick/reducer takes a state object for each state that you list. A fixture is a whole state of its own name, so its discriminant is the name of its key:
searchMachine is the machine of From a switch reducer.
defineFixtures from kotick/xstate takes a context for each leaf that you list:
checkoutMachine is the machine of the checkout demo. To see it, open machine.ts in the Code tab of the checkout example.
The loader has a development condition. The condition removes the fixtures, and the import, from production builds. Use the condition of your bundler.
In Vite:
In Next.js and other bundlers:
Do not use process.env.NODE_ENV in Vite. A default Vite app does not have the Node.js types, thus TypeScript does not know process. In Next.js, put 'use client' at the top of the file. Refer to Next.js.
The examples import from src through the @/ alias. Next.js sets this alias, but Vite does not. In Vite, add the alias to paths in your TypeScript config and to resolve.alias in vite.config.ts, or use relative imports.
How fixtures apply
These rules apply to both adapters:
- A fixture set applies to each instance of its machine.
- The loader runs after the page loads.
- You can give an array of sets in place of a loader. Then the sets are in your main bundle, also in production. Use a loader with a development check to keep them out.
kotick/reducerandkotick/xstateboth exportdefineFixtures. A file with sets for both adapters imports one of them with another name, for exampleimport { defineFixtures as defineReducerFixtures } from 'kotick/reducer'.
Each adapter adds rules of its own:
- The keys are optional, so a set can start with the one state that the app cannot get to.
satisfies CompleteFixtures<typeof searchMachine>makes the file fail when the machine has a state without a fixture. - TypeScript checks each fixture against the state of its key. Without TypeScript,
defineFixturesleaves out a fixture of another state and a key that is not a state. In development, it logs a warning for each. - Copy context in the panel copies the state object of the shown state as JSON. The JSON is a fixture as it is.
- Until the loader finishes, a state without a recording stays greyed out in the panel. Its row enables when the set arrives.
- The keys are leaf ids. TypeScript checks them against the states of the machine, and
contextagainst the context type of the machine. - A fixture set also applies to copies from
machine.provide(), and to machines thatdebug.idrenames. - A preview that started before the loader finished changes to the fixture when it arrives. Such a preview logs the warning about the live context only if the loader gives no fixture for it.
- In a parallel preview, the first leaf that has a fixture gives the context: the leaves you selected, in sequence, then the leaves of the other regions.
- Copy context in the panel copies the context of the shown state as JSON. The JSON is the value of
contextin a fixture.