From 960cdcbcb88eb565bba2f03f3eeceff6001576d9 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Wed, 7 Feb 2024 11:14:19 -0500 Subject: [PATCH] Expose getInitialSnapshot (#4731) * Expose getInitialSnapshot * Add more details to changeset * Add words --- .changeset/clean-brooms-yell.md | 16 ++++++++++++++++ packages/core/src/index.ts | 2 +- packages/core/test/deterministic.test.ts | 10 +++++++--- packages/core/test/getNextSnapshot.test.ts | 5 ++--- packages/core/test/id.test.ts | 8 ++++++-- 5 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 .changeset/clean-brooms-yell.md diff --git a/.changeset/clean-brooms-yell.md b/.changeset/clean-brooms-yell.md new file mode 100644 index 0000000000..fc8273d0f5 --- /dev/null +++ b/.changeset/clean-brooms-yell.md @@ -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 +); +``` diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index b0177584bb..da7666949b 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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 diff --git a/packages/core/test/deterministic.test.ts b/packages/core/test/deterministic.test.ts index 753bb7482e..bdaafbaaa6 100644 --- a/packages/core/test/deterministic.test.ts +++ b/packages/core/test/deterministic.test.ts @@ -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({ diff --git a/packages/core/test/getNextSnapshot.test.ts b/packages/core/test/getNextSnapshot.test.ts index b5b37309ef..76759391d2 100644 --- a/packages/core/test/getNextSnapshot.test.ts +++ b/packages/core/test/getNextSnapshot.test.ts @@ -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', () => { diff --git a/packages/core/test/id.test.ts b/packages/core/test/id.test.ts index 86373a70af..ba624ff889 100644 --- a/packages/core/test/id.test.ts +++ b/packages/core/test/id.test.ts @@ -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',