|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { RefreshPolicy } from './refresh-policy'; |
| 3 | + |
| 4 | +describe('RefreshPolicy - Mouse Interactivity & Click Handling', () => { |
| 5 | + it('prevents double-clicks from triggering redundant refresh calls using cooldown cooldownMs validation', () => { |
| 6 | + const policy = RefreshPolicy.getInstance(); |
| 7 | + policy.reset(); |
| 8 | + policy.setCooldown(1000); |
| 9 | + |
| 10 | + const firstCheck = policy.isRefreshAllowed('double-clicker'); |
| 11 | + expect(firstCheck).toBe(true); |
| 12 | + |
| 13 | + policy.recordRefresh('double-clicker'); |
| 14 | + |
| 15 | + // Simulate instant second click (mouse double click) |
| 16 | + const secondCheck = policy.isRefreshAllowed('double-clicker'); |
| 17 | + expect(secondCheck).toBe(false); |
| 18 | + }); |
| 19 | + |
| 20 | + it('computes layout offset dimensions for hover tooltip displays correctly', () => { |
| 21 | + const hoverEvent = { clientX: 100, clientY: 200 }; |
| 22 | + const getTooltipPosition = (e: { clientX: number; clientY: number }) => ({ |
| 23 | + top: e.clientY + 10, |
| 24 | + left: e.clientX + 10, |
| 25 | + }); |
| 26 | + |
| 27 | + const pos = getTooltipPosition(hoverEvent); |
| 28 | + expect(pos.top).toBe(210); |
| 29 | + expect(pos.left).toBe(110); |
| 30 | + }); |
| 31 | + |
| 32 | + it('verifies click handlers call refresh record actions exactly once', () => { |
| 33 | + const policy = RefreshPolicy.getInstance(); |
| 34 | + policy.reset(); |
| 35 | + const mockRecord = vi.spyOn(policy, 'recordRefresh'); |
| 36 | + |
| 37 | + const handleRefreshClick = (username: string) => { |
| 38 | + if (policy.isRefreshAllowed(username)) { |
| 39 | + policy.recordRefresh(username); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + handleRefreshClick('click-tester'); |
| 44 | + expect(mockRecord).toHaveBeenCalledTimes(1); |
| 45 | + expect(mockRecord).toHaveBeenCalledWith('click-tester'); |
| 46 | + |
| 47 | + // Subsequent click is blocked, so record is not called again |
| 48 | + handleRefreshClick('click-tester'); |
| 49 | + expect(mockRecord).toHaveBeenCalledTimes(1); |
| 50 | + |
| 51 | + vi.restoreAllMocks(); |
| 52 | + }); |
| 53 | +}); |
0 commit comments