|
1 | | -import { determineAutoPosition, TooltipPosition } from "./tooltipPosition"; |
| 1 | +import { determineAutoPosition } from "../tooltip/tooltipPosition"; |
| 2 | +import type { Offset } from "../../util/getOffset"; |
2 | 3 |
|
3 | | -const positionPrecedence: TooltipPosition[] = [ |
4 | | - "bottom", |
5 | | - "top", |
6 | | - "right", |
7 | | - "left", |
8 | | -]; |
| 4 | +const mockViewport = { width: 1000, height: 800 }; |
9 | 5 |
|
10 | | -describe("placeTooltip", () => { |
11 | | - test("should automatically place the tooltip position when there is enough space", () => { |
12 | | - // Arrange |
13 | | - // Act |
14 | | - const position = determineAutoPosition( |
15 | | - positionPrecedence, |
16 | | - { |
17 | | - top: 200, |
18 | | - left: 200, |
19 | | - height: 100, |
20 | | - width: 100, |
21 | | - right: 300, |
22 | | - bottom: 300, |
23 | | - absoluteTop: 200, |
24 | | - absoluteLeft: 200, |
25 | | - absoluteRight: 300, |
26 | | - absoluteBottom: 300, |
27 | | - }, |
28 | | - 100, |
| 6 | +const makeOffset = ( |
| 7 | + left: number, |
| 8 | + top: number, |
| 9 | + width = 100, |
| 10 | + height = 50 |
| 11 | +): Offset => ({ |
| 12 | + top, |
| 13 | + left, |
| 14 | + width, |
| 15 | + height, |
| 16 | + bottom: top + height, |
| 17 | + right: left + width, |
| 18 | + absoluteTop: top, |
| 19 | + absoluteLeft: left, |
| 20 | + absoluteBottom: top + height, |
| 21 | + absoluteRight: left + width, |
| 22 | +}); |
| 23 | + |
| 24 | +describe("determineAutoPosition", () => { |
| 25 | + it("should return 'bottom-left-aligned' when there is enough space below", () => { |
| 26 | + const target = makeOffset(400, 200); |
| 27 | + const pos = determineAutoPosition( |
| 28 | + ["bottom", "top"], |
| 29 | + target, |
| 30 | + 200, |
29 | 31 | 100, |
30 | | - "top", |
31 | | - { height: 1000, width: 1000 } |
| 32 | + "bottom", |
| 33 | + mockViewport |
32 | 34 | ); |
33 | | - |
34 | | - // Assert |
35 | | - expect(position).toBe("top-right-aligned"); |
| 35 | + expect(pos).toBe("bottom-left-aligned"); |
36 | 36 | }); |
37 | 37 |
|
38 | | - test("should use floating tooltips when height/width is limited", () => { |
39 | | - // Arrange |
40 | | - // Act |
41 | | - const position = determineAutoPosition( |
42 | | - positionPrecedence, |
43 | | - { |
44 | | - top: 0, |
45 | | - left: 0, |
46 | | - height: 100, |
47 | | - width: 100, |
48 | | - right: 0, |
49 | | - bottom: 0, |
50 | | - absoluteTop: 0, |
51 | | - absoluteLeft: 0, |
52 | | - absoluteRight: 0, |
53 | | - absoluteBottom: 0, |
54 | | - }, |
55 | | - 100, |
| 38 | + it("should return 'top-left-aligned' when there is no space below", () => { |
| 39 | + const target = makeOffset(400, 750); |
| 40 | + const pos = determineAutoPosition( |
| 41 | + ["bottom", "top"], |
| 42 | + target, |
| 43 | + 200, |
56 | 44 | 100, |
57 | | - "top", |
58 | | - { height: 100, width: 100 } |
| 45 | + "bottom", |
| 46 | + mockViewport |
59 | 47 | ); |
60 | | - |
61 | | - // Assert |
62 | | - expect(position).toBe("floating"); |
| 48 | + expect(pos).toBe("top-left-aligned"); |
63 | 49 | }); |
64 | 50 |
|
65 | | - test("should use bottom middle aligned when there is enough vertical space", () => { |
66 | | - // Arrange |
67 | | - // Act |
68 | | - const position = determineAutoPosition( |
69 | | - positionPrecedence, |
70 | | - { |
71 | | - top: 0, |
72 | | - left: 0, |
73 | | - height: 100, |
74 | | - width: 100, |
75 | | - right: 0, |
76 | | - bottom: 0, |
77 | | - absoluteTop: 0, |
78 | | - absoluteLeft: 0, |
79 | | - absoluteRight: 0, |
80 | | - absoluteBottom: 0, |
81 | | - }, |
| 51 | + it("should switch to 'left' when right side has no space", () => { |
| 52 | + const target = makeOffset(950, 400); |
| 53 | + const pos = determineAutoPosition( |
| 54 | + ["right", "left", "top", "bottom"], |
| 55 | + target, |
82 | 56 | 100, |
| 57 | + 50, |
| 58 | + "right", |
| 59 | + mockViewport |
| 60 | + ); |
| 61 | + expect(pos).toBe("left"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should fall back to 'floating' when no space anywhere", () => { |
| 65 | + const target = makeOffset(0, 0, 1200, 900); |
| 66 | + const pos = determineAutoPosition( |
| 67 | + ["top", "bottom", "left", "right"], |
| 68 | + target, |
| 69 | + 200, |
83 | 70 | 100, |
84 | | - "left", |
85 | | - { height: 500, width: 100 } |
| 71 | + "bottom", |
| 72 | + mockViewport |
86 | 73 | ); |
| 74 | + expect(pos).toBe("floating"); |
| 75 | + }); |
87 | 76 |
|
88 | | - // Assert |
89 | | - expect(position).toBe("bottom-middle-aligned"); |
| 77 | + it("should respect desired alignment if possible", () => { |
| 78 | + const target = makeOffset(400, 200); |
| 79 | + const pos = determineAutoPosition( |
| 80 | + ["bottom", "top"], |
| 81 | + target, |
| 82 | + 200, |
| 83 | + 100, |
| 84 | + "bottom-right-aligned", |
| 85 | + mockViewport |
| 86 | + ); |
| 87 | + expect(pos).toBe("bottom-right-aligned"); |
90 | 88 | }); |
91 | 89 | }); |
0 commit comments