Skip to content

Commit 033c80c

Browse files
Merge pull request #355 from ibi-group/use-storybook-react-intl-addon
Use storybook-react-intl addon
2 parents 29618b9 + c103993 commit 033c80c

19 files changed

Lines changed: 245569 additions & 227666 deletions

File tree

.storybook/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module.exports = {
2020
}
2121
}
2222
},
23-
"@storybook/addon-viewport"
23+
"@storybook/addon-viewport",
24+
"storybook-react-intl"
2425
],
2526
stories: [
2627
"../packages/**/*.story.mdx",

.storybook/preview.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
import { setupWorker } from "msw";
2+
import { withReactIntl } from "storybook-react-intl/dist/cjs/withReactIntl";
3+
24
import locationFieldHandlers from "../packages/location-field/src/mocks/handlers";
3-
import itineraryBodyHandlers from '../packages/itinerary-body/src/__mocks__/handlers'
5+
import itineraryBodyHandlers from "../packages/itinerary-body/src/__mocks__/handlers";
46
import geocoderHandlers from "../packages/geocoder/src/test-fixtures/handlers";
57

8+
import { reactIntl } from './react-intl.js';
9+
610
// Only install worker when running in browser
7-
if (typeof global.process === 'undefined') {
11+
if (typeof global.process === "undefined") {
812
const worker = setupWorker(
913
...locationFieldHandlers,
1014
...itineraryBodyHandlers,
1115
...geocoderHandlers
1216
);
13-
worker.start({onUnhandledRequest: "bypass"})
17+
worker.start({ onUnhandledRequest: "bypass" });
1418
}
1519

16-
1720
export const parameters = {
1821
a11y: {
1922
config: {
2023
rules: [
2124
{
2225
// moved to technical backlog
23-
id: 'aria-required-parent',
26+
id: "aria-required-parent",
2427
reviewOnFail: true,
2528
},
2629
{
2730
// Appears to be a story bug
28-
id: 'duplicate-id',
31+
id: "duplicate-id",
2932
reviewOnFail: true
3033
},
3134
{
3235
// Appears to be a story bug
33-
id: 'duplicate-id-aria',
36+
id: "duplicate-id-aria",
3437
reviewOnFail: true
3538
}
3639
],
@@ -43,4 +46,16 @@ export const parameters = {
4346
date: /Date$/,
4447
},
4548
},
46-
}
49+
locale: reactIntl.defaultLocale,
50+
locales: {
51+
"en-US": { title: "English (US)", left: "🇺🇸" },
52+
fr: { title: "Français", left: "🇫🇷" },
53+
unknown: { title: "Unknown", left: "🚫" }
54+
},
55+
reactIntl
56+
};
57+
58+
// Per https://www.npmjs.com/package/@storybook/addon-storyshots,
59+
// explicitly export the storybook-react-intl decorator
60+
// so it is included in jest snapshots.
61+
export const decorators = [withReactIntl];

.storybook/react-intl.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import flatten from "flat";
2+
3+
/**
4+
* Copied from https://stackoverflow.com/questions/50940640/how-to-determine-if-jest-is-running-the-code-or-not
5+
*/
6+
function isRunningJest() {
7+
return process.env.JEST_WORKER_ID !== undefined;
8+
}
9+
10+
/** Locales supported in the storybook "Globe" dropdown menu. */
11+
const locales = ["en-US", "fr", "unknown"];
12+
13+
/** List of packages that will have localization support in Storybook. */
14+
const packages = [
15+
"from-to-location-picker",
16+
"location-field",
17+
"trip-details",
18+
"trip-form"
19+
];
20+
21+
/** Messages for all packages AND locales above. */
22+
const messages = {};
23+
24+
if (!isRunningJest()) {
25+
// Populate messages if not running snapshots.
26+
// (Message printouts would be unnecessary replicated in snapshots without that check.)
27+
packages.forEach((pkg) => {
28+
locales.forEach((locale) => {
29+
try {
30+
messages[locale] = {
31+
...messages.[locale],
32+
...flatten(require(`../packages/${pkg}/i18n/${locale}.yml`))
33+
};
34+
} catch (e) {
35+
// There is no yml files for the "unknown" locale,
36+
// so it should fail, and we won't display an error message in that case.
37+
if (locale !== "unknown") console.error(e);
38+
}
39+
});
40+
});
41+
}
42+
43+
// TODO: place any applicable (date, time, etc) format parameters here.
44+
const formats = {};
45+
46+
export const reactIntl = {
47+
defaultLocale: "en-US",
48+
formats,
49+
locales,
50+
messages
51+
};

0 commit comments

Comments
 (0)