import '../../src/i18n'; import type { Meta, StoryObj } from '@storybook/react-vite'; import { expect, userEvent, within } from 'storybook/test'; import { ApiError } from '../../src/api/client'; import { LoginForm } from '../../src/pages/auth/LoginForm'; const meta: Meta = { title: 'Authentication/Login form', component: LoginForm, parameters: { layout: 'centered' }, tags: ['autodocs'], args: { onSubmit: async () => undefined }, decorators: [ (Story) => (
), ], }; export default meta; type Story = StoryObj; async function expectLoginSurfaces(canvasElement: HTMLElement) { const canvas = within(canvasElement); const card = canvasElement.querySelector('section'); const notice = canvas.getByText('Your session has expired. Sign in again to continue.'); const username = canvas.getByRole('textbox', { name: 'Username' }); if (!card) throw new Error('Login card was not rendered.'); await expect(window.getComputedStyle(notice).marginBottom).not.toBe('0px'); await expect(window.getComputedStyle(username).backgroundColor).not.toBe( window.getComputedStyle(card).backgroundColor, ); } export const Initial: Story = {}; export const ExpiredSession: Story = { args: { expired: true }, play: async ({ canvasElement }) => expectLoginSurfaces(canvasElement), }; export const SignedOut: Story = { args: { signedOut: true } }; export const DarkTheme: Story = { args: { expired: true }, decorators: [ (Story) => (
), ], play: async ({ canvasElement }) => expectLoginSurfaces(canvasElement), }; export const NarrowViewport: Story = { decorators: [ (Story) => (
), ], }; export const EmptyFieldValidation: Story = { play: async ({ canvasElement }) => { const canvas = within(canvasElement); await userEvent.click(canvas.getByRole('button', { name: 'Sign in' })); await expect(canvas.getByText('Enter your username.')).toBeVisible(); await expect(canvas.getByRole('textbox', { name: 'Username' })).toHaveFocus(); await expect(canvas.getByLabelText('Password')).toHaveAttribute('aria-invalid', 'true'); }, }; export const InvalidCredentials: Story = { args: { onSubmit: async () => { throw new ApiError(401, 'Unauthorized'); }, }, play: async ({ canvasElement }) => { const canvas = within(canvasElement); await userEvent.type(canvas.getByRole('textbox', { name: 'Username' }), 'operator'); await userEvent.type(canvas.getByLabelText('Password'), 'wrong-password'); await userEvent.click(canvas.getByRole('button', { name: 'Sign in' })); await expect(await canvas.findByRole('alert')).toHaveTextContent( 'Username or password is incorrect.', ); await expect(canvas.getByRole('textbox', { name: 'Username' })).toHaveValue('operator'); await expect(canvas.getByLabelText('Password')).toHaveValue(''); await expect(canvas.getByLabelText('Password')).toHaveFocus(); }, }; export const RequestFailureAndRetry: Story = { args: { onSubmit: (() => { let attempts = 0; return async () => { attempts += 1; if (attempts === 1) throw new ApiError(503, 'Unavailable'); }; })(), }, play: async ({ canvasElement }) => { const canvas = within(canvasElement); await userEvent.type(canvas.getByRole('textbox', { name: 'Username' }), 'operator'); await userEvent.type(canvas.getByLabelText('Password'), 'temporary'); await userEvent.click(canvas.getByRole('button', { name: 'Sign in' })); await expect(await canvas.findByRole('alert')).toHaveTextContent( "Couldn't sign in. Check your connection and try again.", ); await expect(canvas.getByLabelText('Password')).toHaveValue('temporary'); await userEvent.click(canvas.getByRole('button', { name: 'Sign in' })); await expect(canvas.queryByRole('alert')).not.toBeInTheDocument(); }, }; export const Submitting: Story = { args: { onSubmit: () => new Promise(() => undefined) }, play: async ({ canvasElement }) => { const canvas = within(canvasElement); await userEvent.type(canvas.getByRole('textbox', { name: 'Username' }), 'operator'); await userEvent.type(canvas.getByLabelText('Password'), 'secret'); await userEvent.click(canvas.getByRole('button', { name: 'Sign in' })); await expect(canvas.getByRole('status')).toHaveTextContent('Signing in…'); await expect(canvas.getByRole('button', { name: 'Signing in…' })).toBeDisabled(); }, };