| 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 {act, fireEvent, render, screen, waitFor, within} from '@testing-library/react'; |
| b69ab31 | | | 9 | import userEvent from '@testing-library/user-event'; |
| b69ab31 | | | 10 | import * as utils from 'shared/utils'; |
| b69ab31 | | | 11 | import App from '../../App'; |
| b69ab31 | | | 12 | import {CommitInfoTestUtils} from '../../testQueries'; |
| b69ab31 | | | 13 | import { |
| b69ab31 | | | 14 | COMMIT, |
| b69ab31 | | | 15 | expectMessageSentToServer, |
| b69ab31 | | | 16 | getLastMessageOfTypeSentToServer, |
| b69ab31 | | | 17 | openCommitInfoSidebar, |
| b69ab31 | | | 18 | resetTestMessages, |
| b69ab31 | | | 19 | simulateCommits, |
| b69ab31 | | | 20 | simulateMessageFromServer, |
| b69ab31 | | | 21 | simulateUncommittedChangedFiles, |
| b69ab31 | | | 22 | } from '../../testUtils'; |
| b69ab31 | | | 23 | import {CommandRunner} from '../../types'; |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | describe('CommitOperation', () => { |
| b69ab31 | | | 26 | beforeEach(() => { |
| b69ab31 | | | 27 | resetTestMessages(); |
| b69ab31 | | | 28 | render(<App />); |
| b69ab31 | | | 29 | act(() => { |
| b69ab31 | | | 30 | expectMessageSentToServer({ |
| b69ab31 | | | 31 | type: 'subscribe', |
| b69ab31 | | | 32 | kind: 'smartlogCommits', |
| b69ab31 | | | 33 | subscriptionID: expect.anything(), |
| b69ab31 | | | 34 | }); |
| b69ab31 | | | 35 | simulateUncommittedChangedFiles({ |
| b69ab31 | | | 36 | value: [ |
| b69ab31 | | | 37 | {path: 'file1.txt', status: 'M'}, |
| b69ab31 | | | 38 | {path: 'file2.txt', status: 'A'}, |
| b69ab31 | | | 39 | {path: 'file3.txt', status: 'R'}, |
| b69ab31 | | | 40 | ], |
| b69ab31 | | | 41 | }); |
| b69ab31 | | | 42 | simulateCommits({ |
| b69ab31 | | | 43 | value: [ |
| b69ab31 | | | 44 | COMMIT('2', 'master', '00', {phase: 'public', remoteBookmarks: ['remote/master']}), |
| b69ab31 | | | 45 | COMMIT('1', 'Commit 1', '0', {phase: 'public'}), |
| b69ab31 | | | 46 | COMMIT('a', 'Commit A', '1'), |
| b69ab31 | | | 47 | COMMIT('b', 'Commit B', 'a', {isDot: true}), |
| b69ab31 | | | 48 | ], |
| b69ab31 | | | 49 | }); |
| b69ab31 | | | 50 | }); |
| b69ab31 | | | 51 | }); |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | const clickQuickCommit = async () => { |
| b69ab31 | | | 54 | const quickCommitButton = screen.getByTestId('quick-commit-button'); |
| b69ab31 | | | 55 | act(() => { |
| b69ab31 | | | 56 | fireEvent.click(quickCommitButton); |
| b69ab31 | | | 57 | }); |
| b69ab31 | | | 58 | await waitFor(() => |
| b69ab31 | | | 59 | expectMessageSentToServer({ |
| b69ab31 | | | 60 | type: 'runOperation', |
| b69ab31 | | | 61 | operation: expect.objectContaining({ |
| b69ab31 | | | 62 | args: expect.arrayContaining(['commit']), |
| b69ab31 | | | 63 | }), |
| b69ab31 | | | 64 | }), |
| b69ab31 | | | 65 | ); |
| b69ab31 | | | 66 | }; |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | const clickCheckboxForFile = (inside: HTMLElement, fileName: string) => { |
| b69ab31 | | | 69 | act(() => { |
| b69ab31 | | | 70 | const checkbox = within(within(inside).getByTestId(`changed-file-${fileName}`)).getByTestId( |
| b69ab31 | | | 71 | 'file-selection-checkbox', |
| b69ab31 | | | 72 | ); |
| b69ab31 | | | 73 | expect(checkbox).toBeInTheDocument(); |
| b69ab31 | | | 74 | fireEvent.click(checkbox); |
| b69ab31 | | | 75 | }); |
| b69ab31 | | | 76 | }; |
| b69ab31 | | | 77 | |
| b69ab31 | | | 78 | it('runs commit', async () => { |
| b69ab31 | | | 79 | await clickQuickCommit(); |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | expectMessageSentToServer({ |
| b69ab31 | | | 82 | type: 'runOperation', |
| b69ab31 | | | 83 | operation: { |
| b69ab31 | | | 84 | args: [ |
| b69ab31 | | | 85 | 'commit', |
| b69ab31 | | | 86 | '--addremove', |
| b69ab31 | | | 87 | '--message', |
| b69ab31 | | | 88 | expect.stringContaining(`Temporary Commit at`), |
| b69ab31 | | | 89 | ], |
| b69ab31 | | | 90 | id: expect.anything(), |
| b69ab31 | | | 91 | runner: CommandRunner.Sapling, |
| b69ab31 | | | 92 | trackEventName: 'CommitOperation', |
| b69ab31 | | | 93 | }, |
| b69ab31 | | | 94 | }); |
| b69ab31 | | | 95 | }); |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | it('runs commit with subset of files selected', async () => { |
| b69ab31 | | | 98 | const commitTree = screen.getByTestId('commit-tree-root'); |
| b69ab31 | | | 99 | clickCheckboxForFile(commitTree, 'file2.txt'); |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | await clickQuickCommit(); |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | expectMessageSentToServer({ |
| b69ab31 | | | 104 | type: 'runOperation', |
| b69ab31 | | | 105 | operation: { |
| b69ab31 | | | 106 | args: [ |
| b69ab31 | | | 107 | 'commit', |
| b69ab31 | | | 108 | '--addremove', |
| b69ab31 | | | 109 | '--message', |
| b69ab31 | | | 110 | expect.stringContaining(`Temporary Commit at`), |
| b69ab31 | | | 111 | {type: 'repo-relative-file', path: 'file1.txt'}, |
| b69ab31 | | | 112 | {type: 'repo-relative-file', path: 'file3.txt'}, |
| b69ab31 | | | 113 | ], |
| b69ab31 | | | 114 | id: expect.anything(), |
| b69ab31 | | | 115 | runner: CommandRunner.Sapling, |
| b69ab31 | | | 116 | trackEventName: 'CommitFileSubsetOperation', |
| b69ab31 | | | 117 | }, |
| b69ab31 | | | 118 | }); |
| b69ab31 | | | 119 | }); |
| b69ab31 | | | 120 | |
| b69ab31 | | | 121 | it('changed files are shown in commit info view', async () => { |
| b69ab31 | | | 122 | const commitTree = screen.getByTestId('commit-tree-root'); |
| b69ab31 | | | 123 | clickCheckboxForFile(commitTree, 'file2.txt'); |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | const quickInput = screen.getByTestId('quick-commit-title'); |
| b69ab31 | | | 126 | |
| b69ab31 | | | 127 | act(() => { |
| b69ab31 | | | 128 | userEvent.type(quickInput, 'My Commit'); |
| b69ab31 | | | 129 | }); |
| b69ab31 | | | 130 | |
| b69ab31 | | | 131 | await clickQuickCommit(); |
| b69ab31 | | | 132 | |
| b69ab31 | | | 133 | expect( |
| b69ab31 | | | 134 | within(screen.getByTestId('changes-to-amend')).queryByText(/file1.txt/), |
| b69ab31 | | | 135 | ).not.toBeInTheDocument(); |
| b69ab31 | | | 136 | expect( |
| b69ab31 | | | 137 | within(screen.getByTestId('changes-to-amend')).getByText(/file2.txt/), |
| b69ab31 | | | 138 | ).toBeInTheDocument(); |
| b69ab31 | | | 139 | expect( |
| b69ab31 | | | 140 | within(screen.getByTestId('changes-to-amend')).queryByText(/file3.txt/), |
| b69ab31 | | | 141 | ).not.toBeInTheDocument(); |
| b69ab31 | | | 142 | |
| b69ab31 | | | 143 | expect( |
| b69ab31 | | | 144 | within(screen.getByTestId('committed-changes')).getByText(/file1.txt/), |
| b69ab31 | | | 145 | ).toBeInTheDocument(); |
| b69ab31 | | | 146 | expect( |
| b69ab31 | | | 147 | within(screen.getByTestId('committed-changes')).queryByText(/file2.txt/), |
| b69ab31 | | | 148 | ).not.toBeInTheDocument(); |
| b69ab31 | | | 149 | expect( |
| b69ab31 | | | 150 | within(screen.getByTestId('committed-changes')).getByText(/file3.txt/), |
| b69ab31 | | | 151 | ).toBeInTheDocument(); |
| b69ab31 | | | 152 | }); |
| b69ab31 | | | 153 | |
| b69ab31 | | | 154 | it('uses commit template if provided', async () => { |
| b69ab31 | | | 155 | await waitFor(() => { |
| b69ab31 | | | 156 | expectMessageSentToServer({type: 'fetchCommitMessageTemplate'}); |
| b69ab31 | | | 157 | }); |
| b69ab31 | | | 158 | act(() => { |
| b69ab31 | | | 159 | simulateMessageFromServer({ |
| b69ab31 | | | 160 | type: 'fetchedCommitMessageTemplate', |
| b69ab31 | | | 161 | template: 'Template Title\n\nSummary: my template', |
| b69ab31 | | | 162 | }); |
| b69ab31 | | | 163 | }); |
| b69ab31 | | | 164 | |
| b69ab31 | | | 165 | await clickQuickCommit(); |
| b69ab31 | | | 166 | |
| b69ab31 | | | 167 | expectMessageSentToServer({ |
| b69ab31 | | | 168 | type: 'runOperation', |
| b69ab31 | | | 169 | operation: { |
| b69ab31 | | | 170 | args: ['commit', '--addremove', '--message', expect.stringContaining('Template Title')], |
| b69ab31 | | | 171 | id: expect.anything(), |
| b69ab31 | | | 172 | runner: CommandRunner.Sapling, |
| b69ab31 | | | 173 | trackEventName: 'CommitOperation', |
| b69ab31 | | | 174 | }, |
| b69ab31 | | | 175 | }); |
| b69ab31 | | | 176 | }); |
| b69ab31 | | | 177 | |
| b69ab31 | | | 178 | it('clears quick commit title after committing', async () => { |
| b69ab31 | | | 179 | const commitTree = screen.getByTestId('commit-tree-root'); |
| b69ab31 | | | 180 | clickCheckboxForFile(commitTree, 'file2.txt'); // partial commit, so the quick input box isn't unmounted |
| b69ab31 | | | 181 | |
| b69ab31 | | | 182 | const quickInput = screen.getByTestId('quick-commit-title'); |
| b69ab31 | | | 183 | act(() => { |
| b69ab31 | | | 184 | userEvent.type(quickInput, 'My Commit'); |
| b69ab31 | | | 185 | }); |
| b69ab31 | | | 186 | |
| b69ab31 | | | 187 | await clickQuickCommit(); |
| b69ab31 | | | 188 | |
| b69ab31 | | | 189 | expect(quickInput).toHaveValue(''); |
| b69ab31 | | | 190 | }); |
| b69ab31 | | | 191 | |
| b69ab31 | | | 192 | it('on error, restores edited commit message to try again', async () => { |
| b69ab31 | | | 193 | act(() => openCommitInfoSidebar()); |
| b69ab31 | | | 194 | act(() => CommitInfoTestUtils.clickCommitMode()); |
| b69ab31 | | | 195 | |
| b69ab31 | | | 196 | act(() => { |
| b69ab31 | | | 197 | const title = CommitInfoTestUtils.getTitleEditor(); |
| b69ab31 | | | 198 | userEvent.type(title, 'My Commit'); |
| b69ab31 | | | 199 | const desc = CommitInfoTestUtils.getDescriptionEditor(); |
| b69ab31 | | | 200 | userEvent.type(desc, 'My description'); |
| b69ab31 | | | 201 | }); |
| b69ab31 | | | 202 | |
| b69ab31 | | | 203 | await CommitInfoTestUtils.clickCommitButton(); |
| b69ab31 | | | 204 | const message = await waitFor(() => |
| b69ab31 | | | 205 | utils.nullthrows(getLastMessageOfTypeSentToServer('runOperation')), |
| b69ab31 | | | 206 | ); |
| b69ab31 | | | 207 | const id = message.operation.id; |
| b69ab31 | | | 208 | |
| b69ab31 | | | 209 | CommitInfoTestUtils.expectIsNOTEditingTitle(); |
| b69ab31 | | | 210 | |
| b69ab31 | | | 211 | act(() => { |
| b69ab31 | | | 212 | simulateMessageFromServer({ |
| b69ab31 | | | 213 | type: 'operationProgress', |
| b69ab31 | | | 214 | kind: 'exit', |
| b69ab31 | | | 215 | exitCode: 1, |
| b69ab31 | | | 216 | id, |
| b69ab31 | | | 217 | timestamp: 0, |
| b69ab31 | | | 218 | }); |
| b69ab31 | | | 219 | }); |
| b69ab31 | | | 220 | |
| b69ab31 | | | 221 | await waitFor(() => { |
| b69ab31 | | | 222 | CommitInfoTestUtils.expectIsEditingTitle(); |
| b69ab31 | | | 223 | const title = CommitInfoTestUtils.getTitleEditor(); |
| b69ab31 | | | 224 | expect(title).toHaveValue('My Commit'); |
| b69ab31 | | | 225 | CommitInfoTestUtils.expectIsEditingDescription(); |
| b69ab31 | | | 226 | const desc = CommitInfoTestUtils.getDescriptionEditor(); |
| b69ab31 | | | 227 | expect(desc.value).toContain('My description'); |
| b69ab31 | | | 228 | }); |
| b69ab31 | | | 229 | }); |
| b69ab31 | | | 230 | |
| b69ab31 | | | 231 | it('on error, merges messages when restoring edited commit message to try again', async () => { |
| b69ab31 | | | 232 | act(() => openCommitInfoSidebar()); |
| b69ab31 | | | 233 | act(() => CommitInfoTestUtils.clickCommitMode()); |
| b69ab31 | | | 234 | |
| b69ab31 | | | 235 | act(() => { |
| b69ab31 | | | 236 | const title = CommitInfoTestUtils.getTitleEditor(); |
| b69ab31 | | | 237 | userEvent.type(title, 'My Commit'); |
| b69ab31 | | | 238 | const desc = CommitInfoTestUtils.getDescriptionEditor(); |
| b69ab31 | | | 239 | userEvent.type(desc, 'My description'); |
| b69ab31 | | | 240 | }); |
| b69ab31 | | | 241 | |
| b69ab31 | | | 242 | await CommitInfoTestUtils.clickCommitButton(); |
| b69ab31 | | | 243 | const message = await waitFor(() => |
| b69ab31 | | | 244 | utils.nullthrows(getLastMessageOfTypeSentToServer('runOperation')), |
| b69ab31 | | | 245 | ); |
| b69ab31 | | | 246 | const id = message.operation.id; |
| b69ab31 | | | 247 | |
| b69ab31 | | | 248 | CommitInfoTestUtils.expectIsNOTEditingTitle(); |
| b69ab31 | | | 249 | |
| b69ab31 | | | 250 | act(() => { |
| b69ab31 | | | 251 | openCommitInfoSidebar(); |
| b69ab31 | | | 252 | CommitInfoTestUtils.clickCommitMode(); |
| b69ab31 | | | 253 | }); |
| b69ab31 | | | 254 | act(() => { |
| b69ab31 | | | 255 | const title = CommitInfoTestUtils.getTitleEditor(); |
| b69ab31 | | | 256 | userEvent.type(title, 'other title'); |
| b69ab31 | | | 257 | const desc = CommitInfoTestUtils.getDescriptionEditor(); |
| b69ab31 | | | 258 | userEvent.type(desc, 'other description'); |
| b69ab31 | | | 259 | }); |
| b69ab31 | | | 260 | |
| b69ab31 | | | 261 | act(() => { |
| b69ab31 | | | 262 | simulateMessageFromServer({ |
| b69ab31 | | | 263 | type: 'operationProgress', |
| b69ab31 | | | 264 | kind: 'exit', |
| b69ab31 | | | 265 | exitCode: 1, |
| b69ab31 | | | 266 | id, |
| b69ab31 | | | 267 | timestamp: 0, |
| b69ab31 | | | 268 | }); |
| b69ab31 | | | 269 | }); |
| b69ab31 | | | 270 | |
| b69ab31 | | | 271 | await waitFor(() => { |
| b69ab31 | | | 272 | CommitInfoTestUtils.expectIsEditingTitle(); |
| b69ab31 | | | 273 | const title = CommitInfoTestUtils.getTitleEditor(); |
| b69ab31 | | | 274 | expect(title).toHaveValue('other title, My Commit'); |
| b69ab31 | | | 275 | CommitInfoTestUtils.expectIsEditingDescription(); |
| b69ab31 | | | 276 | const desc = CommitInfoTestUtils.getDescriptionEditor(); |
| b69ab31 | | | 277 | expect(desc.value).toContain('other description'); |
| b69ab31 | | | 278 | expect(desc.value).toContain('My description'); |
| b69ab31 | | | 279 | }); |
| b69ab31 | | | 280 | }); |
| b69ab31 | | | 281 | }); |