|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { clampMenuPositionToBounds, resolveMenuBounds } from '../menu-position.js'; |
| 3 | + |
| 4 | +const viewport = { left: 0, top: 0, right: 1000, bottom: 800 }; |
| 5 | + |
| 6 | +describe('clampMenuPositionToBounds', () => { |
| 7 | + it('shifts left when the menu overflows the right edge', () => { |
| 8 | + const rect = { left: 900, top: 100, right: 1100, bottom: 300 }; |
| 9 | + expect(clampMenuPositionToBounds({ left: '900px', top: '100px' }, rect, viewport)).toEqual({ |
| 10 | + left: '792px', |
| 11 | + top: '100px', |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + it('shifts up when the menu overflows the bottom edge', () => { |
| 16 | + const rect = { left: 100, top: 700, right: 300, bottom: 900 }; |
| 17 | + expect(clampMenuPositionToBounds({ left: '100px', top: '700px' }, rect, viewport)).toEqual({ |
| 18 | + left: '100px', |
| 19 | + top: '592px', |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('shifts back when the menu is off the left/top edge', () => { |
| 24 | + const rect = { left: -20, top: -10, right: 180, bottom: 190 }; |
| 25 | + expect(clampMenuPositionToBounds({ left: '-20px', top: '-10px' }, rect, viewport)).toEqual({ |
| 26 | + left: '8px', |
| 27 | + top: '8px', |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('leaves a fully in-bounds menu unchanged', () => { |
| 32 | + const rect = { left: 100, top: 100, right: 300, bottom: 300 }; |
| 33 | + expect(clampMenuPositionToBounds({ left: '100px', top: '100px' }, rect, viewport)).toEqual({ |
| 34 | + left: '100px', |
| 35 | + top: '100px', |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('clears the scroll container scrollbar (bounds narrower than viewport)', () => { |
| 40 | + // Bounds right 985 (15px scrollbar): a menu at right 990 shifts to bounds.right - gutter = 977. |
| 41 | + const bounds = { left: 0, top: 0, right: 985, bottom: 760 }; |
| 42 | + const rect = { left: 810, top: 100, right: 990, bottom: 300 }; |
| 43 | + expect(clampMenuPositionToBounds({ left: '810px', top: '100px' }, rect, bounds)).toEqual({ |
| 44 | + left: '797px', |
| 45 | + top: '100px', |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('renders as-is on an axis where the menu is larger than the bounds', () => { |
| 50 | + // 200x320 menu cannot fit in 180x240 bounds; shifting would only trade edges, so leave it. |
| 51 | + const bounds = { left: 0, top: 0, right: 180, bottom: 240 }; |
| 52 | + const rect = { left: 40, top: 30, right: 240, bottom: 350 }; |
| 53 | + expect(clampMenuPositionToBounds({ left: '40px', top: '30px' }, rect, bounds)).toEqual({ |
| 54 | + left: '40px', |
| 55 | + top: '30px', |
| 56 | + }); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('resolveMenuBounds', () => { |
| 61 | + const makeView = (clientW, clientH, computed) => ({ |
| 62 | + document: { documentElement: { clientWidth: clientW, clientHeight: clientH } }, |
| 63 | + getComputedStyle: (el) => computed.get(el) ?? { overflowX: 'visible', overflowY: 'visible' }, |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns the viewport when there is no scrollable ancestor', () => { |
| 67 | + const el = { parentElement: null }; |
| 68 | + const view = makeView(1000, 800, new Map()); |
| 69 | + expect(resolveMenuBounds(el, view)).toEqual({ left: 0, top: 0, right: 1000, bottom: 800 }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('intersects with the scroll container content box (excludes its scrollbar)', () => { |
| 73 | + const scroller = { |
| 74 | + parentElement: null, |
| 75 | + clientWidth: 985, // 15px vertical scrollbar |
| 76 | + clientHeight: 445, |
| 77 | + getBoundingClientRect: () => ({ left: 0, top: 315 }), |
| 78 | + }; |
| 79 | + const anchor = { parentElement: scroller }; |
| 80 | + const computed = new Map([ |
| 81 | + [anchor, { overflowX: 'visible', overflowY: 'visible' }], |
| 82 | + [scroller, { overflowX: 'hidden', overflowY: 'auto' }], |
| 83 | + ]); |
| 84 | + const view = makeView(1000, 760, computed); |
| 85 | + expect(resolveMenuBounds(anchor, view)).toEqual({ left: 0, top: 315, right: 985, bottom: 760 }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments