Skip to content

Commit

Permalink
Expose getInitialSnapshot (#4731)
Browse files Browse the repository at this point in the history
* Expose getInitialSnapshot

* Add more details to changeset

* Add words
  • Loading branch information
davidkpiano authored Feb 7, 2024
1 parent 54a2b01 commit 960cdcb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
16 changes: 16 additions & 0 deletions .changeset/clean-brooms-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'xstate': patch
---

You can now import `getInitialSnapshot(…)` from `xstate` directly, which is useful for getting a mock of the initial snapshot when interacting with machines (or other actor logic) without `createActor(…)`:

```ts
import { getInitialSnapshot } from 'xstate';
import { someMachine } from './someMachine';

// Returns the initial snapshot (state) of the machine
const initialSnapshot = getInitialSnapshot(
someMachine,
{ name: 'Mateusz' } // optional input
);
```
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export * from './typegenTypes.ts';
export * from './types.ts';
export { waitFor } from './waitFor.ts';
import { createMachine } from './createMachine.ts';
export { getNextSnapshot } from './getNextSnapshot.ts';
export { getNextSnapshot, getInitialSnapshot } from './getNextSnapshot.ts';
import { Actor, createActor, interpret, Interpreter } from './createActor.ts';
import { StateNode } from './StateNode.ts';
// TODO: decide from where those should be exported
Expand Down
10 changes: 7 additions & 3 deletions packages/core/test/deterministic.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { fromCallback, createActor, getNextSnapshot } from '../src/index.ts';
import { createMachine } from '../src/createMachine.ts';
import { getInitialSnapshot } from '../src/getNextSnapshot.ts';
import {
fromCallback,
createActor,
getNextSnapshot,
createMachine,
getInitialSnapshot
} from '../src/index.ts';

describe('deterministic machine', () => {
const lightMachine = createMachine({
Expand Down
5 changes: 2 additions & 3 deletions packages/core/test/getNextSnapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {
createActor,
createMachine,
fromTransition,
getNextSnapshot
getNextSnapshot,
getInitialSnapshot
} from '../src';
import { getInitialSnapshot } from '../src/getNextSnapshot';

describe('getNextSnapshot', () => {
it('should calculate the next snapshot for transition logic', () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/core/test/id.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { testAll } from './utils';
import { createMachine, createActor } from '../src/index.ts';
import { getInitialSnapshot, getNextSnapshot } from '../src/getNextSnapshot.ts';
import {
createMachine,
createActor,
getNextSnapshot,
getInitialSnapshot
} from '../src/index.ts';

const idMachine = createMachine({
initial: 'A',
Expand Down

0 comments on commit 960cdcb

Please sign in to comment.