| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Writable} from 'shared/typeUtils'; |
| b69ab31 | | | 9 | import type {TestingEventBus} from './TestingMessageBus'; |
| b69ab31 | | | 10 | import type { |
| b69ab31 | | | 11 | ClientToServerMessage, |
| b69ab31 | | | 12 | CommitInfo, |
| b69ab31 | | | 13 | Hash, |
| b69ab31 | | | 14 | Result, |
| b69ab31 | | | 15 | ServerToClientMessage, |
| b69ab31 | | | 16 | SmartlogCommits, |
| b69ab31 | | | 17 | UncommittedChanges, |
| b69ab31 | | | 18 | } from './types'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | import {act, screen, waitFor, within} from '@testing-library/react'; |
| b69ab31 | | | 21 | import {nextTick} from 'shared/testUtils'; |
| b69ab31 | | | 22 | import platform from './platform'; |
| b69ab31 | | | 23 | import {deserializeFromString, serializeToString} from './serialize'; |
| b69ab31 | | | 24 | import {mostRecentSubscriptionIds} from './serverAPIState'; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | const testMessageBus = platform.messageBus as TestingEventBus; |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | export function simulateMessageFromServer(message: ServerToClientMessage): void { |
| b69ab31 | | | 29 | testMessageBus.simulateMessage(serializeToString(message)); |
| b69ab31 | | | 30 | } |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | /** Filter out binary messages, and filter by wanted type. */ |
| b69ab31 | | | 33 | function filterMessages<K extends string>(wantedType?: K) { |
| b69ab31 | | | 34 | let messages = testMessageBus.sent |
| b69ab31 | | | 35 | .filter((msg: unknown): msg is string => !(msg instanceof ArrayBuffer)) |
| b69ab31 | | | 36 | .map(deserializeFromString) as Array<ClientToServerMessage>; |
| b69ab31 | | | 37 | if (wantedType != null) { |
| b69ab31 | | | 38 | messages = messages.filter(msg => msg.type == null || msg.type === wantedType); |
| b69ab31 | | | 39 | } |
| b69ab31 | | | 40 | return messages; |
| b69ab31 | | | 41 | } |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | export function expectMessageSentToServer(message: Partial<ClientToServerMessage>): void { |
| b69ab31 | | | 44 | expect(filterMessages(message.type)).toContainEqual(message); |
| b69ab31 | | | 45 | } |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | export function getLastMessageOfTypeSentToServer<K extends string>( |
| b69ab31 | | | 48 | wantedType: K, |
| b69ab31 | | | 49 | ): (ClientToServerMessage & {type: K}) | undefined { |
| b69ab31 | | | 50 | const values = filterMessages(wantedType) as Array<ClientToServerMessage & {type: K}>; |
| b69ab31 | | | 51 | return values[values.length - 1]; |
| b69ab31 | | | 52 | } |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | export function expectMessageNOTSentToServer(message: Partial<ClientToServerMessage>): void { |
| b69ab31 | | | 55 | expect(filterMessages(message.type)).not.toContainEqual(message); |
| b69ab31 | | | 56 | } |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | /** |
| b69ab31 | | | 59 | * Return last `num` stringified JSON messages sent to the server. |
| b69ab31 | | | 60 | */ |
| b69ab31 | | | 61 | export function getLastMessagesSentToServer(num: number): Array<string> { |
| b69ab31 | | | 62 | return testMessageBus.sent.slice(-num); |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | export function simulateServerDisconnected(): void { |
| b69ab31 | | | 66 | testMessageBus.simulateServerStatusChange({type: 'error', error: 'server disconnected'}); |
| b69ab31 | | | 67 | } |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | export function simulateCommits(commits: Result<SmartlogCommits>) { |
| b69ab31 | | | 70 | simulateMessageFromServer({ |
| b69ab31 | | | 71 | type: 'subscriptionResult', |
| b69ab31 | | | 72 | kind: 'smartlogCommits', |
| b69ab31 | | | 73 | subscriptionID: mostRecentSubscriptionIds.smartlogCommits, |
| b69ab31 | | | 74 | data: { |
| b69ab31 | | | 75 | fetchStartTimestamp: 1, |
| b69ab31 | | | 76 | fetchCompletedTimestamp: 2, |
| b69ab31 | | | 77 | commits, |
| b69ab31 | | | 78 | }, |
| b69ab31 | | | 79 | }); |
| b69ab31 | | | 80 | } |
| b69ab31 | | | 81 | export function simulateUncommittedChangedFiles(files: Result<UncommittedChanges>) { |
| b69ab31 | | | 82 | simulateMessageFromServer({ |
| b69ab31 | | | 83 | type: 'subscriptionResult', |
| b69ab31 | | | 84 | kind: 'uncommittedChanges', |
| b69ab31 | | | 85 | subscriptionID: mostRecentSubscriptionIds.uncommittedChanges, |
| b69ab31 | | | 86 | data: { |
| b69ab31 | | | 87 | fetchStartTimestamp: 1, |
| b69ab31 | | | 88 | fetchCompletedTimestamp: 2, |
| b69ab31 | | | 89 | files, |
| b69ab31 | | | 90 | }, |
| b69ab31 | | | 91 | }); |
| b69ab31 | | | 92 | } |
| b69ab31 | | | 93 | export function simulateRepoConnected(repoRoot?: string, cwd?: string) { |
| b69ab31 | | | 94 | const root = repoRoot ?? '/path/to/repo'; |
| b69ab31 | | | 95 | simulateMessageFromServer({ |
| b69ab31 | | | 96 | type: 'repoInfo', |
| b69ab31 | | | 97 | info: { |
| b69ab31 | | | 98 | type: 'success', |
| b69ab31 | | | 99 | repoRoot: root, |
| b69ab31 | | | 100 | dotdir: `${root}/.sl`, |
| b69ab31 | | | 101 | command: 'sl', |
| b69ab31 | | | 102 | pullRequestDomain: undefined, |
| b69ab31 | | | 103 | codeReviewSystem: {type: 'github', owner: 'owner', repo: 'repo', hostname: 'github.com'}, |
| b69ab31 | | | 104 | isEdenFs: false, |
| b69ab31 | | | 105 | }, |
| b69ab31 | | | 106 | cwd, |
| b69ab31 | | | 107 | }); |
| b69ab31 | | | 108 | testMessageBus.simulateServerStatusChange({type: 'open'}); |
| b69ab31 | | | 109 | } |
| b69ab31 | | | 110 | |
| b69ab31 | | | 111 | export function resetTestMessages() { |
| b69ab31 | | | 112 | act(() => { |
| b69ab31 | | | 113 | testMessageBus.resetTestMessages(); |
| b69ab31 | | | 114 | }); |
| b69ab31 | | | 115 | } |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | export function commitInfoIsOpen(): boolean { |
| b69ab31 | | | 118 | return ( |
| b69ab31 | | | 119 | screen.queryByTestId('commit-info-view') != null || |
| b69ab31 | | | 120 | screen.queryByTestId('commit-info-view-loading') != null |
| b69ab31 | | | 121 | ); |
| b69ab31 | | | 122 | } |
| b69ab31 | | | 123 | |
| b69ab31 | | | 124 | export function closeCommitInfoSidebar() { |
| b69ab31 | | | 125 | if (!commitInfoIsOpen()) { |
| b69ab31 | | | 126 | return; |
| b69ab31 | | | 127 | } |
| b69ab31 | | | 128 | screen.queryAllByTestId('drawer-label').forEach(el => { |
| b69ab31 | | | 129 | const commitInfoTab = within(el).queryByText('Commit Info'); |
| b69ab31 | | | 130 | commitInfoTab?.click(); |
| b69ab31 | | | 131 | }); |
| b69ab31 | | | 132 | } |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | export function openCommitInfoSidebar() { |
| b69ab31 | | | 135 | if (commitInfoIsOpen()) { |
| b69ab31 | | | 136 | return; |
| b69ab31 | | | 137 | } |
| b69ab31 | | | 138 | screen.queryAllByTestId('drawer-label').forEach(el => { |
| b69ab31 | | | 139 | const commitInfoTab = within(el).queryByText('Commit Info'); |
| b69ab31 | | | 140 | commitInfoTab?.click(); |
| b69ab31 | | | 141 | }); |
| b69ab31 | | | 142 | } |
| b69ab31 | | | 143 | |
| b69ab31 | | | 144 | export const emptyCommit: CommitInfo = { |
| b69ab31 | | | 145 | title: 'title', |
| b69ab31 | | | 146 | hash: '0', |
| b69ab31 | | | 147 | parents: [], |
| b69ab31 | | | 148 | grandparents: [], |
| b69ab31 | | | 149 | phase: 'draft', |
| b69ab31 | | | 150 | isDot: false, |
| b69ab31 | | | 151 | author: 'author', |
| b69ab31 | | | 152 | date: new Date(), |
| b69ab31 | | | 153 | description: '', |
| b69ab31 | | | 154 | bookmarks: [], |
| b69ab31 | | | 155 | remoteBookmarks: [], |
| b69ab31 | | | 156 | filePathsSample: [], |
| b69ab31 | | | 157 | totalFileCount: 0, |
| b69ab31 | | | 158 | maxCommonPathPrefix: '', |
| b69ab31 | | | 159 | }; |
| b69ab31 | | | 160 | |
| b69ab31 | | | 161 | export function COMMIT( |
| b69ab31 | | | 162 | hash: string, |
| b69ab31 | | | 163 | title: string, |
| b69ab31 | | | 164 | parent: Hash, |
| b69ab31 | | | 165 | info?: Partial<CommitInfo>, |
| b69ab31 | | | 166 | ): CommitInfo { |
| b69ab31 | | | 167 | return { |
| b69ab31 | | | 168 | ...emptyCommit, |
| b69ab31 | | | 169 | ...info, |
| b69ab31 | | | 170 | hash, |
| b69ab31 | | | 171 | title, |
| b69ab31 | | | 172 | parents: [parent], |
| b69ab31 | | | 173 | }; |
| b69ab31 | | | 174 | } |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | /** |
| b69ab31 | | | 177 | ``` |
| b69ab31 | | | 178 | | z - Commit Z |
| b69ab31 | | | 179 | | | |
| b69ab31 | | | 180 | | y - Commit Y |
| b69ab31 | | | 181 | | | |
| b69ab31 | | | 182 | | x - Commit X |
| b69ab31 | | | 183 | |/ |
| b69ab31 | | | 184 | 2 - another public branch (remote/master) |
| b69ab31 | | | 185 | | |
| b69ab31 | | | 186 | | e - Commit E (You are here) |
| b69ab31 | | | 187 | | | |
| b69ab31 | | | 188 | | d - Commit D |
| b69ab31 | | | 189 | | | |
| b69ab31 | | | 190 | | c - Commit C |
| b69ab31 | | | 191 | | | |
| b69ab31 | | | 192 | | b - Commit B |
| b69ab31 | | | 193 | | | |
| b69ab31 | | | 194 | | a - Commit A |
| b69ab31 | | | 195 | |/ |
| b69ab31 | | | 196 | 1 - some public base |
| b69ab31 | | | 197 | ``` |
| b69ab31 | | | 198 | */ |
| b69ab31 | | | 199 | export const TEST_COMMIT_HISTORY = [ |
| b69ab31 | | | 200 | COMMIT('z', 'Commit Z', 'y'), |
| b69ab31 | | | 201 | COMMIT('y', 'Commit Y', 'x'), |
| b69ab31 | | | 202 | COMMIT('x', 'Commit X', '2'), |
| b69ab31 | | | 203 | COMMIT('2', 'another public branch', '0', {phase: 'public', remoteBookmarks: ['remote/master']}), |
| b69ab31 | | | 204 | COMMIT('e', 'Commit E', 'd', {isDot: true}), |
| b69ab31 | | | 205 | COMMIT('d', 'Commit D', 'c'), |
| b69ab31 | | | 206 | COMMIT('c', 'Commit C', 'b'), |
| b69ab31 | | | 207 | COMMIT('b', 'Commit B', 'a'), |
| b69ab31 | | | 208 | COMMIT('a', 'Commit A', '1'), |
| b69ab31 | | | 209 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| b69ab31 | | | 210 | ]; |
| b69ab31 | | | 211 | |
| b69ab31 | | | 212 | export const fireMouseEvent = function ( |
| b69ab31 | | | 213 | type: string, |
| b69ab31 | | | 214 | elem: EventTarget, |
| b69ab31 | | | 215 | centerX: number, |
| b69ab31 | | | 216 | centerY: number, |
| b69ab31 | | | 217 | additionalProperties?: Partial<MouseEvent | InputEvent>, |
| b69ab31 | | | 218 | ) { |
| b69ab31 | | | 219 | const evt = document.createEvent('MouseEvents') as Writable<MouseEvent & InputEvent>; |
| b69ab31 | | | 220 | evt.initMouseEvent( |
| b69ab31 | | | 221 | type, |
| b69ab31 | | | 222 | true, |
| b69ab31 | | | 223 | true, |
| b69ab31 | | | 224 | window, |
| b69ab31 | | | 225 | 1, |
| b69ab31 | | | 226 | 1, |
| b69ab31 | | | 227 | 1, |
| b69ab31 | | | 228 | centerX, |
| b69ab31 | | | 229 | centerY, |
| b69ab31 | | | 230 | false, |
| b69ab31 | | | 231 | false, |
| b69ab31 | | | 232 | false, |
| b69ab31 | | | 233 | false, |
| b69ab31 | | | 234 | 0, |
| b69ab31 | | | 235 | elem, |
| b69ab31 | | | 236 | ); |
| b69ab31 | | | 237 | evt.dataTransfer = {} as DataTransfer; |
| b69ab31 | | | 238 | if (additionalProperties != null) { |
| b69ab31 | | | 239 | for (const [key, value] of Object.entries(additionalProperties)) { |
| b69ab31 | | | 240 | (evt as Record<string, unknown>)[key] = value; |
| b69ab31 | | | 241 | } |
| b69ab31 | | | 242 | } |
| b69ab31 | | | 243 | return elem.dispatchEvent(evt); |
| b69ab31 | | | 244 | }; |
| b69ab31 | | | 245 | |
| b69ab31 | | | 246 | export const drag = (elemDrag: HTMLElement, elemDrop: HTMLElement) => { |
| b69ab31 | | | 247 | act(() => { |
| b69ab31 | | | 248 | // calculate positions |
| b69ab31 | | | 249 | let pos = elemDrag.getBoundingClientRect(); |
| b69ab31 | | | 250 | const center1X = Math.floor((pos.left + pos.right) / 2); |
| b69ab31 | | | 251 | const center1Y = Math.floor((pos.top + pos.bottom) / 2); |
| b69ab31 | | | 252 | |
| b69ab31 | | | 253 | pos = elemDrop.getBoundingClientRect(); |
| b69ab31 | | | 254 | const center2X = Math.floor((pos.left + pos.right) / 2); |
| b69ab31 | | | 255 | const center2Y = Math.floor((pos.top + pos.bottom) / 2); |
| b69ab31 | | | 256 | |
| b69ab31 | | | 257 | // mouse over dragged element and mousedown |
| b69ab31 | | | 258 | fireMouseEvent('mousemove', elemDrag, center1X, center1Y); |
| b69ab31 | | | 259 | fireMouseEvent('mouseenter', elemDrag, center1X, center1Y); |
| b69ab31 | | | 260 | fireMouseEvent('mouseover', elemDrag, center1X, center1Y); |
| b69ab31 | | | 261 | fireMouseEvent('mousedown', elemDrag, center1X, center1Y); |
| b69ab31 | | | 262 | |
| b69ab31 | | | 263 | if (!elemDrag.draggable) { |
| b69ab31 | | | 264 | return; |
| b69ab31 | | | 265 | } |
| b69ab31 | | | 266 | |
| b69ab31 | | | 267 | // start dragging process over to drop target |
| b69ab31 | | | 268 | const dragStarted = fireMouseEvent('dragstart', elemDrag, center1X, center1Y); |
| b69ab31 | | | 269 | if (!dragStarted) { |
| b69ab31 | | | 270 | return; |
| b69ab31 | | | 271 | } |
| b69ab31 | | | 272 | |
| b69ab31 | | | 273 | fireMouseEvent('drag', elemDrag, center1X, center1Y); |
| b69ab31 | | | 274 | fireMouseEvent('mousemove', elemDrag, center1X, center1Y); |
| b69ab31 | | | 275 | fireMouseEvent('drag', elemDrag, center2X, center2Y); |
| b69ab31 | | | 276 | fireMouseEvent('mousemove', elemDrop, center2X, center2Y); |
| b69ab31 | | | 277 | |
| b69ab31 | | | 278 | // trigger dragging process on top of drop target |
| b69ab31 | | | 279 | fireMouseEvent('mouseenter', elemDrop, center2X, center2Y); |
| b69ab31 | | | 280 | fireMouseEvent('dragenter', elemDrop, center2X, center2Y); |
| b69ab31 | | | 281 | fireMouseEvent('mouseover', elemDrop, center2X, center2Y); |
| b69ab31 | | | 282 | fireMouseEvent('dragover', elemDrop, center2X, center2Y); |
| b69ab31 | | | 283 | }); |
| b69ab31 | | | 284 | }; |
| b69ab31 | | | 285 | export const drop = (elemDrag: HTMLElement, elemDrop: HTMLElement) => { |
| b69ab31 | | | 286 | act(() => { |
| b69ab31 | | | 287 | // calculate positions |
| b69ab31 | | | 288 | let pos = elemDrag.getBoundingClientRect(); |
| b69ab31 | | | 289 | pos = elemDrop.getBoundingClientRect(); |
| b69ab31 | | | 290 | const center2X = Math.floor((pos.left + pos.right) / 2); |
| b69ab31 | | | 291 | const center2Y = Math.floor((pos.top + pos.bottom) / 2); |
| b69ab31 | | | 292 | |
| b69ab31 | | | 293 | // release dragged element on top of drop target |
| b69ab31 | | | 294 | fireMouseEvent('drop', elemDrop, center2X, center2Y); |
| b69ab31 | | | 295 | fireMouseEvent('dragend', elemDrag, center2X, center2Y); |
| b69ab31 | | | 296 | fireMouseEvent('mouseup', elemDrag, center2X, center2Y); |
| b69ab31 | | | 297 | }); |
| b69ab31 | | | 298 | }; |
| b69ab31 | | | 299 | |
| b69ab31 | | | 300 | // See https://github.com/testing-library/user-event/issues/440 |
| b69ab31 | | | 301 | export const dragAndDrop = (elemDrag: HTMLElement, elemDrop: HTMLElement) => { |
| b69ab31 | | | 302 | drag(elemDrag, elemDrop); |
| b69ab31 | | | 303 | drop(elemDrag, elemDrop); |
| b69ab31 | | | 304 | }; |
| b69ab31 | | | 305 | |
| b69ab31 | | | 306 | export function dragAndDropCommits( |
| b69ab31 | | | 307 | draggedCommit: Hash | HTMLElement, |
| b69ab31 | | | 308 | onto: Hash, |
| b69ab31 | | | 309 | op: typeof dragAndDrop | typeof drag | typeof drop = dragAndDrop, |
| b69ab31 | | | 310 | ) { |
| b69ab31 | | | 311 | const draggableCommit = |
| b69ab31 | | | 312 | typeof draggedCommit !== 'string' |
| b69ab31 | | | 313 | ? draggedCommit |
| b69ab31 | | | 314 | : within(screen.getByTestId(`commit-${draggedCommit}`)).queryByTestId('draggable-commit'); |
| b69ab31 | | | 315 | expect(draggableCommit).toBeDefined(); |
| b69ab31 | | | 316 | const dragTargetCommit = screen.queryByTestId(`commit-${onto}`)?.querySelector('.commit-details'); |
| b69ab31 | | | 317 | expect(dragTargetCommit).toBeDefined(); |
| b69ab31 | | | 318 | |
| b69ab31 | | | 319 | act(() => { |
| b69ab31 | | | 320 | op(draggableCommit as HTMLElement, dragTargetCommit as HTMLElement); |
| b69ab31 | | | 321 | jest.advanceTimersByTime(2); |
| b69ab31 | | | 322 | }); |
| b69ab31 | | | 323 | } |
| b69ab31 | | | 324 | |
| b69ab31 | | | 325 | export function dragCommits(draggedCommit: Hash | HTMLElement, onto: Hash) { |
| b69ab31 | | | 326 | dragAndDropCommits(draggedCommit, onto, drag); |
| b69ab31 | | | 327 | } |
| b69ab31 | | | 328 | |
| b69ab31 | | | 329 | export function dropCommits(draggedCommit: Hash | HTMLElement, onto: Hash) { |
| b69ab31 | | | 330 | dragAndDropCommits(draggedCommit, onto, drop); |
| b69ab31 | | | 331 | } |
| b69ab31 | | | 332 | |
| b69ab31 | | | 333 | /** Check that YouAreHere points to the given commit. */ |
| b69ab31 | | | 334 | export function expectYouAreHerePointAt(hash: string) { |
| b69ab31 | | | 335 | // The previous row of "hash" should be "YouAreHere". |
| b69ab31 | | | 336 | // YouAreHere |
| b69ab31 | | | 337 | // / |
| b69ab31 | | | 338 | // hash |
| b69ab31 | | | 339 | const row = screen.getByTestId(`dag-row-group-${hash}`); |
| b69ab31 | | | 340 | const previousRow = row.previousElementSibling; |
| b69ab31 | | | 341 | expect(previousRow).toHaveTextContent('You are here'); |
| b69ab31 | | | 342 | } |
| b69ab31 | | | 343 | |
| b69ab31 | | | 344 | /** |
| b69ab31 | | | 345 | * Despite catching the error in our error boundary, react + jsdom still |
| b69ab31 | | | 346 | * print big scary messages to console.warn when we throw an error. |
| b69ab31 | | | 347 | * We can ignore these during the select tests that test error boundaries. |
| b69ab31 | | | 348 | * This should be done only when needed, to prevent filtering out useful |
| b69ab31 | | | 349 | * console.error statements. |
| b69ab31 | | | 350 | * See also: https://github.com/facebook/react/issues/11098#issuecomment-412682721 |
| b69ab31 | | | 351 | */ |
| b69ab31 | | | 352 | export function suppressReactErrorBoundaryErrorMessages() { |
| b69ab31 | | | 353 | beforeAll(() => { |
| b69ab31 | | | 354 | jest.spyOn(console, 'error').mockImplementation(() => undefined); |
| b69ab31 | | | 355 | }); |
| b69ab31 | | | 356 | afterAll(() => { |
| b69ab31 | | | 357 | jest.restoreAllMocks(); |
| b69ab31 | | | 358 | }); |
| b69ab31 | | | 359 | } |
| b69ab31 | | | 360 | |
| b69ab31 | | | 361 | /** |
| b69ab31 | | | 362 | * Print test name beforeEach. This can be useful to figure out which test prints |
| b69ab31 | | | 363 | * React or testing-library warnings, if the stack trace does not include the test |
| b69ab31 | | | 364 | * code. |
| b69ab31 | | | 365 | */ |
| b69ab31 | | | 366 | export function beforeEachPrintTestName() { |
| b69ab31 | | | 367 | beforeEach(() => { |
| b69ab31 | | | 368 | // eslint-disable-next-line no-console |
| b69ab31 | | | 369 | console.log(`Starting test: ${expect.getState().currentTestName}`); |
| b69ab31 | | | 370 | }); |
| b69ab31 | | | 371 | } |
| b69ab31 | | | 372 | |
| b69ab31 | | | 373 | /** |
| b69ab31 | | | 374 | * Drop-in replacement of `waitFor` that includes a `nextTick` to workaround |
| b69ab31 | | | 375 | * some `act()` warnings. |
| b69ab31 | | | 376 | */ |
| b69ab31 | | | 377 | export function waitForWithTick<T>(callback: () => Promise<T> | T): Promise<T> { |
| b69ab31 | | | 378 | return waitFor(async () => { |
| b69ab31 | | | 379 | await nextTick(); |
| b69ab31 | | | 380 | return callback(); |
| b69ab31 | | | 381 | }); |
| b69ab31 | | | 382 | } |
| b69ab31 | | | 383 | |
| b69ab31 | | | 384 | /** |
| b69ab31 | | | 385 | * Check that the "commit" is in the forked branch from "base". |
| b69ab31 | | | 386 | * |
| b69ab31 | | | 387 | * o <- does not return commit hashes here (not right-side) |
| b69ab31 | | | 388 | * : |
| b69ab31 | | | 389 | * | ┌─────┐ |
| b69ab31 | | | 390 | * | │ o │ |
| b69ab31 | | | 391 | * | │ : │ |
| b69ab31 | | | 392 | * | │ o │ <- return commit hashes here (right-side branch) |
| b69ab31 | | | 393 | * | │ | │ |
| b69ab31 | | | 394 | * | │ o │ |
| b69ab31 | | | 395 | * | │ / │ |
| b69ab31 | | | 396 | * | └─────┘ |
| b69ab31 | | | 397 | * | / |
| b69ab31 | | | 398 | * base |
| b69ab31 | | | 399 | * |
| b69ab31 | | | 400 | * This is a naive implementation that does not consider merges. |
| b69ab31 | | | 401 | */ |
| b69ab31 | | | 402 | export function scanForkedBranchHashes(base: string): string[] { |
| b69ab31 | | | 403 | const baseRow = screen.getByTestId(`dag-row-group-${base}`); |
| b69ab31 | | | 404 | const getAttr = (e: Element, attr: string) => e.querySelector(`[${attr}]`)?.getAttribute(attr); |
| b69ab31 | | | 405 | const getNodeColumn = (row: Element) => parseInt(getAttr(row, 'data-nodecolumn') ?? '-1'); |
| b69ab31 | | | 406 | const baseIndent = getNodeColumn(baseRow); |
| b69ab31 | | | 407 | // Scan rows above baseRow. |
| b69ab31 | | | 408 | let prevRow = baseRow.previousElementSibling; |
| b69ab31 | | | 409 | const branchHashes = []; |
| b69ab31 | | | 410 | while (prevRow) { |
| b69ab31 | | | 411 | const prevIndent = getNodeColumn(prevRow); |
| b69ab31 | | | 412 | if (prevIndent <= baseIndent) { |
| b69ab31 | | | 413 | // No longer right-side branch from 'base'. |
| b69ab31 | | | 414 | break; |
| b69ab31 | | | 415 | } |
| b69ab31 | | | 416 | const hash = getAttr(prevRow, 'data-commit-hash'); |
| b69ab31 | | | 417 | if (hash) { |
| b69ab31 | | | 418 | branchHashes.push(hash); |
| b69ab31 | | | 419 | } |
| b69ab31 | | | 420 | prevRow = prevRow.previousElementSibling; |
| b69ab31 | | | 421 | } |
| b69ab31 | | | 422 | return branchHashes; |
| b69ab31 | | | 423 | } |