| 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 {Hash} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {act, fireEvent, screen, waitFor, within} from '@testing-library/react'; |
| b69ab31 | | | 11 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 12 | import {commitMessageFieldsSchema} from './CommitInfoView/CommitMessageFields'; |
| b69ab31 | | | 13 | import {OSSCommitMessageFieldSchema} from './CommitInfoView/OSSCommitMessageFieldsSchema'; |
| b69ab31 | | | 14 | import {convertFieldNameToKey} from './CommitInfoView/utils'; |
| b69ab31 | | | 15 | import {readAtom} from './jotaiUtils'; |
| b69ab31 | | | 16 | import {individualToggleKey} from './selection'; |
| b69ab31 | | | 17 | import {expectMessageSentToServer} from './testUtils'; |
| b69ab31 | | | 18 | import {assert} from './utils'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | export const CommitTreeListTestUtils = { |
| b69ab31 | | | 21 | withinCommitTree() { |
| b69ab31 | | | 22 | return within(screen.getByTestId('commit-tree-root')); |
| b69ab31 | | | 23 | }, |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | async clickGoto(commit: Hash) { |
| b69ab31 | | | 26 | const myCommit = screen.queryByTestId(`commit-${commit}`); |
| b69ab31 | | | 27 | const gotoButton = myCommit?.querySelector('.goto-button button'); |
| b69ab31 | | | 28 | expect(gotoButton).toBeDefined(); |
| b69ab31 | | | 29 | await act(async () => { |
| b69ab31 | | | 30 | await fireEvent.click(gotoButton as Element); |
| b69ab31 | | | 31 | }); |
| b69ab31 | | | 32 | }, |
| b69ab31 | | | 33 | }; |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | export const CommitInfoTestUtils = { |
| b69ab31 | | | 36 | withinCommitInfo() { |
| b69ab31 | | | 37 | return within(screen.getByTestId('commit-info-view')); |
| b69ab31 | | | 38 | }, |
| b69ab31 | | | 39 | |
| b69ab31 | | | 40 | withinCommitActionBar() { |
| b69ab31 | | | 41 | return within(screen.getByTestId('commit-info-actions-bar')); |
| b69ab31 | | | 42 | }, |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | openCommitInfoSidebar() { |
| b69ab31 | | | 45 | screen.queryAllByTestId('drawer-label').forEach(el => { |
| b69ab31 | | | 46 | const commitInfoTab = within(el).queryByText('Commit Info'); |
| b69ab31 | | | 47 | commitInfoTab?.click(); |
| b69ab31 | | | 48 | }); |
| b69ab31 | | | 49 | }, |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | clickToSelectCommit(hash: string, cmdClick?: boolean) { |
| b69ab31 | | | 52 | const commit = within(screen.getByTestId(`commit-${hash}`)).queryByTestId('draggable-commit'); |
| b69ab31 | | | 53 | expect(commit).toBeInTheDocument(); |
| b69ab31 | | | 54 | act(() => { |
| b69ab31 | | | 55 | fireEvent.click(nullthrows(commit), {[individualToggleKey]: cmdClick === true}); |
| b69ab31 | | | 56 | }); |
| b69ab31 | | | 57 | }, |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | clickCommitMode() { |
| b69ab31 | | | 60 | const commitRadioChoice = within(screen.getByTestId('commit-info-toolbar-top')).getByText( |
| b69ab31 | | | 61 | 'Commit', |
| b69ab31 | | | 62 | ); |
| b69ab31 | | | 63 | act(() => { |
| b69ab31 | | | 64 | fireEvent.click(commitRadioChoice); |
| b69ab31 | | | 65 | }); |
| b69ab31 | | | 66 | }, |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | clickAmendMode() { |
| b69ab31 | | | 69 | const commitRadioChoice = within(screen.getByTestId('commit-info-toolbar-top')).getByText( |
| b69ab31 | | | 70 | 'Amend', |
| b69ab31 | | | 71 | ); |
| b69ab31 | | | 72 | act(() => { |
| b69ab31 | | | 73 | fireEvent.click(commitRadioChoice); |
| b69ab31 | | | 74 | }); |
| b69ab31 | | | 75 | }, |
| b69ab31 | | | 76 | |
| b69ab31 | | | 77 | async clickAmendButton() { |
| b69ab31 | | | 78 | const amendButton: HTMLButtonElement | null = within( |
| b69ab31 | | | 79 | screen.getByTestId('commit-info-actions-bar'), |
| b69ab31 | | | 80 | ).queryByText('Amend'); |
| b69ab31 | | | 81 | expect(amendButton).toBeInTheDocument(); |
| b69ab31 | | | 82 | act(() => { |
| b69ab31 | | | 83 | fireEvent.click(nullthrows(amendButton)); |
| b69ab31 | | | 84 | }); |
| b69ab31 | | | 85 | await waitFor(() => |
| b69ab31 | | | 86 | expectMessageSentToServer({ |
| b69ab31 | | | 87 | type: 'runOperation', |
| b69ab31 | | | 88 | operation: expect.objectContaining({ |
| b69ab31 | | | 89 | args: expect.arrayContaining(['amend']), |
| b69ab31 | | | 90 | }), |
| b69ab31 | | | 91 | }), |
| b69ab31 | | | 92 | ); |
| b69ab31 | | | 93 | }, |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | clickAmendMessageButton() { |
| b69ab31 | | | 96 | const amendMessageButton: HTMLButtonElement | null = within( |
| b69ab31 | | | 97 | screen.getByTestId('commit-info-actions-bar'), |
| b69ab31 | | | 98 | ).queryByText('Amend Message'); |
| b69ab31 | | | 99 | expect(amendMessageButton).toBeInTheDocument(); |
| b69ab31 | | | 100 | act(() => { |
| b69ab31 | | | 101 | fireEvent.click(nullthrows(amendMessageButton)); |
| b69ab31 | | | 102 | }); |
| b69ab31 | | | 103 | }, |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | async clickCommitButton() { |
| b69ab31 | | | 106 | const commitButton: HTMLButtonElement | null = within( |
| b69ab31 | | | 107 | screen.getByTestId('commit-info-actions-bar'), |
| b69ab31 | | | 108 | ).queryByText('Commit'); |
| b69ab31 | | | 109 | expect(commitButton).toBeInTheDocument(); |
| b69ab31 | | | 110 | act(() => { |
| b69ab31 | | | 111 | fireEvent.click(nullthrows(commitButton)); |
| b69ab31 | | | 112 | }); |
| b69ab31 | | | 113 | await waitFor(() => |
| b69ab31 | | | 114 | expectMessageSentToServer({ |
| b69ab31 | | | 115 | type: 'runOperation', |
| b69ab31 | | | 116 | operation: expect.objectContaining({ |
| b69ab31 | | | 117 | args: expect.arrayContaining(['commit']), |
| b69ab31 | | | 118 | }), |
| b69ab31 | | | 119 | }), |
| b69ab31 | | | 120 | ); |
| b69ab31 | | | 121 | }, |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | clickCancel() { |
| b69ab31 | | | 124 | const cancelButton: HTMLButtonElement | null = |
| b69ab31 | | | 125 | CommitInfoTestUtils.withinCommitInfo().queryByText('Cancel'); |
| b69ab31 | | | 126 | expect(cancelButton).toBeInTheDocument(); |
| b69ab31 | | | 127 | |
| b69ab31 | | | 128 | act(() => { |
| b69ab31 | | | 129 | fireEvent.click(nullthrows(cancelButton)); |
| b69ab31 | | | 130 | }); |
| b69ab31 | | | 131 | }, |
| b69ab31 | | | 132 | |
| b69ab31 | | | 133 | /** Get the textarea for the title editor */ |
| b69ab31 | | | 134 | getTitleEditor(): HTMLTextAreaElement { |
| b69ab31 | | | 135 | const title = screen.getByTestId('commit-info-title-field') as HTMLTextAreaElement; |
| b69ab31 | | | 136 | expect(title).toBeInTheDocument(); |
| b69ab31 | | | 137 | return title; |
| b69ab31 | | | 138 | }, |
| b69ab31 | | | 139 | |
| b69ab31 | | | 140 | /** Get the textarea for the description editor |
| b69ab31 | | | 141 | * For internal builds, this points to the "summary" editor instead of the "description" editor |
| b69ab31 | | | 142 | */ |
| b69ab31 | | | 143 | getDescriptionEditor(): HTMLTextAreaElement { |
| b69ab31 | | | 144 | const description = screen.getByTestId( |
| b69ab31 | | | 145 | isInternalMessageFields() ? 'commit-info-summary-field' : 'commit-info-description-field', |
| b69ab31 | | | 146 | ) as HTMLTextAreaElement; |
| b69ab31 | | | 147 | expect(description).toBeInTheDocument(); |
| b69ab31 | | | 148 | return description; |
| b69ab31 | | | 149 | }, |
| b69ab31 | | | 150 | |
| b69ab31 | | | 151 | /** Get the textarea for the test plan editor. Unavailable in OSS tests (use internal-only tests). */ |
| b69ab31 | | | 152 | getTestPlanEditor(): HTMLTextAreaElement { |
| b69ab31 | | | 153 | assert(isInternalMessageFields(), 'Cannot edit test plan in OSS'); |
| b69ab31 | | | 154 | const testPlan = screen.getByTestId('commit-info-test-plan-field') as HTMLTextAreaElement; |
| b69ab31 | | | 155 | expect(testPlan).toBeInTheDocument(); |
| b69ab31 | | | 156 | return testPlan; |
| b69ab31 | | | 157 | }, |
| b69ab31 | | | 158 | |
| b69ab31 | | | 159 | /** Get the input element for a given field's editor, according to the field key in the FieldConfig (actually just a div in tests) */ |
| b69ab31 | | | 160 | getFieldEditor(key: string): HTMLDivElement { |
| b69ab31 | | | 161 | const renderKey = convertFieldNameToKey(key); |
| b69ab31 | | | 162 | const el = screen.getByTestId(`commit-info-${renderKey}-field`) as HTMLDivElement; |
| b69ab31 | | | 163 | expect(el).toBeInTheDocument(); |
| b69ab31 | | | 164 | return el; |
| b69ab31 | | | 165 | }, |
| b69ab31 | | | 166 | |
| b69ab31 | | | 167 | descriptionTextContent() { |
| b69ab31 | | | 168 | return CommitInfoTestUtils.getDescriptionEditor().value; |
| b69ab31 | | | 169 | }, |
| b69ab31 | | | 170 | |
| b69ab31 | | | 171 | clickToEditTitle() { |
| b69ab31 | | | 172 | act(() => { |
| b69ab31 | | | 173 | const title = screen.getByTestId('commit-info-rendered-title'); |
| b69ab31 | | | 174 | expect(title).toBeInTheDocument(); |
| b69ab31 | | | 175 | fireEvent.click(title); |
| b69ab31 | | | 176 | }); |
| b69ab31 | | | 177 | }, |
| b69ab31 | | | 178 | clickToEditDescription() { |
| b69ab31 | | | 179 | act(() => { |
| b69ab31 | | | 180 | const description = screen.getByTestId( |
| b69ab31 | | | 181 | isInternalMessageFields() |
| b69ab31 | | | 182 | ? 'commit-info-rendered-summary' |
| b69ab31 | | | 183 | : 'commit-info-rendered-description', |
| b69ab31 | | | 184 | ); |
| b69ab31 | | | 185 | expect(description).toBeInTheDocument(); |
| b69ab31 | | | 186 | fireEvent.click(description); |
| b69ab31 | | | 187 | }); |
| b69ab31 | | | 188 | }, |
| b69ab31 | | | 189 | clickToEditTestPlan() { |
| b69ab31 | | | 190 | assert(isInternalMessageFields(), 'Cannot edit test plan in OSS'); |
| b69ab31 | | | 191 | act(() => { |
| b69ab31 | | | 192 | const testPlan = screen.getByTestId('commit-info-rendered-test-plan'); |
| b69ab31 | | | 193 | expect(testPlan).toBeInTheDocument(); |
| b69ab31 | | | 194 | fireEvent.click(testPlan); |
| b69ab31 | | | 195 | }); |
| b69ab31 | | | 196 | }, |
| b69ab31 | | | 197 | |
| b69ab31 | | | 198 | /** Internal tests only, since GitHub's message schema does not include this field */ |
| b69ab31 | | | 199 | clickToEditReviewers() { |
| b69ab31 | | | 200 | act(() => { |
| b69ab31 | | | 201 | const title = screen.getByTestId('commit-info-rendered-reviewers'); |
| b69ab31 | | | 202 | expect(title).toBeInTheDocument(); |
| b69ab31 | | | 203 | fireEvent.click(title); |
| b69ab31 | | | 204 | }); |
| b69ab31 | | | 205 | }, |
| b69ab31 | | | 206 | |
| b69ab31 | | | 207 | expectIsEditingTitle() { |
| b69ab31 | | | 208 | const titleEditor = screen.queryByTestId('commit-info-title-field') as HTMLInputElement; |
| b69ab31 | | | 209 | expect(titleEditor).toBeInTheDocument(); |
| b69ab31 | | | 210 | }, |
| b69ab31 | | | 211 | expectIsNOTEditingTitle() { |
| b69ab31 | | | 212 | const titleEditor = screen.queryByTestId('commit-info-title-field') as HTMLInputElement; |
| b69ab31 | | | 213 | expect(titleEditor).not.toBeInTheDocument(); |
| b69ab31 | | | 214 | }, |
| b69ab31 | | | 215 | |
| b69ab31 | | | 216 | expectIsEditingDescription() { |
| b69ab31 | | | 217 | const descriptionEditor = screen.queryByTestId( |
| b69ab31 | | | 218 | isInternalMessageFields() ? 'commit-info-summary-field' : 'commit-info-description-field', |
| b69ab31 | | | 219 | ) as HTMLTextAreaElement; |
| b69ab31 | | | 220 | expect(descriptionEditor).toBeInTheDocument(); |
| b69ab31 | | | 221 | }, |
| b69ab31 | | | 222 | expectIsNOTEditingDescription() { |
| b69ab31 | | | 223 | const descriptionEditor = screen.queryByTestId( |
| b69ab31 | | | 224 | isInternalMessageFields() ? 'commit-info-summary-field' : 'commit-info-description-field', |
| b69ab31 | | | 225 | ) as HTMLTextAreaElement; |
| b69ab31 | | | 226 | expect(descriptionEditor).not.toBeInTheDocument(); |
| b69ab31 | | | 227 | }, |
| b69ab31 | | | 228 | }; |
| b69ab31 | | | 229 | |
| b69ab31 | | | 230 | export const MergeConflictTestUtils = { |
| b69ab31 | | | 231 | waitForContinueButtonNotDisabled: () => |
| b69ab31 | | | 232 | waitFor(() => |
| b69ab31 | | | 233 | expect( |
| b69ab31 | | | 234 | within(screen.getByTestId('commit-tree-root')).getByTestId( |
| b69ab31 | | | 235 | 'conflict-continue-button', |
| b69ab31 | | | 236 | ) as HTMLButtonElement, |
| b69ab31 | | | 237 | ).not.toBeDisabled(), |
| b69ab31 | | | 238 | ), |
| b69ab31 | | | 239 | clickContinueConflicts: () => |
| b69ab31 | | | 240 | act(() => { |
| b69ab31 | | | 241 | fireEvent.click( |
| b69ab31 | | | 242 | within(screen.getByTestId('commit-tree-root')).getByTestId('conflict-continue-button'), |
| b69ab31 | | | 243 | ); |
| b69ab31 | | | 244 | }), |
| b69ab31 | | | 245 | expectInMergeConflicts: () => |
| b69ab31 | | | 246 | expect( |
| b69ab31 | | | 247 | within(screen.getByTestId('commit-tree-root')).getByText('Unresolved Merge Conflicts'), |
| b69ab31 | | | 248 | ).toBeInTheDocument(), |
| b69ab31 | | | 249 | expectNotInMergeConflicts: () => |
| b69ab31 | | | 250 | expect( |
| b69ab31 | | | 251 | within(screen.getByTestId('commit-tree-root')).queryByText('Unresolved Merge Conflicts'), |
| b69ab31 | | | 252 | ).not.toBeInTheDocument(), |
| b69ab31 | | | 253 | }; |
| b69ab31 | | | 254 | |
| b69ab31 | | | 255 | function isInternalMessageFields(): boolean { |
| b69ab31 | | | 256 | const schema = readAtom(commitMessageFieldsSchema); |
| b69ab31 | | | 257 | return schema !== OSSCommitMessageFieldSchema; |
| b69ab31 | | | 258 | } |
| b69ab31 | | | 259 | |
| b69ab31 | | | 260 | /** |
| b69ab31 | | | 261 | * When querying changed files, there may be unicode left-to-right marks in the path, |
| b69ab31 | | | 262 | * which make the test hard to read. This util searches for a string, inserting optional |
| b69ab31 | | | 263 | * RTL marks at path boundaries. |
| b69ab31 | | | 264 | */ |
| b69ab31 | | | 265 | export function ignoreRTL(s: string): RegExp { |
| b69ab31 | | | 266 | return new RegExp(`^\u200E?${s}\u200E?$`); |
| b69ab31 | | | 267 | } |