import { userEvent, within, expect, waitFor } from 'storybook/test'; import { action } from 'storybook/actions'; import { Card } from '@transferwise/icons'; import { withVariantConfig } from '../../../../.storybook/helpers'; import { allModes } from '../../../../.storybook/modes'; import { lorem20 } from '../../../test-utils'; import { wait } from '../../../test-utils/wait'; import Button from '../../../Button'; import { Snackbar } from '../Snackbar'; import type { SnackbarCreationProps } from '../Snackbar.types'; import { SnackbarConsumer } from '../SnackbarContext'; import SnackbarProvider from '../SnackbarProvider'; import useSnackbar from '../useSnackbar'; export default { title: 'Prompts/Snackbar/Tests', component: Snackbar, argTypes: {}, tags: ['!autodocs', '!manifest'], }; const launchSnackbar = async (canvasElement: HTMLElement) => { await wait(0); const canvas = within(canvasElement); await userEvent.click(canvas.getByRole('button', { name: 'Show Snackbar' })); await waitFor(async () => expect(document.querySelector('.wds-Snackbar-container-appear-done')).not.toBeNull(), ); }; type Args = Partial & { extraContext?: React.ReactNode; }; function SnackbarTestTrigger({ text = 'Some text', extraContext, ...props }: Args) { const createSnackbar = useSnackbar(); return ( <> {extraContext} ); } const Default = { args: { duration: 'indefinite', action: { label: 'View', onClick: action('View button clicked.') }, } as Args, play: async ({ canvasElement }: { canvasElement: HTMLElement }) => { await launchSnackbar(canvasElement); }, render: (args: Args) => { return ( ); }, }; export const Basic = { ...Default, args: { ...Default.args, }, }; export const Success = { ...Default, args: { ...Default.args, sentiment: 'success' }, }; export const Warning = { ...Default, args: { ...Default.args, sentiment: 'warning' }, }; export const Negative = { ...Default, args: { ...Default.args, sentiment: 'negative' }, }; export const Proposition = { ...Default, args: { ...Default.args, sentiment: 'proposition' }, parameters: { chromatic: { modes: { Desktop: allModes.desktop, 'Zoom 400%': allModes.zoom400 }, }, }, }; export const WithoutAction = { ...Default, args: { ...Default.args, action: undefined }, }; export const CustomMedia = { ...Default, render: (args: Args) => ( } /> ), parameters: { chromatic: { modes: { Desktop: allModes.desktop, Dark: { ...allModes.desktop, ...allModes.dark }, Mobile: allModes.largeMobile, 'Zoom 400%': allModes.zoom400, }, }, }, }; export const Loading = { ...Default, args: { ...Default.args, loading: true, }, parameters: { chromatic: { modes: { Desktop: allModes.desktop, Dark: { ...allModes.desktop, ...allModes.dark }, Mobile: allModes.largeMobile, 'Zoom 400%': allModes.zoom400, }, }, }, }; export const NavigationAction = { ...Default, args: { ...Default.args, action: { label: 'View transfer', href: '/transfers/123', target: '_blank', }, }, play: async ({ canvasElement }: { canvasElement: HTMLElement }) => { await launchSnackbar(canvasElement); const link = await waitFor(() => within(document.body).getByRole('link', { name: 'View transfer' }), ); await expect(link).toHaveAttribute('href', '/transfers/123'); await expect(link).toHaveAttribute('target', '_blank'); }, }; export const Mobile = { ...Default, args: { ...Default.args, }, ...withVariantConfig(['mobile'], Default), }; export const DarkMode = { ...Default, args: { ...Default.args, }, globals: { screenMode: 'dark' }, }; export const Zoom400 = { ...Default, args: { ...Default.args, extraContext: (
Snackbar isn't transparent (I'm going to make this text very long so that it will fill up the entire page and go behind the snackbar. This way, when you look at it you will easily be able to tell if the snackbar is transparent and recognize the bug because we don't want it to be transparent. Transparent is bad. When we first did rebrand we had a lot of problems with transparencies. {lorem20}
), }, ...withVariantConfig(['400%'], Default), }; export const Zoom400ShortMessage = { ...Default, ...withVariantConfig(['400%'], Default), }; /** Deliberately unsuitable content: records how unbroken words can overflow the layout. */ export const LongUnbrokenText = { ...Default, args: { ...Default.args, text: `Your transfer is ready. ${'VeryLongUnbrokenTransferReference'.repeat(5)}`, }, parameters: { docs: { description: { story: 'Stress case, not recommended usage. Very long unbroken words can overflow the current layout; keep Snackbar messages short and readable.', }, }, chromatic: { modes: { Desktop: allModes.desktop, Mobile: allModes.largeMobile }, }, }, }; /** * Snackbar is only shown once even if trigger is clicked many times */ export const MultipleClicks = { ...Default, args: { ...Default.args, }, play: async ({ canvasElement }: { canvasElement: HTMLElement }) => { const canvas = within(canvasElement); for (let i = 0; i < 8; i += 1) { await userEvent.click(canvas.getByRole('button')); } await expect(within(document.body).getAllByRole('status')).toHaveLength(1); }, }; /** Empty text dismisses the current Snackbar without rendering a blank replacement. */ export const ClearedMessage = { ...Default, args: { ...Default.args, extraContext: ( {({ createSnackbar }) => ( )} ), }, play: async ({ canvasElement }: { canvasElement: HTMLElement }) => { const canvas = within(canvasElement); await userEvent.click(canvas.getByRole('button', { name: 'Show Snackbar' })); await waitFor(async () => { await expect(within(document.body).getByRole('status')).toBeInTheDocument(); }); await userEvent.click(canvas.getByRole('button', { name: 'Clear Snackbar' })); await waitFor(async () => { await expect(within(document.body).queryByRole('status')).not.toBeInTheDocument(); await expect(document.querySelector('.wds-Snackbar')).toBeNull(); }); }, };