2026-09-19 21:53:35 +03:00
|
|
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
|
|
import { screen, waitFor } from '@testing-library/react';
|
|
|
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
|
|
|
2026-09-19 22:03:46 +03:00
|
|
|
|
import { findRequest, installFetch, json, makeUser, recordedRequests, renderApp } from './helpers';
|
2026-09-19 21:53:35 +03:00
|
|
|
|
|
|
|
|
|
|
const instanceBase = {
|
|
|
|
|
|
name: 'glchat-test',
|
|
|
|
|
|
version: 'v0.1.0-test',
|
|
|
|
|
|
allow_guild_creation: true,
|
|
|
|
|
|
max_guilds_per_user: 5,
|
|
|
|
|
|
max_members_per_guild: 100,
|
|
|
|
|
|
max_message_length: 2000,
|
|
|
|
|
|
user_count: 1,
|
|
|
|
|
|
guild_count: 0,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
describe('страница регистрации', () => {
|
|
|
|
|
|
it('успешная регистрация ведёт на /onboarding', async () => {
|
|
|
|
|
|
const user = makeUser({ onboarding_completed: false });
|
|
|
|
|
|
const fetchMock = installFetch([
|
|
|
|
|
|
{
|
|
|
|
|
|
match: '/api/v1/instance',
|
|
|
|
|
|
response: () => json({ instance: { ...instanceBase, registration_enabled: true } }),
|
|
|
|
|
|
},
|
|
|
|
|
|
{ match: '/api/v1/auth/register', method: 'POST', response: () => json({ user }) },
|
|
|
|
|
|
{ match: '/api/v1/users/@me', response: () => json({ user }) },
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
const { router } = renderApp('/register');
|
|
|
|
|
|
const visitor = userEvent.setup();
|
|
|
|
|
|
|
|
|
|
|
|
await visitor.type(await screen.findByLabelText('Имя пользователя'), 'alice');
|
|
|
|
|
|
await visitor.type(screen.getByLabelText('Отображаемое имя'), 'Алиса');
|
|
|
|
|
|
await visitor.type(screen.getByLabelText('Почта'), 'alice@example.com');
|
|
|
|
|
|
await visitor.type(screen.getByLabelText('Пароль'), 'super-secret-1');
|
|
|
|
|
|
await visitor.click(screen.getByRole('button', { name: 'Создать аккаунт' }));
|
|
|
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
|
expect(router.state.location.pathname).toBe('/onboarding');
|
|
|
|
|
|
});
|
|
|
|
|
|
expect(await screen.findByText('Привет, Alice!')).toBeVisible();
|
|
|
|
|
|
|
2026-09-19 22:03:46 +03:00
|
|
|
|
expect(findRequest(fetchMock, { url: '/auth/register', method: 'POST' })?.body).toEqual({
|
2026-09-19 21:53:35 +03:00
|
|
|
|
username: 'alice',
|
|
|
|
|
|
display_name: 'Алиса',
|
|
|
|
|
|
email: 'alice@example.com',
|
|
|
|
|
|
password: 'super-secret-1',
|
|
|
|
|
|
locale: 'ru',
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('не отправляет форму с некорректными данными', async () => {
|
|
|
|
|
|
const fetchMock = installFetch([
|
|
|
|
|
|
{
|
|
|
|
|
|
match: '/api/v1/instance',
|
|
|
|
|
|
response: () => json({ instance: { ...instanceBase, registration_enabled: true } }),
|
|
|
|
|
|
},
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
renderApp('/register');
|
|
|
|
|
|
const visitor = userEvent.setup();
|
|
|
|
|
|
|
|
|
|
|
|
await visitor.type(await screen.findByLabelText('Имя пользователя'), 'al');
|
|
|
|
|
|
await visitor.type(screen.getByLabelText('Отображаемое имя'), 'Алиса');
|
|
|
|
|
|
await visitor.type(screen.getByLabelText('Почта'), 'not-an-email');
|
|
|
|
|
|
await visitor.type(screen.getByLabelText('Пароль'), 'short');
|
|
|
|
|
|
await visitor.click(screen.getByRole('button', { name: 'Создать аккаунт' }));
|
|
|
|
|
|
|
|
|
|
|
|
expect(await screen.findByText(/Имя пользователя: 3–32 символа/u)).toBeVisible();
|
|
|
|
|
|
expect(screen.getByText('Укажите корректный адрес почты.')).toBeVisible();
|
|
|
|
|
|
expect(screen.getByText('Пароль должен быть не короче 8 символов.')).toBeVisible();
|
|
|
|
|
|
expect(
|
2026-09-19 22:03:46 +03:00
|
|
|
|
recordedRequests(fetchMock).filter((request) => request.url.includes('/auth/register')),
|
2026-09-19 21:53:35 +03:00
|
|
|
|
).toHaveLength(0);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('при registration_enabled=false показывает сообщение вместо формы', async () => {
|
|
|
|
|
|
installFetch([
|
|
|
|
|
|
{
|
|
|
|
|
|
match: '/api/v1/instance',
|
|
|
|
|
|
response: () => json({ instance: { ...instanceBase, registration_enabled: false } }),
|
|
|
|
|
|
},
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
renderApp('/register');
|
|
|
|
|
|
|
|
|
|
|
|
expect(await screen.findByText('Регистрация закрыта')).toBeVisible();
|
|
|
|
|
|
expect(screen.queryByLabelText('Имя пользователя')).toBeNull();
|
|
|
|
|
|
expect(screen.queryByRole('button', { name: 'Создать аккаунт' })).toBeNull();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|