- 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>
71 lines
2.0 KiB
JavaScript
71 lines
2.0 KiB
JavaScript
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);
|
||
});
|
||
});
|