Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix state.can() from restored state #3097

Merged
merged 7 commits into from
Mar 11, 2022
Merged

Fix state.can() from restored state #3097

merged 7 commits into from
Mar 11, 2022

Conversation

davidkpiano
Copy link
Member

Fixes #3096

@changeset-bot
Copy link

changeset-bot bot commented Feb 26, 2022

🦋 Changeset detected

Latest commit: 1b2f3bd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
xstate Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ghost
Copy link

ghost commented Feb 26, 2022

CodeSee Review Map:

Review these changes using an interactive CodeSee Map

Review in an interactive map

View more CodeSee Maps

Legend

CodeSee Map Legend

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 26, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1b2f3bd:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration
funny-beaver-kdvrmu Issue #3096

@davidkpiano
Copy link
Member Author

@Andarist is this good to go?

@@ -905,6 +905,27 @@ describe('State', () => {

expect(nextState.can({ type: 'NEXT' })).toBeTruthy();
});

// https://github.com/statelyai/xstate/issues/3096
it('should work with restored state', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a dedicated rehydration test suite so perhaps it would be worth moving this there. I didn't notice at first that you already have a fix for this and I put such a test in that file locally:

    it('should get correct information back from `can` immediately', () => {
      const machine = createMachine({
        on: {
          FOO: {
            actions: () => {}
          }
        }
      });

      const persistedState = JSON.stringify(interpret(machine).start().state);
      const restoredState = JSON.parse(persistedState);

      const service = interpret(machine).start(restoredState);

      expect(service.state.can('FOO')).toBe(true);
    });

Both are pretty much equivalent, so I don't mind using any of those.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only this one here depicts better what actually got reported - the fact that under the hood we call machine.resolveState could be considered an implementation detail.

Comment on lines 919 to 921
const state = State.create(
JSON.parse(JSON.stringify(machine.initialState))
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels rather irrelevant for this test case - if we want to test that "machineless" state returns false then that should be moved to a dedicated test

@Andarist Andarist merged commit c881c8c into main Mar 11, 2022
@Andarist Andarist deleted the davidkpiano/3096-1 branch March 11, 2022 16:15
@github-actions github-actions bot mentioned this pull request Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: interpreter.state.can() always be false if the interpreter started with an initial state from DB
2 participants