Files
nahodka-front/src/hooks/useAdViewability.test.jsx
SlimusMinus a029254cf0 Tests for auth refresh, ad viewability and URL safety; nginx, Dockerfile, README
- Jest: single-flight refresh, session expiry vs transient errors, token save on login change,
  logout revoke, IAB viewability, safe URLs, HTML escaping, password policy, ad session id
- test:ci with explicit testMatch (dot-directories in worktree paths hid all tests)
- nginx: gzip, nosniff/Referrer-Policy/X-Frame-Options, no-cache for index.html
- Dockerfile: npm ci, REACT_APP_WS_ERROR_QUEUE build arg
- README: new backend contracts, flags, compatibility notes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 11:56:39 +03:00

71 lines
2.0 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, act } from "@testing-library/react";
import { useRef } from "react";
import { useAdViewability, VIEWABLE_MS } from "./useAdViewability";
let observerCallback;
beforeEach(() => {
jest.useFakeTimers();
observerCallback = null;
global.IntersectionObserver = jest.fn(function IntersectionObserver(cb) {
observerCallback = cb;
this.observe = jest.fn();
this.disconnect = jest.fn();
});
});
afterEach(() => {
jest.useRealTimers();
});
function Banner({ onViewable }) {
const ref = useRef(null);
useAdViewability(ref, onViewable);
return <div ref={ref} />;
}
const intersect = (ratio) => act(() => {
observerCallback([{ isIntersecting: ratio > 0, intersectionRatio: ratio }]);
});
describe("useAdViewability", () => {
it("засчитывает показ, когда ≥ 50% видно ≥ 1 секунды", () => {
const onViewable = jest.fn();
render(<Banner onViewable={onViewable} />);
intersect(0.6);
act(() => { jest.advanceTimersByTime(VIEWABLE_MS - 1); });
expect(onViewable).not.toHaveBeenCalled();
act(() => { jest.advanceTimersByTime(1); });
expect(onViewable).toHaveBeenCalledTimes(1);
});
it("меньше 50% или ушёл с экрана раньше секунды — не показ", () => {
const onViewable = jest.fn();
render(<Banner onViewable={onViewable} />);
intersect(0.3);
act(() => { jest.advanceTimersByTime(5000); });
intersect(0.8);
act(() => { jest.advanceTimersByTime(500); });
intersect(0);
act(() => { jest.advanceTimersByTime(5000); });
expect(onViewable).not.toHaveBeenCalled();
});
it("показ засчитывается один раз", () => {
const onViewable = jest.fn();
render(<Banner onViewable={onViewable} />);
intersect(1);
act(() => { jest.advanceTimersByTime(VIEWABLE_MS); });
intersect(0);
intersect(1);
act(() => { jest.advanceTimersByTime(VIEWABLE_MS * 3); });
expect(onViewable).toHaveBeenCalledTimes(1);
});
});