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

Core: Move prepareContext into store.getStoryContext() #22135

Merged
merged 7 commits into from
Apr 30, 2023

Conversation

tmeasday
Copy link
Member

@tmeasday tmeasday commented Apr 18, 2023

This way any consumer of the context will get the properly mapped args.

Closes #22099

What I did

The issue was that although the source decorator emitted the mapped/conditional args, references to store.getStoryContext().args would return the unmapped args.

This is due to #20755 which moved the mapping to before the decorator chain.

This PR moves the mapping slightly earlier, so the context.args is mapped everywhere (not just inside decorators/render). That will make them inconsistent with context.initialArgs, but I think that's probably OK? I'm not sure, this might be considered to risky a change.

In which case we could consider just calling prepareContext in the Source block.

FTR, this inconsistency between context.args + context.initialArgs is already the case inside the decorator stack/render function, so this would only affect outside callers of store.getStoryContext(), which I'm not sure there are any.

How to test

  1. Run react sandbox
  2. Add this file:
import { Meta, StoryObj } from '@storybook/react';
import React from 'react';

const Example = (props: any) => {
  return <div>Message: {props.hasMessage ? props.message : 'no text'}</div>;
};

type Component = typeof Example;

const meta: Meta<Component> = {
  component: Example,
  tags: ['autodocs'],
  argTypes: {
    hasMessage: { type: 'boolean' },
    message: {
      control: 'text',
      if: { arg: 'hasMessage' },
    },
  },
};

export default meta;

export const Default: StoryObj<Component> = {
  args: {
    hasMessage: false,
    message: 'foo',
  },
};
  1. Check the source:

image

Checklist

  • Make sure your changes are tested (stories and/or unit, integration, or end-to-end tests)
  • Make sure to add/update documentation regarding your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Maintainers

  • If this PR should be tested against many or all sandboxes,
    make sure to add the ci:merged or ci:daily GH label to it.
  • Make sure this PR contains one of the labels below.

["cleanup", "BREAKING CHANGE", "feature request", "bug", "documentation", "maintenance", "dependencies", "other"]

This way any consumer of the context will get the properly mapped args. Also store the unmapped args on the context
@tmeasday tmeasday force-pushed the tom/22099-fix-conditional-context branch from cc3dd8c to d45d3d3 Compare April 20, 2023 05:50
@@ -103,48 +103,4 @@ describe('StoryRender', () => {
await render.renderToElement({} as any);
expect(story.playFunction).not.toHaveBeenCalled();
});

it('passes the initialArgs to loaders and render function if forceInitialArgs is true', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This functionality moved to the store so not really much point unit testing it here. We have a unit test in the story and an integration test in PreviewWeb.test.ts

@tmeasday
Copy link
Member Author

@shilman I ended up not adding using the new CSF type as, until we do #22228, we don't really have the correct type for unmappedArgs.

We may as well leave it as any (as a undefined key on the context) and sort of "undocumented" for now.

Copy link
Contributor

@JReinhold JReinhold left a comment

Choose a reason for hiding this comment

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

LGTM! 🥳

@tmeasday tmeasday merged commit d4e0b67 into next Apr 30, 2023
@tmeasday tmeasday deleted the tom/22099-fix-conditional-context branch April 30, 2023 23:41
@tmeasday tmeasday added the patch:yes Bugfix & documentation PR that need to be picked to main branch label May 15, 2023
shilman pushed a commit that referenced this pull request May 15, 2023
…context

Core: Move `prepareContext` into `store.getStoryContext()`
@shilman shilman mentioned this pull request May 15, 2023
@shilman shilman added the patch:done Patch/release PRs already cherry-picked to main/release branch label May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
block: source bug patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: On Storybook 7, the code snippet doesn't work if one of the controls is hidden
3 participants