|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import type { StoryContext } from '@storybook/react-vite'; |
| 4 | + |
| 5 | +import { expect, fn } from 'storybook/test'; |
| 6 | + |
| 7 | +import preview from '../../../../.storybook/preview'; |
| 8 | + |
| 9 | +const Button = (args: React.ComponentProps<'button'>) => <button {...args} />; |
| 10 | + |
| 11 | +const meta = preview.meta({ |
| 12 | + component: Button, |
| 13 | + render: (args, { name }) => ( |
| 14 | + <span> |
| 15 | + {name} |
| 16 | + <br /> |
| 17 | + <br /> |
| 18 | + <button {...args} /> |
| 19 | + </span> |
| 20 | + ), |
| 21 | + args: { |
| 22 | + children: 'Default', |
| 23 | + onClick: fn(), |
| 24 | + }, |
| 25 | + tags: ['some-tag', 'autodocs'], |
| 26 | +}); |
| 27 | + |
| 28 | +export const WithNoTests = meta.story(); |
| 29 | + |
| 30 | +export const TestFunctionTypes = meta.story({ |
| 31 | + args: { |
| 32 | + children: 'Arg from story', |
| 33 | + }, |
| 34 | +}); |
| 35 | + |
| 36 | +export const PlayFunction = meta.story({ |
| 37 | + play: async ({ canvas, userEvent }) => { |
| 38 | + const button = canvas.getByText('Default'); |
| 39 | + await userEvent.click(button); |
| 40 | + }, |
| 41 | +}); |
| 42 | + |
| 43 | +TestFunctionTypes.test('simple', async ({ canvas, userEvent, args }) => { |
| 44 | + const button = canvas.getByText('Arg from story'); |
| 45 | + await userEvent.click(button); |
| 46 | + await expect(args.onClick).toHaveBeenCalled(); |
| 47 | +}); |
| 48 | + |
| 49 | +const doTest = async ({ |
| 50 | + canvas, |
| 51 | + userEvent, |
| 52 | + args, |
| 53 | +}: StoryContext<React.ComponentProps<'button'>>) => { |
| 54 | + const button = canvas.getByText('Arg from story'); |
| 55 | + await userEvent.click(button); |
| 56 | + await expect(args.onClick).toHaveBeenCalled(); |
| 57 | +}; |
| 58 | +TestFunctionTypes.test('referring to function in file', doTest); |
| 59 | + |
| 60 | +TestFunctionTypes.test( |
| 61 | + 'with overrides', |
| 62 | + { |
| 63 | + args: { |
| 64 | + children: 'Arg from test override', |
| 65 | + }, |
| 66 | + parameters: { |
| 67 | + viewport: { |
| 68 | + options: { |
| 69 | + sized: { |
| 70 | + name: 'Sized', |
| 71 | + styles: { |
| 72 | + width: '380px', |
| 73 | + height: '500px', |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + chromatic: { viewports: [380] }, |
| 79 | + }, |
| 80 | + globals: { sb_theme: 'dark', viewport: { value: 'sized' } }, |
| 81 | + }, |
| 82 | + async ({ canvas }) => { |
| 83 | + const button = canvas.getByText('Arg from test override'); |
| 84 | + await expect(button).toBeInTheDocument(); |
| 85 | + expect(document.body.clientWidth).toBe(380); |
| 86 | + } |
| 87 | +); |
| 88 | + |
| 89 | +TestFunctionTypes.test( |
| 90 | + 'with play function', |
| 91 | + { |
| 92 | + play: async ({ canvas }) => { |
| 93 | + const button = canvas.getByText('Arg from story'); |
| 94 | + await expect(button).toBeInTheDocument(); |
| 95 | + }, |
| 96 | + }, |
| 97 | + async ({ canvas }) => { |
| 98 | + const button = canvas.getByText('Arg from story'); |
| 99 | + await expect(button).toBeEnabled(); |
| 100 | + } |
| 101 | +); |
| 102 | + |
| 103 | +export const ExtendedStorySinglePlayExample = TestFunctionTypes.extend({ |
| 104 | + args: { |
| 105 | + children: 'Arg from extended story', |
| 106 | + }, |
| 107 | + play: async ({ canvas }) => { |
| 108 | + const button = canvas.getByText('Arg from extended story'); |
| 109 | + await expect(button).toBeEnabled(); |
| 110 | + }, |
| 111 | +}); |
| 112 | + |
| 113 | +export const ExtendedStorySingleTestExample = TestFunctionTypes.extend({ |
| 114 | + args: { |
| 115 | + children: 'Arg from extended story', |
| 116 | + }, |
| 117 | +}); |
| 118 | + |
| 119 | +ExtendedStorySingleTestExample.test( |
| 120 | + 'this is a very long test name to explain that this story test should guarantee that the args have been extended correctly', |
| 121 | + async ({ canvas }) => { |
| 122 | + const button = canvas.getByText('Arg from extended story'); |
| 123 | + await expect(button).toBeEnabled(); |
| 124 | + } |
| 125 | +); |
| 126 | + |
| 127 | +// This is intentionally defined out-of-order |
| 128 | +PlayFunction.test('should be clicked by play function', async ({ args }) => { |
| 129 | + await expect(args.onClick).toHaveBeenCalled(); |
| 130 | +}); |
| 131 | + |
| 132 | +export const TestNames = meta.story({ |
| 133 | + args: { |
| 134 | + children: 'This story is no-op, just focus on the test names', |
| 135 | + }, |
| 136 | +}); |
| 137 | +TestNames.test( |
| 138 | + 'should display an error when login is attempted with an expired session token', |
| 139 | + () => {} |
| 140 | +); |
| 141 | + |
| 142 | +TestNames.test( |
| 143 | + 'should display an error when login is attempted with multiple invalid password attempts', |
| 144 | + () => {} |
| 145 | +); |
| 146 | + |
| 147 | +TestNames.test('should display an error when login is attempted with a revoked API key', () => {}); |
| 148 | + |
| 149 | +TestNames.test( |
| 150 | + 'should display an error when login is attempted after exceeding the maximum session limit', |
| 151 | + () => {} |
| 152 | +); |
| 153 | + |
| 154 | +TestNames.test( |
| 155 | + 'should display an error when login is attempted with a disabled user account', |
| 156 | + () => {} |
| 157 | +); |
| 158 | + |
| 159 | +TestNames.test( |
| 160 | + 'should display an error when login is attempted with an unsupported authentication provider', |
| 161 | + () => {} |
| 162 | +); |
| 163 | + |
| 164 | +TestNames.test( |
| 165 | + 'should display an error when login is attempted after the password reset process is incomplete', |
| 166 | + () => {} |
| 167 | +); |
| 168 | + |
| 169 | +TestNames.test( |
| 170 | + 'should display an error when login is attempted with a malformed authentication request', |
| 171 | + () => {} |
| 172 | +); |
| 173 | + |
| 174 | +TestNames.test( |
| 175 | + 'should display an error when login is attempted with an unverified email address', |
| 176 | + () => {} |
| 177 | +); |
0 commit comments