| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {act, render, waitFor} from '@testing-library/react'; |
| 9 | import userEvent from '@testing-library/user-event'; |
| 10 | import * as utils from 'shared/utils'; |
| 11 | import App from '../../App'; |
| 12 | import {CommitInfoTestUtils} from '../../testQueries'; |
| 13 | import { |
| 14 | COMMIT, |
| 15 | expectMessageSentToServer, |
| 16 | getLastMessageOfTypeSentToServer, |
| 17 | openCommitInfoSidebar, |
| 18 | resetTestMessages, |
| 19 | simulateCommits, |
| 20 | simulateMessageFromServer, |
| 21 | simulateUncommittedChangedFiles, |
| 22 | } from '../../testUtils'; |
| 23 | |
| 24 | describe('AmendOperation', () => { |
| 25 | beforeEach(() => { |
| 26 | resetTestMessages(); |
| 27 | render(<App />); |
| 28 | act(() => { |
| 29 | expectMessageSentToServer({ |
| 30 | type: 'subscribe', |
| 31 | kind: 'smartlogCommits', |
| 32 | subscriptionID: expect.anything(), |
| 33 | }); |
| 34 | simulateUncommittedChangedFiles({ |
| 35 | value: [ |
| 36 | {path: 'file1.txt', status: 'M'}, |
| 37 | {path: 'file2.txt', status: 'A'}, |
| 38 | {path: 'file3.txt', status: 'R'}, |
| 39 | ], |
| 40 | }); |
| 41 | simulateCommits({ |
| 42 | value: [ |
| 43 | COMMIT('1', 'Commit 1', '0', {phase: 'public'}), |
| 44 | COMMIT('a', 'Commit A', '1'), |
| 45 | COMMIT('b', 'Commit B', 'a', {isDot: true}), |
| 46 | ], |
| 47 | }); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | it('on error, restores edited commit message to try again', async () => { |
| 52 | act(() => openCommitInfoSidebar()); |
| 53 | act(() => { |
| 54 | CommitInfoTestUtils.clickToEditTitle(); |
| 55 | CommitInfoTestUtils.clickToEditDescription(); |
| 56 | }); |
| 57 | act(() => { |
| 58 | const title = CommitInfoTestUtils.getTitleEditor(); |
| 59 | userEvent.type(title, 'My Commit'); |
| 60 | const desc = CommitInfoTestUtils.getDescriptionEditor(); |
| 61 | userEvent.type(desc, 'My description'); |
| 62 | }); |
| 63 | |
| 64 | await CommitInfoTestUtils.clickAmendButton(); |
| 65 | const message = await waitFor(() => |
| 66 | utils.nullthrows(getLastMessageOfTypeSentToServer('runOperation')), |
| 67 | ); |
| 68 | const id = message.operation.id; |
| 69 | |
| 70 | CommitInfoTestUtils.expectIsNOTEditingTitle(); |
| 71 | |
| 72 | act(() => { |
| 73 | simulateMessageFromServer({ |
| 74 | type: 'operationProgress', |
| 75 | kind: 'exit', |
| 76 | exitCode: 1, |
| 77 | id, |
| 78 | timestamp: 0, |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | waitFor(() => { |
| 83 | CommitInfoTestUtils.expectIsEditingTitle(); |
| 84 | const title = CommitInfoTestUtils.getTitleEditor(); |
| 85 | expect(title).toHaveValue('My Commit'); |
| 86 | CommitInfoTestUtils.expectIsEditingDescription(); |
| 87 | const desc = CommitInfoTestUtils.getDescriptionEditor(); |
| 88 | expect(desc).toHaveValue('My description'); |
| 89 | }); |
| 90 | }); |
| 91 | |
| 92 | it('on error, merges messages when restoring edited commit message to try again', async () => { |
| 93 | act(() => openCommitInfoSidebar()); |
| 94 | |
| 95 | act(() => { |
| 96 | CommitInfoTestUtils.clickToEditTitle(); |
| 97 | CommitInfoTestUtils.clickToEditDescription(); |
| 98 | }); |
| 99 | act(() => { |
| 100 | const title = CommitInfoTestUtils.getTitleEditor(); |
| 101 | userEvent.type(title, 'My Commit'); |
| 102 | const desc = CommitInfoTestUtils.getDescriptionEditor(); |
| 103 | userEvent.type(desc, 'My description'); |
| 104 | }); |
| 105 | |
| 106 | await CommitInfoTestUtils.clickAmendButton(); |
| 107 | const message = await waitFor(() => |
| 108 | utils.nullthrows(getLastMessageOfTypeSentToServer('runOperation')), |
| 109 | ); |
| 110 | const id = message.operation.id; |
| 111 | |
| 112 | CommitInfoTestUtils.expectIsNOTEditingTitle(); |
| 113 | |
| 114 | act(() => { |
| 115 | openCommitInfoSidebar(); |
| 116 | }); |
| 117 | act(() => { |
| 118 | CommitInfoTestUtils.clickToEditTitle(); |
| 119 | CommitInfoTestUtils.clickToEditDescription(); |
| 120 | }); |
| 121 | act(() => { |
| 122 | const title = CommitInfoTestUtils.getTitleEditor(); |
| 123 | userEvent.type(title, 'other title'); |
| 124 | const desc = CommitInfoTestUtils.getDescriptionEditor(); |
| 125 | userEvent.type(desc, 'other description'); |
| 126 | }); |
| 127 | |
| 128 | act(() => { |
| 129 | simulateMessageFromServer({ |
| 130 | type: 'operationProgress', |
| 131 | kind: 'exit', |
| 132 | exitCode: 1, |
| 133 | id, |
| 134 | timestamp: 0, |
| 135 | }); |
| 136 | }); |
| 137 | |
| 138 | waitFor(() => { |
| 139 | CommitInfoTestUtils.expectIsEditingTitle(); |
| 140 | const title = CommitInfoTestUtils.getTitleEditor(); |
| 141 | expect(title).toHaveValue('other title, My Commit'); |
| 142 | CommitInfoTestUtils.expectIsEditingDescription(); |
| 143 | const desc = CommitInfoTestUtils.getDescriptionEditor(); |
| 144 | expect(desc).toHaveValue('other description, My description'); |
| 145 | }); |
| 146 | }); |
| 147 | }); |
| 148 | |