Files
nahodka-front/src/components/GuestGuide.test.jsx
SlimusMinus df91e7e129 Add first-visit tip and how-to guide for guests
- guests in the feed see a "New here?" tip with a waving hand: swipe up to
  dismiss on mobile (top), close button on desktop (bottom right); shown
  once, remembered in localStorage
- GuideModal: six short steps (feed and map, map pins, posting, chat with
  photos/video, phone reveal, marking as returned) plus safety tips;
  guests get a sign-in button
- "How it works" item in the mobile menu and the desktop sidebar
- ru/en texts, tests, README

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 22:25:15 +03:00

119 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Подсказка гостю «Ты здесь впервые?» и инструкция: появление с задержкой,
* закрытие крестиком и смахиванием вверх, запоминание, открытие инструкции.
*/
import { render, screen, fireEvent, act } from "@testing-library/react";
import "@testing-library/jest-dom";
import "../i18n";
import i18n from "../i18n";
import GuestGuideBanner, { GUIDE_BANNER_KEY } from "./GuestGuideBanner";
import GuideModal from "./GuideModal";
beforeAll(() => i18n.changeLanguage("ru"));
beforeEach(() => {
localStorage.clear();
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
function showBanner(props = {}) {
render(<GuestGuideBanner onOpenGuide={jest.fn()} {...props} />);
act(() => {
jest.advanceTimersByTime(1500);
});
}
test("подсказка появляется не сразу, а через секунду", () => {
render(<GuestGuideBanner onOpenGuide={jest.fn()} />);
expect(screen.queryByText("Ты здесь впервые?")).toBeNull();
act(() => {
jest.advanceTimersByTime(1500);
});
expect(screen.getByText("Ты здесь впервые?")).toBeInTheDocument();
expect(screen.getByText(/Посмотри, как пользоваться приложением/)).toBeInTheDocument();
});
test("крестик закрывает подсказку навсегда", () => {
showBanner();
fireEvent.click(screen.getByLabelText("Скрыть подсказку"));
act(() => {
jest.advanceTimersByTime(300);
});
expect(screen.queryByText("Ты здесь впервые?")).toBeNull();
expect(localStorage.getItem(GUIDE_BANNER_KEY)).toBe("1");
});
test("закрытая подсказка больше не показывается", () => {
localStorage.setItem(GUIDE_BANNER_KEY, "1");
showBanner();
expect(screen.queryByText("Ты здесь впервые?")).toBeNull();
});
test("смахивание вверх закрывает, короткое движение — нет", () => {
showBanner();
const banner = screen.getByRole("status");
// Чуть-чуть потянули — карточка возвращается на место
fireEvent.touchStart(banner, { touches: [{ clientY: 100 }] });
fireEvent.touchMove(banner, { touches: [{ clientY: 85 }] });
fireEvent.touchEnd(banner);
expect(localStorage.getItem(GUIDE_BANNER_KEY)).toBeNull();
expect(screen.getByText("Ты здесь впервые?")).toBeInTheDocument();
// Смахнули вверх — закрылась и запомнилась
fireEvent.touchStart(banner, { touches: [{ clientY: 100 }] });
fireEvent.touchMove(banner, { touches: [{ clientY: 30 }] });
fireEvent.touchEnd(banner);
act(() => {
jest.advanceTimersByTime(300);
});
expect(screen.queryByText("Ты здесь впервые?")).toBeNull();
expect(localStorage.getItem(GUIDE_BANNER_KEY)).toBe("1");
});
test("кнопка открывает инструкцию и убирает подсказку", () => {
const onOpenGuide = jest.fn();
showBanner({ onOpenGuide });
fireEvent.click(screen.getByRole("button", { name: "Как пользоваться" }));
expect(onOpenGuide).toHaveBeenCalledTimes(1);
expect(screen.queryByText("Ты здесь впервые?")).toBeNull();
expect(localStorage.getItem(GUIDE_BANNER_KEY)).toBe("1");
});
test("инструкция: шаги, безопасность и вход для гостя", () => {
const onClose = jest.fn();
const onLoginClick = jest.fn();
render(<GuideModal onClose={onClose} onLoginClick={onLoginClick} />);
expect(screen.getByRole("dialog", { name: "Как пользоваться Находкой" })).toBeInTheDocument();
// 6 шагов + 4 совета по безопасности
expect(screen.getAllByRole("listitem")).toHaveLength(6 + 4);
expect(screen.getByText("Ищи в ленте или на карте")).toBeInTheDocument();
expect(screen.getByText("Смотри на карте")).toBeInTheDocument();
expect(screen.getByText("Пиши автору")).toBeInTheDocument();
expect(screen.getByText("Будь осторожен", { exact: false })).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Войти или зарегистрироваться" }));
expect(onLoginClick).toHaveBeenCalledTimes(1);
fireEvent.keyDown(window, { key: "Escape" });
expect(onClose).toHaveBeenCalledTimes(1);
});
test("инструкция для вошедшего — без кнопки входа", () => {
render(<GuideModal onClose={jest.fn()} />);
expect(screen.queryByRole("button", { name: "Войти или зарегистрироваться" })).toBeNull();
expect(screen.getByRole("button", { name: "Понятно" })).toBeInTheDocument();
});