- Add spacing for the session-expired notice and distinguish login inputs. - Assert those surfaces in expired-session light and dark stories.
133 lines
4.7 KiB
TypeScript
133 lines
4.7 KiB
TypeScript
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<typeof LoginForm> = {
|
|
title: 'Authentication/Login form',
|
|
component: LoginForm,
|
|
parameters: { layout: 'centered' },
|
|
tags: ['autodocs'],
|
|
args: { onSubmit: async () => undefined },
|
|
decorators: [
|
|
(Story) => (
|
|
<div style={{ width: 'min(calc(100vw - 32px), 440px)' }}>
|
|
<Story />
|
|
</div>
|
|
),
|
|
],
|
|
};
|
|
export default meta;
|
|
type Story = StoryObj<typeof LoginForm>;
|
|
|
|
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) => (
|
|
<div data-theme="dark" style={{ width: 440, padding: 24, background: 'var(--color-bg)' }}>
|
|
<Story />
|
|
</div>
|
|
),
|
|
],
|
|
play: async ({ canvasElement }) => expectLoginSurfaces(canvasElement),
|
|
};
|
|
|
|
export const NarrowViewport: Story = {
|
|
decorators: [
|
|
(Story) => (
|
|
<div style={{ width: 320, padding: 16, background: 'var(--color-bg)' }}>
|
|
<Story />
|
|
</div>
|
|
),
|
|
],
|
|
};
|
|
|
|
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();
|
|
},
|
|
};
|