Desktop: drag and drop photos into the post form

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-26 22:51:53 +03:00
parent a657bd0762
commit c0aaed7d28
6 changed files with 153 additions and 7 deletions

View File

@@ -1,16 +1,33 @@
/**
* На десктопе форма объявления закрывается только крестиком: клик по фону
* не должен терять заполненные поля. На мобильном клик по фону закрывает.
* Там же на десктопе фото можно перетащить в форму (drag & drop).
*/
import { render, screen, fireEvent } from "@testing-library/react";
import CreateUpdatePost from "./CreateUpdatePost";
import { useIsDesktop } from "../hooks/useIsDesktop";
import { compressImage } from "../utils/compressImage";
import "../i18n";
jest.mock("../hooks/useIsDesktop", () => ({ useIsDesktop: jest.fn() }));
jest.mock("../hooks/usePostMutation", () => ({
usePostMutation: () => ({ save: jest.fn(), loading: false, error: null }),
}));
// Сжатие через canvas в jsdom не работает и тут не проверяется
jest.mock("../utils/compressImage", () => ({ compressImage: jest.fn() }));
// CRA сбрасывает реализации моков перед каждым тестом — задаём их здесь
beforeEach(() => {
compressImage.mockImplementation((file) => Promise.resolve(file));
URL.createObjectURL = jest.fn(() => "blob:preview");
URL.revokeObjectURL = jest.fn();
});
const ADD_PHOTO = /Добавить фото|Add photo/;
function fileDrag(files) {
return { dataTransfer: { files, types: ["Files"], dropEffect: "none" } };
}
function renderForm() {
const onClose = jest.fn();
@@ -56,3 +73,46 @@ test("клик внутри окна не закрывает форму", () =>
expect(onClose).not.toHaveBeenCalled();
});
test("на десктопе перетащенное фото добавляется в форму", async () => {
useIsDesktop.mockReturnValue(true);
renderForm();
const photo = new File(["x"], "cat.jpg", { type: "image/jpeg" });
fireEvent.drop(screen.getByText(ADD_PHOTO), fileDrag([photo]));
// После добавления первого фото кнопка становится «Добавить ещё»
expect(await screen.findByText(/Добавить ещё|Add more/)).toBeTruthy();
expect(compressImage).toHaveBeenCalledWith(photo);
});
test("на десктопе файл над зоной подсвечивает её подсказкой", () => {
useIsDesktop.mockReturnValue(true);
renderForm();
fireEvent.dragEnter(screen.getByText(ADD_PHOTO), fileDrag([]));
expect(screen.getByText(/Отпустите, чтобы добавить фото|Drop to add photos/)).toBeTruthy();
});
test("перетащенный файл неподходящего типа не добавляется", async () => {
useIsDesktop.mockReturnValue(true);
renderForm();
const gif = new File(["x"], "anim.gif", { type: "image/gif" });
fireEvent.drop(screen.getByText(ADD_PHOTO), fileDrag([gif]));
expect(await screen.findByText(/Только изображения|Images only/)).toBeTruthy();
expect(compressImage).not.toHaveBeenCalled();
});
test("на мобильном перетаскивание фото выключено", () => {
useIsDesktop.mockReturnValue(false);
renderForm();
const photo = new File(["x"], "cat.jpg", { type: "image/jpeg" });
fireEvent.drop(screen.getByText(ADD_PHOTO), fileDrag([photo]));
expect(compressImage).not.toHaveBeenCalled();
expect(screen.queryByText(/или перетащите фото сюда|or drag photos here/)).toBeNull();
});