|
| 1 | +import { createMockEl } from '@test/helpers/mock-element'; |
| 2 | + |
| 3 | +import { positionHoverDropdown } from '@/features/chat/ui/input-toolbar'; |
| 4 | +import { |
| 5 | + HOVER_DROPDOWN_EDGE_INSET, |
| 6 | + placeHoverDropdown, |
| 7 | +} from '@/features/chat/ui/toolbar/hover-dropdown-placement'; |
| 8 | + |
| 9 | +describe('placeHoverDropdown', () => { |
| 10 | + it('centers on the icon when the dropdown fits', () => { |
| 11 | + const placement = placeHoverDropdown(200, 280, 600); |
| 12 | + expect(placement).toEqual({ center: 200, maxWidth: null }); |
| 13 | + }); |
| 14 | + |
| 15 | + it('clamps the center at the leading edge', () => { |
| 16 | + const placement = placeHoverDropdown(80, 320, 600); |
| 17 | + expect(placement).toEqual({ center: HOVER_DROPDOWN_EDGE_INSET + 160, maxWidth: null }); |
| 18 | + }); |
| 19 | + |
| 20 | + it('clamps the center at the trailing edge', () => { |
| 21 | + const placement = placeHoverDropdown(560, 320, 600); |
| 22 | + expect(placement).toEqual({ center: 600 - HOVER_DROPDOWN_EDGE_INSET - 160, maxWidth: null }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('treats an exact fit at both insets as fitting', () => { |
| 26 | + const toolbarWidth = 320 + HOVER_DROPDOWN_EDGE_INSET * 2; |
| 27 | + const placement = placeHoverDropdown(135, 320, toolbarWidth); |
| 28 | + expect(placement).toEqual({ center: HOVER_DROPDOWN_EDGE_INSET + 160, maxWidth: null }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('caps the width and centers when the toolbar is barely narrower than the dropdown', () => { |
| 32 | + const placement = placeHoverDropdown(135, 320, 324); |
| 33 | + expect(placement).toEqual({ center: 162, maxWidth: 324 - HOVER_DROPDOWN_EDGE_INSET * 2 }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('caps the width down to the inset-bounded space in a narrow sidebar', () => { |
| 37 | + const placement = placeHoverDropdown(135, 320, 300); |
| 38 | + expect(placement).toEqual({ center: 150, maxWidth: 284 }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('still centers symmetrically when wider than the whole toolbar', () => { |
| 42 | + const placement = placeHoverDropdown(135, 320, 200); |
| 43 | + expect(placement).toEqual({ center: 100, maxWidth: 184 }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('centers symmetrically when the toolbar is smaller than the insets', () => { |
| 47 | + const placement = placeHoverDropdown(135, 320, 12); |
| 48 | + expect(placement).toEqual({ center: 6, maxWidth: null }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns null for unusable measurements', () => { |
| 52 | + expect(placeHoverDropdown(Number.NaN, 320, 600)).toBeNull(); |
| 53 | + expect(placeHoverDropdown(200, Number.NaN, 600)).toBeNull(); |
| 54 | + expect(placeHoverDropdown(200, 320, Number.NaN)).toBeNull(); |
| 55 | + expect(placeHoverDropdown(200, 0, 600)).toBeNull(); |
| 56 | + expect(placeHoverDropdown(200, 320, 0)).toBeNull(); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('positionHoverDropdown', () => { |
| 61 | + interface RectInit { |
| 62 | + left: number; |
| 63 | + width: number; |
| 64 | + } |
| 65 | + |
| 66 | + const rect = ({ left, width }: RectInit) => ({ |
| 67 | + top: 0, |
| 68 | + left, |
| 69 | + width, |
| 70 | + height: 40, |
| 71 | + right: left + width, |
| 72 | + bottom: 40, |
| 73 | + x: left, |
| 74 | + y: 0, |
| 75 | + toJSON: () => ({}), |
| 76 | + }); |
| 77 | + |
| 78 | + function createTree(toolbar: RectInit, icon: RectInit, dropdownWidth: number) { |
| 79 | + const toolbarEl = createMockEl(); |
| 80 | + const selectorEl = createMockEl(); |
| 81 | + const iconEl = createMockEl(); |
| 82 | + const dropdownEl = createMockEl(); |
| 83 | + |
| 84 | + toolbarEl.getBoundingClientRect = () => rect(toolbar); |
| 85 | + selectorEl.getBoundingClientRect = () => rect({ left: icon.left, width: icon.width }); |
| 86 | + iconEl.getBoundingClientRect = () => rect(icon); |
| 87 | + dropdownEl.getBoundingClientRect = () => rect({ left: 0, width: dropdownWidth }); |
| 88 | + selectorEl.closest = () => toolbarEl; |
| 89 | + |
| 90 | + return { toolbarEl, selectorEl, iconEl, dropdownEl }; |
| 91 | + } |
| 92 | + |
| 93 | + it('keeps the dropdown centered on the icon in a wide toolbar', () => { |
| 94 | + const { selectorEl, iconEl, dropdownEl } = createTree( |
| 95 | + { left: 0, width: 600 }, |
| 96 | + { left: 188, width: 24 }, |
| 97 | + 280, |
| 98 | + ); |
| 99 | + |
| 100 | + positionHoverDropdown(selectorEl, iconEl, dropdownEl); |
| 101 | + |
| 102 | + expect(dropdownEl.style['--qoderian-hover-dropdown-left']).toBe('12px'); |
| 103 | + expect(dropdownEl.style['--qoderian-hover-dropdown-max-width']).toBe(''); |
| 104 | + expect(dropdownEl.style['--qoderian-hover-dropdown-min-width']).toBe(''); |
| 105 | + }); |
| 106 | + |
| 107 | + it('shifts the dropdown inwards when the icon sits near the leading edge', () => { |
| 108 | + const { selectorEl, iconEl, dropdownEl } = createTree( |
| 109 | + { left: 100, width: 324 }, |
| 110 | + { left: 223, width: 24 }, |
| 111 | + 320, |
| 112 | + ); |
| 113 | + |
| 114 | + positionHoverDropdown(selectorEl, iconEl, dropdownEl); |
| 115 | + |
| 116 | + // Center 162 relative to the toolbar -> 39px relative to the selector. |
| 117 | + expect(dropdownEl.style['--qoderian-hover-dropdown-left']).toBe('39px'); |
| 118 | + expect(dropdownEl.style['--qoderian-hover-dropdown-max-width']).toBe('308px'); |
| 119 | + expect(dropdownEl.style['--qoderian-hover-dropdown-min-width']).toBe('0'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('releases a stale width cap once the toolbar fits the natural width again', () => { |
| 123 | + const { selectorEl, iconEl, dropdownEl } = createTree( |
| 124 | + { left: 0, width: 600 }, |
| 125 | + { left: 188, width: 24 }, |
| 126 | + 280, |
| 127 | + ); |
| 128 | + dropdownEl.style['--qoderian-hover-dropdown-min-width'] = '0'; |
| 129 | + dropdownEl.style['--qoderian-hover-dropdown-max-width'] = '200px'; |
| 130 | + |
| 131 | + positionHoverDropdown(selectorEl, iconEl, dropdownEl); |
| 132 | + |
| 133 | + expect(dropdownEl.style['--qoderian-hover-dropdown-min-width']).toBe(''); |
| 134 | + expect(dropdownEl.style['--qoderian-hover-dropdown-max-width']).toBe(''); |
| 135 | + }); |
| 136 | + |
| 137 | + it('skips layout-less shims that cannot report a toolbar rect', () => { |
| 138 | + const selectorEl = createMockEl(); |
| 139 | + const iconEl = createMockEl(); |
| 140 | + const dropdownEl = createMockEl(); |
| 141 | + |
| 142 | + expect(() => positionHoverDropdown(selectorEl, iconEl, dropdownEl)).not.toThrow(); |
| 143 | + expect(dropdownEl.style['--qoderian-hover-dropdown-left']).toBeUndefined(); |
| 144 | + }); |
| 145 | +}); |
0 commit comments