|
| 1 | +import getOffset from "./getOffset"; |
| 2 | +import dom from "../packages/dom"; |
| 3 | + |
| 4 | +const { div } = dom.tags; |
| 5 | + |
| 6 | +const mockBoundingClientRect = ( |
| 7 | + element: HTMLElement, |
| 8 | + domRect: Partial<DOMRect> |
| 9 | +) => { |
| 10 | + const getBoundingClientRectSpy = jest.fn(() => { |
| 11 | + const rect: DOMRect = { |
| 12 | + x: 0, |
| 13 | + y: 0, |
| 14 | + width: 0, |
| 15 | + height: 0, |
| 16 | + top: 0, |
| 17 | + right: 0, |
| 18 | + bottom: 0, |
| 19 | + left: 0, |
| 20 | + toJSON: () => null, |
| 21 | + ...domRect, |
| 22 | + }; |
| 23 | + return rect; |
| 24 | + }); |
| 25 | + element.getBoundingClientRect = getBoundingClientRectSpy; |
| 26 | + |
| 27 | + return element; |
| 28 | +}; |
| 29 | + |
| 30 | +describe("getOffset", () => { |
| 31 | + afterEach(() => { |
| 32 | + document.body.innerHTML = ""; |
| 33 | + document.body.scrollTop = 0; |
| 34 | + document.body.scrollLeft; |
| 35 | + }); |
| 36 | + |
| 37 | + it("should return the correct offset without scroll", () => { |
| 38 | + // Arrange |
| 39 | + const element = mockBoundingClientRect(div(), { |
| 40 | + top: 100, |
| 41 | + left: 10, |
| 42 | + bottom: 350, |
| 43 | + right: 50, |
| 44 | + height: 250, |
| 45 | + }); |
| 46 | + document.body.appendChild(element); |
| 47 | + |
| 48 | + // Act |
| 49 | + const result = getOffset(element); |
| 50 | + |
| 51 | + // Assert |
| 52 | + expect(result).toEqual({ |
| 53 | + absoluteBottom: 350, |
| 54 | + absoluteLeft: 10, |
| 55 | + absoluteRight: 50, |
| 56 | + absoluteTop: 100, |
| 57 | + bottom: 350, |
| 58 | + height: 250, |
| 59 | + left: 10, |
| 60 | + right: 10, |
| 61 | + top: 100, |
| 62 | + width: 0, |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should return the correct offset with scrollTop", () => { |
| 67 | + // Arrange |
| 68 | + const element = mockBoundingClientRect(div(), { |
| 69 | + top: 100, |
| 70 | + left: 10, |
| 71 | + bottom: 350, |
| 72 | + right: 50, |
| 73 | + height: 250, |
| 74 | + }); |
| 75 | + document.body.scrollTop = 150; |
| 76 | + document.body.appendChild(element); |
| 77 | + |
| 78 | + // Act |
| 79 | + const result = getOffset(element); |
| 80 | + |
| 81 | + // Assert |
| 82 | + expect(result).toEqual({ |
| 83 | + absoluteBottom: 350, |
| 84 | + absoluteLeft: 10, |
| 85 | + absoluteRight: 50, |
| 86 | + absoluteTop: 100, |
| 87 | + bottom: 500, |
| 88 | + height: 250, |
| 89 | + left: 10, |
| 90 | + right: 10, |
| 91 | + top: 250, |
| 92 | + width: 0, |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should ignore scroll values when element is fixed", () => { |
| 97 | + // Arrange |
| 98 | + const element = mockBoundingClientRect( |
| 99 | + div({ |
| 100 | + style: "position: fixed", |
| 101 | + }), |
| 102 | + { |
| 103 | + top: 100, |
| 104 | + left: 10, |
| 105 | + bottom: 350, |
| 106 | + right: 50, |
| 107 | + height: 250, |
| 108 | + } |
| 109 | + ); |
| 110 | + document.body.scrollTop = 150; |
| 111 | + document.body.scrollLeft = 100; |
| 112 | + document.body.appendChild(element); |
| 113 | + |
| 114 | + // Act |
| 115 | + const result = getOffset(element); |
| 116 | + |
| 117 | + // Assert |
| 118 | + expect(result).toEqual({ |
| 119 | + absoluteBottom: 350, |
| 120 | + absoluteLeft: 10, |
| 121 | + absoluteRight: 50, |
| 122 | + absoluteTop: 100, |
| 123 | + bottom: 350, |
| 124 | + height: 250, |
| 125 | + left: 10, |
| 126 | + right: 10, |
| 127 | + top: 100, |
| 128 | + width: 0, |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should return position relative to a relative element", () => { |
| 133 | + // Given |
| 134 | + const relativeElement = mockBoundingClientRect( |
| 135 | + div({ |
| 136 | + style: "position: relative", |
| 137 | + }), |
| 138 | + { |
| 139 | + top: 100, |
| 140 | + left: 10, |
| 141 | + bottom: 700, |
| 142 | + right: 50, |
| 143 | + height: 550, |
| 144 | + } |
| 145 | + ); |
| 146 | + |
| 147 | + const element = mockBoundingClientRect(div(), { |
| 148 | + top: 150, |
| 149 | + left: 30, |
| 150 | + bottom: 350, |
| 151 | + right: 150, |
| 152 | + height: 250, |
| 153 | + }); |
| 154 | + |
| 155 | + relativeElement.appendChild(element); |
| 156 | + |
| 157 | + // When |
| 158 | + const result = getOffset(element, relativeElement); |
| 159 | + |
| 160 | + // Then |
| 161 | + expect(result).toEqual({ |
| 162 | + absoluteBottom: 350, |
| 163 | + absoluteLeft: 30, |
| 164 | + absoluteRight: 150, |
| 165 | + absoluteTop: 150, |
| 166 | + bottom: 300, |
| 167 | + height: 250, |
| 168 | + left: 20, |
| 169 | + right: 20, |
| 170 | + top: 50, |
| 171 | + width: 0, |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + it("should return position relative to a sticky element", () => { |
| 176 | + // Given |
| 177 | + const relativeElement = mockBoundingClientRect( |
| 178 | + div({ |
| 179 | + style: "position: sticky", |
| 180 | + }), |
| 181 | + { |
| 182 | + top: 100, |
| 183 | + left: 10, |
| 184 | + bottom: 700, |
| 185 | + right: 50, |
| 186 | + height: 550, |
| 187 | + } |
| 188 | + ); |
| 189 | + |
| 190 | + const element = mockBoundingClientRect(div(), { |
| 191 | + top: 150, |
| 192 | + left: 30, |
| 193 | + bottom: 350, |
| 194 | + right: 150, |
| 195 | + height: 250, |
| 196 | + }); |
| 197 | + |
| 198 | + relativeElement.appendChild(element); |
| 199 | + |
| 200 | + // When |
| 201 | + const result = getOffset(element, relativeElement); |
| 202 | + |
| 203 | + // Then |
| 204 | + expect(result).toEqual({ |
| 205 | + absoluteBottom: 350, |
| 206 | + absoluteLeft: 30, |
| 207 | + absoluteRight: 150, |
| 208 | + absoluteTop: 150, |
| 209 | + bottom: 300, |
| 210 | + height: 250, |
| 211 | + left: 20, |
| 212 | + right: 20, |
| 213 | + top: 50, |
| 214 | + width: 0, |
| 215 | + }); |
| 216 | + }); |
| 217 | +}); |
0 commit comments