|
| 1 | +Object.defineProperty(globalThis, 'window', { |
| 2 | + value: { |
| 3 | + innerHeight: 0, |
| 4 | + innerWidth: 0 |
| 5 | + } as Partial<Window>, |
| 6 | + writable: true |
| 7 | +}); |
| 8 | + |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { getFloatingProps } from './floating-components'; |
| 11 | + |
| 12 | +describe('getFloatingProps', () => { |
| 13 | + it('calculates the dimensions correctly', () => { |
| 14 | + const element = { |
| 15 | + getBoundingClientRect: () => ({ height: 50, width: 80 }) |
| 16 | + } as HTMLElement; |
| 17 | + const parent = { |
| 18 | + getBoundingClientRect: () => ({ |
| 19 | + top: 10, |
| 20 | + left: 20, |
| 21 | + width: 200, |
| 22 | + height: 100, |
| 23 | + bottom: 110, |
| 24 | + right: 220 |
| 25 | + }) |
| 26 | + } as HTMLElement; |
| 27 | + |
| 28 | + const props = getFloatingProps(element, parent, 'center'); |
| 29 | + expect(props.childWidth).toBe(80); |
| 30 | + expect(props.childHeight).toBe(50); |
| 31 | + expect(props.top).toBe(10); |
| 32 | + expect(props.left).toBe(20); |
| 33 | + expect(props.width).toBe(200); |
| 34 | + expect(props.height).toBe(100); |
| 35 | + expect(props.bottom).toBe(110); |
| 36 | + expect(props.right).toBe(220); |
| 37 | + }); |
| 38 | + |
| 39 | + it('does not throw when element or parent is missing', () => { |
| 40 | + expect(() => |
| 41 | + getFloatingProps( |
| 42 | + null as unknown as HTMLElement, |
| 43 | + {} as HTMLElement, |
| 44 | + 'top' |
| 45 | + ) |
| 46 | + ).not.toThrow(); |
| 47 | + expect(() => |
| 48 | + getFloatingProps( |
| 49 | + {} as HTMLElement, |
| 50 | + null as unknown as HTMLElement, |
| 51 | + 'left' |
| 52 | + ) |
| 53 | + ).not.toThrow(); |
| 54 | + expect( |
| 55 | + getFloatingProps( |
| 56 | + null as unknown as HTMLElement, |
| 57 | + null as unknown as HTMLElement, |
| 58 | + 'bottom' |
| 59 | + ) |
| 60 | + ).toEqual({ |
| 61 | + bottom: 0, |
| 62 | + childHeight: 0, |
| 63 | + childWidth: 0, |
| 64 | + correctedPlacement: 'bottom', |
| 65 | + height: 0, |
| 66 | + innerHeight: 0, |
| 67 | + innerWidth: 0, |
| 68 | + left: 0, |
| 69 | + right: 0, |
| 70 | + top: 0, |
| 71 | + width: 0 |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments