Skip to content

[Feature request]: Change globals before rendering story #535

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

Open
fredcido opened this issue Jan 27, 2025 · 5 comments
Open

[Feature request]: Change globals before rendering story #535

fredcido opened this issue Jan 27, 2025 · 5 comments
Labels
feature request New feature or request needs triage

Comments

@fredcido
Copy link

Is your feature request related to a problem? Please describe.

Hey all,

I've been using a decorator + global types to support locales on our stories:

// preview.tsx

export const globalTypes = {
  locale: {
    name: 'Locale',
    description: 'Internationalization locale',
    toolbar: {
      icon: 'globe',
      items: languages,
    },
  },
}

In the stories we allow to define supported languages for visual regression tests:

// my.stories.tsx

const Story: Meta<typeof PageContent> = {
  component: PageContent,
  title: 'Page Layouts/Layouts/Page',
  parameters: {
    i18n: {
      namespace: 'pricing',
      visualTests: ['en', 'es', 'fr'],
    },
  },
}

What I am trying to achieve is to use one of the existing hooks from test runners to set the globals before rendering the story based on these configured parameters, the solution I've found out so far was to read current page URL and set the globals using query parameter, but that brings a lot of overhead since for each language the page needs to be reloaded:

// test-runner.ts

const config: TestRunnerConfig = {
  tags: {
    include: ['visual-tests'],
  },
  setup: () => {
    expect.extend({toMatchImageSnapshot})
  },
  preRender: async (page, context) => {
    const pageContext = await getStoryContext(page, context)
    const languages = pageContext.parameters.i18n.visualTests
    for (const lang of languages) {
      const newUrl = new URL(page.url())
      newUrl.searchParams.set('globals', `locale:${lang}`)
     // Refreshing the whole page on every language doesn't seem a good idea
      await page.goto(newUrl.toString())

      const image = await page.screenshot({
        fullPage: true,
        animations: 'disabled',
      })

      expect(image).toMatchImageSnapshot()
    }
  },
}

Describe the solution you'd like

What would be the best way to set these globals for the story before it's rendered based on these parameters?
Not to mention we are going to have multiple breakpoints too, which could exponentially increase test timeouts.

Describe alternatives you've considered

No response

Are you able to assist to bring the feature to reality?

no

Additional context

No response

@fredcido fredcido added feature request New feature or request needs triage labels Jan 27, 2025
@annayap
Copy link

annayap commented Mar 20, 2025

we need this, too!

@annayap
Copy link

annayap commented Mar 24, 2025

@shilman using the updated hooks, previsit and postvisit.. ^ the workaround indicated above won't work anymore.

is there anyway this feature can be accommodated..? we wanna be able to take snapshots of components in different languages WITHOUT having to duplicate stories in a different language. 🙏

@shilman
Copy link
Member

shilman commented Mar 24, 2025

We are considering porting story modes from Chromatic to Storybook. Would that solve your needs?

https://www.chromatic.com/docs/modes/

@annayap
Copy link

annayap commented Mar 24, 2025

We are considering porting story modes from Chromatic to Storybook. Would that solve your needs?

https://www.chromatic.com/docs/modes/

@shilman yup, this definitely will! do we know when this may happen? will it be in Storybook v9?

@rhuanbarreto
Copy link

This is the magic that allows you to switch modes:

import type { Page } from "playwright";
import type { Channel } from "@storybook/core/channels";

interface StorybookGlobaThis {
  __STORYBOOK_PREVIEW__: {
    channel: Channel;
  };
}

export async function setStorybookGlobals(
  page: Page,
  globals: Record<string, unknown>
) {
  await page.evaluate((g) => {
    const channel = (globalThis as unknown as StorybookGlobaThis)
      .__STORYBOOK_PREVIEW__.channel;
    channel.emit("updateGlobals", {
      // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
      globals: {
        // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
        ...channel.last("globalsUpdated")?.[0].initialGlobals,
        ...g,
      },
    });
  }, globals);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request needs triage
Projects
None yet
Development

No branches or pull requests

4 participants