Testing and stories
By the end of this guide you have a story for a TILE component, with the theme and the translations it needs.
A story is one state of a component, rendered on its own in Storybook with no app around it. A component that takes its data as props is easy to show this way. DXOS writes a story for each component, and runs the stories as tests.
A story
Section titled “A story”A story needs the theme provider, react-ui’s translations and the component’s own. withTheme from
@dxos/react-ui/testing wraps the story in Theme.Provider with Theme.defaultTx, and passes the story’s
translations parameter as its resources. Use it on every story of a TILE component.
The component here is ContactList from TILE in a PLEX plugin. It is a
list of contacts that shows a hint when it has none:
import { const withTheme: ({ tx, platform }?: Partial<Theme.ThemeContextValue>) => Decorator
Adds theme decorator.
withTheme } from '@dxos/react-ui/testing';
export default { title: string
title: 'contacts/ContactList', component: ({ contacts, onSelect }: ContactListProps) => React.JSX.Element
component: const ContactList: ({ contacts, onSelect }: ContactListProps) => React.JSX.Element
Takes its data and callbacks as props; a container reads them from the app and passes them in.
ContactList, decorators: Decorator[]
decorators: [function withTheme({ tx, platform }?: Partial<Theme.ThemeContextValue>): Decorator
Adds theme decorator.
withTheme()], // react-ui's strings, and the component's own. parameters: { translations: ({ readonly 'en-US': { readonly "@dxos/react-ui": { readonly "empty.label": "No items"; readonly "error-fallback.title.label": "Runtime Error"; readonly "error-fallback.stack.label": "Stack"; readonly "error-fallback.data.label": "Data"; readonly "steps.step.label": "Step {{index}}"; readonly "remove.label": "Delete"; readonly "toolbar-menu.label": "Action menu"; readonly "toolbar-drag-handle.label": "Drag to rearrange"; readonly "toolbar-close.label": "Close"; readonly "toolbar-delete.label": "Delete"; readonly "floating-panel.minimize.label": "Minimize"; readonly "floating-panel.maximize.label": "Maximize"; readonly "floating-panel.restore.label": "Restore"; readonly "drawer.resize.label": "Resize drawer"; readonly "system-button.star.label": "Star"; readonly "system-button.unstar.label": "Unstar"; readonly "system-button.bookmark.label": "Bookmark"; readonly "system-button.unbookmark.label": "Remove bookmark"; readonly "system-button.check.label": "Check"; readonly "system-button.uncheck.label": "Uncheck"; readonly "system-button.expand.label": "Expand"; readonly "system-button.collapse.label": "Collapse"; readonly "system-button.ai.label": "Run AI"; readonly "system-button.add.label": "Add"; readonly "system-button.delete.label": "Delete"; readonly "system-button.remove.label": "Delete"; readonly "system-button.edit.label": "Edit"; readonly "system-button.close.label": "Close"; readonly "system-button.open.label": "Open"; readonly "system-button.save.label": "Save"; readonly "system-button.cancel.label": "Cancel"; readonly "system-button.upload.label": "Upload"; readonly "system-button.download.label": "Download"; readonly "system-button.clipboard.label": "Copy"; readonly "system-button.copied.label": "Copied"; readonly "carousel-viewport.label": "Carousel"; readonly "carousel-prev.label": "Previous slide"; readonly "carousel-next.label": "Next slide"; readonly "carousel-indicators.label": "Carousel slides"; readonly "carousel-go-to.label": "Go to slide {{index}}"; readonly "date-picker.placeholder.single.label": "Pick a date"; readonly "date-picker.placeholder.range.label": "Pick a date range"; readonly "date-picker.placeholder.multiple.label": "Pick dates"; readonly "date-picker.clear.label": "Clear"; readonly "calendar.nav.previous.label": "Previous month"; readonly "calendar.nav.next.label": "Next month"; readonly "calendar.nav.previous-year.label": "Previous year"; readonly "calendar.nav.next-year.label": "Next year"; readonly "calendar.nav.previous-decade.label": "Previous decade"; readonly "calendar.nav.next-decade.label": "Next decade"; readonly "calendar.footer.today.label": "Today"; readonly "trigger-button.label": "Open"; readonly "number-input.increment.label": "Increment"; readonly "number-input.decrement.label": "Decrement"; readonly "number-input.slider.label": "Adjust"; readonly "password-input.show.label": "Show password"; readonly "password-input.hide.label": "Hide password"; readonly "tag.delete.label": "Remove {{label}}"; readonly "combobox.search.label": "Search"; readonly "combobox.empty.label": "No results"; readonly "combobox.loading.label": "Loading\u2026"; readonly "combobox.create.label": "Create \u201C{{query}}\u201D"; readonly "drag-handle.label": "Drag to rearrange"; readonly "drag-handle.role.label": "drag handle"; readonly "drag-handle.grabbed.message": "Grabbed. Press the arrow keys to move, Space to drop."; readonly "drag-handle.moved-up.message": "Moved up."; readonly "drag-handle.moved-down.message": "Moved down."; readonly "drag-handle.dropped.message": "Dropped."; }; }; } | { ...; })[];}
parameters: { translations: ({ readonly 'en-US': { readonly "@dxos/react-ui": { readonly "empty.label": "No items"; readonly "error-fallback.title.label": "Runtime Error"; readonly "error-fallback.stack.label": "Stack"; readonly "error-fallback.data.label": "Data"; readonly "steps.step.label": "Step {{index}}"; readonly "remove.label": "Delete"; readonly "toolbar-menu.label": "Action menu"; readonly "toolbar-drag-handle.label": "Drag to rearrange"; readonly "toolbar-close.label": "Close"; readonly "toolbar-delete.label": "Delete"; readonly "floating-panel.minimize.label": "Minimize"; readonly "floating-panel.maximize.label": "Maximize"; readonly "floating-panel.restore.label": "Restore"; readonly "drawer.resize.label": "Resize drawer"; readonly "system-button.star.label": "Star"; readonly "system-button.unstar.label": "Unstar"; readonly "system-button.bookmark.label": "Bookmark"; readonly "system-button.unbookmark.label": "Remove bookmark"; readonly "system-button.check.label": "Check"; readonly "system-button.uncheck.label": "Uncheck"; readonly "system-button.expand.label": "Expand"; readonly "system-button.collapse.label": "Collapse"; readonly "system-button.ai.label": "Run AI"; readonly "system-button.add.label": "Add"; readonly "system-button.delete.label": "Delete"; readonly "system-button.remove.label": "Delete"; readonly "system-button.edit.label": "Edit"; readonly "system-button.close.label": "Close"; readonly "system-button.open.label": "Open"; readonly "system-button.save.label": "Save"; readonly "system-button.cancel.label": "Cancel"; readonly "system-button.upload.label": "Upload"; readonly "system-button.download.label": "Download"; readonly "system-button.clipboard.label": "Copy"; readonly "system-button.copied.label": "Copied"; readonly "carousel-viewport.label": "Carousel"; readonly "carousel-prev.label": "Previous slide"; readonly "carousel-next.label": "Next slide"; readonly "carousel-indicators.label": "Carousel slides"; readonly "carousel-go-to.label": "Go to slide {{index}}"; readonly "date-picker.placeholder.single.label": "Pick a date"; readonly "date-picker.placeholder.range.label": "Pick a date range"; readonly "date-picker.placeholder.multiple.label": "Pick dates"; readonly "date-picker.clear.label": "Clear"; readonly "calendar.nav.previous.label": "Previous month"; readonly "calendar.nav.next.label": "Next month"; readonly "calendar.nav.previous-year.label": "Previous year"; readonly "calendar.nav.next-year.label": "Next year"; readonly "calendar.nav.previous-decade.label": "Previous decade"; readonly "calendar.nav.next-decade.label": "Next decade"; readonly "calendar.footer.today.label": "Today"; readonly "trigger-button.label": "Open"; readonly "number-input.increment.label": "Increment"; readonly "number-input.decrement.label": "Decrement"; readonly "number-input.slider.label": "Adjust"; readonly "password-input.show.label": "Show password"; readonly "password-input.hide.label": "Hide password"; readonly "tag.delete.label": "Remove {{label}}"; readonly "combobox.search.label": "Search"; readonly "combobox.empty.label": "No results"; readonly "combobox.loading.label": "Loading\u2026"; readonly "combobox.create.label": "Create \u201C{{query}}\u201D"; readonly "drag-handle.label": "Drag to rearrange"; readonly "drag-handle.role.label": "drag handle"; readonly "drag-handle.grabbed.message": "Grabbed. Press the arrow keys to move, Space to drop."; readonly "drag-handle.moved-up.message": "Moved up."; readonly "drag-handle.moved-down.message": "Moved down."; readonly "drag-handle.dropped.message": "Dropped."; }; };} | { ...;})[]
translations: [...const uiTranslations: [{ readonly 'en-US': { readonly "@dxos/react-ui": { readonly "empty.label": "No items"; readonly "error-fallback.title.label": "Runtime Error"; readonly "error-fallback.stack.label": "Stack"; readonly "error-fallback.data.label": "Data"; readonly "steps.step.label": "Step {{index}}"; readonly "remove.label": "Delete"; readonly "toolbar-menu.label": "Action menu"; readonly "toolbar-drag-handle.label": "Drag to rearrange"; readonly "toolbar-close.label": "Close"; readonly "toolbar-delete.label": "Delete"; readonly "floating-panel.minimize.label": "Minimize"; readonly "floating-panel.maximize.label": "Maximize"; readonly "floating-panel.restore.label": "Restore"; readonly "drawer.resize.label": "Resize drawer"; readonly "system-button.star.label": "Star"; readonly "system-button.unstar.label": "Unstar"; readonly "system-button.bookmark.label": "Bookmark"; readonly "system-button.unbookmark.label": "Remove bookmark"; readonly "system-button.check.label": "Check"; readonly "system-button.uncheck.label": "Uncheck"; readonly "system-button.expand.label": "Expand"; readonly "system-button.collapse.label": "Collapse"; readonly "system-button.ai.label": "Run AI"; readonly "system-button.add.label": "Add"; readonly "system-button.delete.label": "Delete"; readonly "system-button.remove.label": "Delete"; readonly "system-button.edit.label": "Edit"; readonly "system-button.close.label": "Close"; readonly "system-button.open.label": "Open"; readonly "system-button.save.label": "Save"; readonly "system-button.cancel.label": "Cancel"; readonly "system-button.upload.label": "Upload"; readonly "system-button.download.label": "Download"; readonly "system-button.clipboard.label": "Copy"; readonly "system-button.copied.label": "Copied"; readonly "carousel-viewport.label": "Carousel"; readonly "carousel-prev.label": "Previous slide"; readonly "carousel-next.label": "Next slide"; readonly "carousel-indicators.label": "Carousel slides"; readonly "carousel-go-to.label": "Go to slide {{index}}"; readonly "date-picker.placeholder.single.label": "Pick a date"; readonly "date-picker.placeholder.range.label": "Pick a date range"; readonly "date-picker.placeholder.multiple.label": "Pick dates"; readonly "date-picker.clear.label": "Clear"; readonly "calendar.nav.previous.label": "Previous month"; readonly "calendar.nav.next.label": "Next month"; readonly "calendar.nav.previous-year.label": "Previous year"; readonly "calendar.nav.next-year.label": "Next year"; readonly "calendar.nav.previous-decade.label": "Previous decade"; readonly "calendar.nav.next-decade.label": "Next decade"; readonly "calendar.footer.today.label": "Today"; readonly "trigger-button.label": "Open"; readonly "number-input.increment.label": "Increment"; readonly "number-input.decrement.label": "Decrement"; readonly "number-input.slider.label": "Adjust"; readonly "password-input.show.label": "Show password"; readonly "password-input.hide.label": "Hide password"; readonly "tag.delete.label": "Remove {{label}}"; readonly "combobox.search.label": "Search"; readonly "combobox.empty.label": "No results"; readonly "combobox.loading.label": "Loading\u2026"; readonly "combobox.create.label": "Create \u201C{{query}}\u201D"; readonly "drag-handle.label": "Drag to rearrange"; readonly "drag-handle.role.label": "drag handle"; readonly "drag-handle.grabbed.message": "Grabbed. Press the arrow keys to move, Space to drop."; readonly "drag-handle.moved-up.message": "Moved up."; readonly "drag-handle.moved-down.message": "Moved down."; readonly "drag-handle.dropped.message": "Dropped."; }; };}]
uiTranslations, ...const translations: [{ ...;}]
translations] },};
export const const Default: { args: { contacts: { id: string; name: string; }[]; };}
Default = { args: { contacts: { id: string; name: string; }[];}
args: { contacts: { id: string; name: string;}[]
contacts: [ { id: string
id: 'ada', name: string
name: 'Ada Lovelace' }, { id: string
id: 'grace', name: string
name: 'Grace Hopper' }, ], },};
export const const NoContacts: { args: { contacts: never[]; };}
NoContacts = { args: { contacts: never[];}
args: { contacts: never[]
contacts: [] } };
Default and NoContacts are two stories, because each export after the default is one. Write a story per state,
rather than one story with every state at once. A change to the empty list then shows in NoContacts and nowhere
else.
withTheme also passes Storybook’s theme global to the provider as themeMode. A component that calls
useThemeMode then gets the mode that the switcher in Storybook’s toolbar chose.
The Storybook setup
Section titled “The Storybook setup”Your app’s Storybook needs the same Vite setup as the app. Add ThemePlugin and IconsPlugin from
Installation, and put the two stylesheet imports in its preview file.
Helpers and tests
Section titled “Helpers and tests”@dxos/react-ui/testing also exports the helpers DXOS’s own stories use:
| Export | Use |
|---|---|
withTheme |
The theme provider for a story |
withLayout |
A frame around the story, with classNames for its size and padding |
withSizes, SIZE_ARG_TYPES |
The story at the size its size arg names, and the control that picks it |
Loading |
A loading placeholder for a story |
ThrowError |
A component that throws after a delay, for trying an error boundary |
A story can also test behavior. A play function is code that Storybook runs in the browser after it renders the
story. For example, a play function can open a dialog, press a key, and check where focus went. Target elements by
data-testid, which TILE parts pass through to their element.