|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | + |
| 3 | +describe('refresh-rate-limiter.ts - Interactive Tooltips, Cursor Hovers & Touch Event Propagation', () => { |
| 4 | + beforeEach(() => { |
| 5 | + // Clean up DOM before each test |
| 6 | + document.body.innerHTML = ''; |
| 7 | + }); |
| 8 | + |
| 9 | + it('Trigger simulated mouseenter/hover gestures on active segments or interactive nodes', () => { |
| 10 | + // 1st condition |
| 11 | + const node = document.createElement('div'); |
| 12 | + const mockCallback = vi.fn(); |
| 13 | + node.addEventListener('mouseenter', mockCallback); |
| 14 | + document.body.appendChild(node); |
| 15 | + |
| 16 | + // Simulate hover/mouseenter |
| 17 | + const event = new MouseEvent('mouseenter', { bubbles: true }); |
| 18 | + node.dispatchEvent(event); |
| 19 | + |
| 20 | + expect(mockCallback).toHaveBeenCalledOnce(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('Verify that responsive tooltip layouts display at computed coordinates', () => { |
| 24 | + // 2nd condition |
| 25 | + const tooltip = document.createElement('div'); |
| 26 | + document.body.appendChild(tooltip); |
| 27 | + |
| 28 | + // Function simulating coordinate computation for tooltip |
| 29 | + const showTooltipAt = (element: HTMLElement, x: number, y: number) => { |
| 30 | + element.style.position = 'absolute'; |
| 31 | + element.style.left = `${x}px`; |
| 32 | + element.style.top = `${y}px`; |
| 33 | + element.style.display = 'block'; |
| 34 | + }; |
| 35 | + |
| 36 | + showTooltipAt(tooltip, 250, 120); |
| 37 | + |
| 38 | + expect(tooltip.style.left).toBe('250px'); |
| 39 | + expect(tooltip.style.top).toBe('120px'); |
| 40 | + expect(tooltip.style.display).toBe('block'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('Test custom click/touch gestures and ensure click events propagate correctly', () => { |
| 44 | + // 3rd condition |
| 45 | + const parent = document.createElement('div'); |
| 46 | + const child = document.createElement('button'); |
| 47 | + parent.appendChild(child); |
| 48 | + document.body.appendChild(parent); |
| 49 | + |
| 50 | + const parentClickSpy = vi.fn(); |
| 51 | + const childTouchSpy = vi.fn(); |
| 52 | + |
| 53 | + parent.addEventListener('click', parentClickSpy); |
| 54 | + child.addEventListener('touchstart', childTouchSpy, { passive: true }); |
| 55 | + |
| 56 | + // Simulate touchstart |
| 57 | + const touchEvent = new TouchEvent('touchstart', { bubbles: true }); |
| 58 | + child.dispatchEvent(touchEvent); |
| 59 | + expect(childTouchSpy).toHaveBeenCalledOnce(); |
| 60 | + |
| 61 | + // Simulate click and verify propagation (bubbling) |
| 62 | + const clickEvent = new MouseEvent('click', { bubbles: true }); |
| 63 | + child.dispatchEvent(clickEvent); |
| 64 | + |
| 65 | + expect(parentClickSpy).toHaveBeenCalledOnce(); // Event bubbled successfully to parent |
| 66 | + }); |
| 67 | + |
| 68 | + it('Assert appropriate cursor style classes (like pointer) are applied on hover', () => { |
| 69 | + // 4th condition |
| 70 | + const button = document.createElement('button'); |
| 71 | + button.className = 'btn-default'; |
| 72 | + document.body.appendChild(button); |
| 73 | + |
| 74 | + // Mock class change on hover entering |
| 75 | + button.addEventListener('mouseenter', () => { |
| 76 | + button.classList.add('cursor-pointer', 'hover:bg-gray-100'); |
| 77 | + }); |
| 78 | + |
| 79 | + button.dispatchEvent(new MouseEvent('mouseenter')); |
| 80 | + |
| 81 | + expect(button.classList.contains('cursor-pointer')).toBe(true); |
| 82 | + expect(button.classList.contains('hover:bg-gray-100')).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + it('Check that mouseleave events successfully hide temporary overlay visuals', () => { |
| 86 | + // 5th condition |
| 87 | + const target = document.createElement('div'); |
| 88 | + const overlay = document.createElement('div'); |
| 89 | + |
| 90 | + overlay.id = 'temp-overlay'; |
| 91 | + // Overlay starts out visible |
| 92 | + overlay.style.opacity = '1'; |
| 93 | + overlay.style.visibility = 'visible'; |
| 94 | + |
| 95 | + document.body.appendChild(target); |
| 96 | + document.body.appendChild(overlay); |
| 97 | + |
| 98 | + // Setup listener to hide on leave |
| 99 | + target.addEventListener('mouseleave', () => { |
| 100 | + overlay.style.opacity = '0'; |
| 101 | + overlay.style.visibility = 'hidden'; |
| 102 | + }); |
| 103 | + |
| 104 | + target.dispatchEvent(new MouseEvent('mouseleave')); |
| 105 | + |
| 106 | + expect(overlay.style.opacity).toBe('0'); |
| 107 | + expect(overlay.style.visibility).toBe('hidden'); |
| 108 | + }); |
| 109 | +}); |
0 commit comments