fixed show phone number

This commit is contained in:
SlimusMinus
2026-10-27 22:52:10 +03:00
parent 113355c0ef
commit 4b7db41a97
13 changed files with 425 additions and 32 deletions

View File

@@ -0,0 +1,71 @@
/**
* Номер в карточке: в ленту он не приходит, открывается кнопкой
* «Показать номер» (proof-of-work на бэкенде). Автор и админ видят номер сразу.
*/
import { render, screen, fireEvent } from "@testing-library/react";
import PostOnFeed from "./PostOnFeed";
import { useAuthContext } from "../context/AuthContext";
import { revealPhone } from "../api/phoneApi";
import "../i18n";
jest.mock("../context/AuthContext", () => ({ useAuthContext: jest.fn() }));
jest.mock("../hooks/useContactSeller", () => ({
useContactSeller: () => ({ contactSeller: jest.fn(), error: "", clearError: jest.fn() }),
}));
jest.mock("../api/phoneApi", () => ({ revealPhone: jest.fn() }));
const AD = { id: 7, userId: 2, title: "Ключи", type: "loss", status: "ACTIVE", images: [] };
const SHOW = /Показать номер|Show number/;
beforeEach(() => {
useAuthContext.mockReturnValue({ isAuthenticated: false, user: null });
});
test("номер открывается по кнопке", async () => {
revealPhone.mockResolvedValue("+7 (999) 111-22-33");
render(<PostOnFeed ad={{ ...AD, phoneVisible: true }} />);
fireEvent.click(screen.getByText(SHOW));
expect(await screen.findByText("+7 (999) 111-22-33")).toBeTruthy();
expect(revealPhone).toHaveBeenCalledWith(7, expect.anything());
});
test("без согласия автора кнопки нет — только чат", () => {
render(<PostOnFeed ad={{ ...AD, phoneVisible: false }} />);
expect(screen.queryByText(SHOW)).toBeNull();
});
test("у закрытого объявления номер не предлагается", () => {
render(<PostOnFeed ad={{ ...AD, phoneVisible: true, status: "CLOSED" }} />);
expect(screen.queryByText(SHOW)).toBeNull();
});
test("номер, пришедший с объявлением (автор, админ), показывается сразу", () => {
render(<PostOnFeed ad={{ ...AD, phoneVisible: true, phone: "+7 (900) 000-00-00" }} />);
expect(screen.getByText("+7 (900) 000-00-00")).toBeTruthy();
expect(revealPhone).not.toHaveBeenCalled();
});
test("ошибка показа номера — сообщение", async () => {
revealPhone.mockRejectedValue(new TypeError("Failed to fetch"));
render(<PostOnFeed ad={{ ...AD, phoneVisible: true }} />);
fireEvent.click(screen.getByText(SHOW));
expect(await screen.findByText(/связ|connection|network/i)).toBeTruthy();
});
test("своё объявление в ленте: номер открывается кнопкой, вместо «Написать» — подпись", async () => {
useAuthContext.mockReturnValue({ isAuthenticated: true, user: { id: 2 } });
revealPhone.mockResolvedValue("+7 (999) 111-22-33");
render(<PostOnFeed ad={{ ...AD, phoneVisible: true }} />);
expect(screen.queryByText(/^(Написать|Write)$/)).toBeNull();
expect(screen.getByText(/Ваше объявление|Your listing/)).toBeTruthy();
fireEvent.click(screen.getByText(SHOW));
expect(await screen.findByText("+7 (999) 111-22-33")).toBeTruthy();
});