Skip to content

Commit

Permalink
add play stories and fix play functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensacks committed Jun 6, 2023
1 parent e72c802 commit f021609
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 10 deletions.
19 changes: 16 additions & 3 deletions src/stories/Test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import React, {FC} from 'react';
import {useTranslation} from 'react-i18next';
import React, {FC, useState} from 'react';
import {useTranslation, Trans} from 'react-i18next';

const Test: FC = () => {
const {t} = useTranslation();
const [count, setCount] = useState(0);

return <div>{t('hello')}</div>;
const onClick = () => setCount((count) => count + 1);

return (
<div>
<span>{t('hello')}</span>{' '}
<span>
<Trans>world</Trans>
</span>
<div style={{marginTop: '1rem'}}>
<button onClick={onClick}>{t('click', {count})}</button>
</div>
</div>
);
};

export default Test;
3 changes: 3 additions & 0 deletions src/stories/locales/en.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export default {
hello: 'Hello',
world: 'World',
click_one: '{{count}} click',
click_other: '{{count}} clicks',
};
3 changes: 3 additions & 0 deletions src/stories/locales/fr.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export default {
hello: 'Bonjour',
world: 'Monde',
click_one: '{{count}} clic',
click_other: '{{count}} clics',
};
3 changes: 3 additions & 0 deletions src/stories/locales/ja.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export default {
hello: 'こんにちは',
world: '世界',
click_one: '{{count}}クリック',
click_other: '{{count}}クリック',
};
54 changes: 54 additions & 0 deletions src/stories/test.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import type {Meta, StoryObj} from '@storybook/react';
import {userEvent, within} from '@storybook/testing-library';
import Test from './Test';

// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction#default-export
Expand Down Expand Up @@ -34,3 +35,56 @@ export const Japanese: StoryObj = {
},
render: Test,
};

export const PlayDefault: StoryObj = {
render: Test,
};
PlayDefault.play = async ({canvasElement}) => {
const canvas = within(canvasElement);
const button = canvas.getByRole('button');
await userEvent.click(button);
await userEvent.click(button);
await userEvent.click(button);
};

export const PlayEnglish: StoryObj = {
parameters: {
locale: 'en',
},
render: Test,
};
PlayEnglish.play = async ({canvasElement}) => {
const canvas = within(canvasElement);
const button = canvas.getByRole('button');
await userEvent.click(button);
await userEvent.click(button);
await userEvent.click(button);
};

export const PlayFrench: StoryObj = {
parameters: {
locale: 'fr',
},
render: Test,
};
PlayFrench.play = async ({canvasElement}) => {
const canvas = within(canvasElement);
const button = canvas.getByRole('button');
await userEvent.click(button);
await userEvent.click(button);
await userEvent.click(button);
};

export const PlayJapanese: StoryObj = {
parameters: {
locale: 'ja',
},
render: Test,
};
PlayJapanese.play = async ({canvasElement}) => {
const canvas = within(canvasElement);
const button = canvas.getByRole('button');
await userEvent.click(button);
await userEvent.click(button);
await userEvent.click(button);
};
26 changes: 19 additions & 7 deletions src/withI18Next.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {ReactNode} from 'react';
import React, {Fragment, ReactNode, useState} from 'react';
import {useEffect, useGlobals} from '@storybook/client-api';
import {
PartialStoryFn as StoryFunction,
Expand All @@ -15,17 +15,29 @@ export const withI18Next = (
parameters: {i18n},
} = context;

const language = i18n.language;

const [{locale}] = useGlobals();
const [key, setKey] = useState(0);

useEffect(() => {
i18n.on('languageChanged', () => {
setKey(Date.now());
});
return () => i18n.off('languageChanged');
}, []);

useEffect(() => {
if (locale) {
i18n?.changeLanguage(locale);
if (i18n && locale && language && locale !== language) {
i18n.changeLanguage(locale);
}
}, [locale]);
}, [language, locale, i18n]);

return (
<I18nextProvider i18n={i18n}>
{story(context) as ReactNode | null}
</I18nextProvider>
<Fragment key={key}>
<I18nextProvider i18n={i18n}>
{story(context) as ReactNode | null}
</I18nextProvider>
</Fragment>
);
};

0 comments on commit f021609

Please sign in to comment.