| 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 App from '../App'; |
| b69ab31 | | | 11 | import platform from '../platform'; |
| b69ab31 | | | 12 | import {CommitInfoTestUtils} from '../testQueries'; |
| b69ab31 | | | 13 | import { |
| b69ab31 | | | 14 | COMMIT, |
| b69ab31 | | | 15 | expectMessageSentToServer, |
| b69ab31 | | | 16 | openCommitInfoSidebar, |
| b69ab31 | | | 17 | simulateCommits, |
| b69ab31 | | | 18 | simulateMessageFromServer, |
| b69ab31 | | | 19 | } from '../testUtils'; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | /* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | const { |
| b69ab31 | | | 24 | withinCommitInfo, |
| b69ab31 | | | 25 | clickAmendMode, |
| b69ab31 | | | 26 | clickCommitMode, |
| b69ab31 | | | 27 | clickToSelectCommit, |
| b69ab31 | | | 28 | getTitleEditor, |
| b69ab31 | | | 29 | getDescriptionEditor, |
| b69ab31 | | | 30 | } = CommitInfoTestUtils; |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | describe('FillCommitMessage', () => { |
| b69ab31 | | | 33 | beforeEach(() => { |
| b69ab31 | | | 34 | render(<App />); |
| b69ab31 | | | 35 | act(() => { |
| b69ab31 | | | 36 | openCommitInfoSidebar(); |
| b69ab31 | | | 37 | expectMessageSentToServer({ |
| b69ab31 | | | 38 | type: 'subscribe', |
| b69ab31 | | | 39 | kind: 'smartlogCommits', |
| b69ab31 | | | 40 | subscriptionID: expect.anything(), |
| b69ab31 | | | 41 | }); |
| b69ab31 | | | 42 | simulateCommits({ |
| b69ab31 | | | 43 | value: [ |
| b69ab31 | | | 44 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| b69ab31 | | | 45 | COMMIT('a', 'My Commit', '1'), |
| b69ab31 | | | 46 | COMMIT('b', 'Head Commit', 'a', { |
| b69ab31 | | | 47 | isDot: true, |
| b69ab31 | | | 48 | description: 'Summary: This is my commit message\n', |
| b69ab31 | | | 49 | }), |
| b69ab31 | | | 50 | ], |
| b69ab31 | | | 51 | }); |
| b69ab31 | | | 52 | }); |
| b69ab31 | | | 53 | }); |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | it('Shows fill message buttons in commit mode', () => { |
| b69ab31 | | | 56 | clickCommitMode(); |
| b69ab31 | | | 57 | expect(screen.getByText('Fill commit message from', {exact: false})).toBeInTheDocument(); |
| b69ab31 | | | 58 | }); |
| b69ab31 | | | 59 | |
| b69ab31 | | | 60 | it('does not show fill buttons in amend mode', () => { |
| b69ab31 | | | 61 | clickCommitMode(); |
| b69ab31 | | | 62 | clickToSelectCommit('a'); |
| b69ab31 | | | 63 | expect(screen.queryByText('Fill commit message from', {exact: false})).not.toBeInTheDocument(); |
| b69ab31 | | | 64 | clickToSelectCommit('b'); |
| b69ab31 | | | 65 | clickAmendMode(); |
| b69ab31 | | | 66 | expect(screen.queryByText('Fill commit message from', {exact: false})).not.toBeInTheDocument(); |
| b69ab31 | | | 67 | }); |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | it('Load from last commit', async () => { |
| b69ab31 | | | 70 | clickCommitMode(); |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | expect(getTitleEditor()).toHaveValue(''); |
| b69ab31 | | | 73 | expect(getDescriptionEditor()).toHaveValue(''); |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | const loadFromLastCommit = withinCommitInfo().getByText('last commit'); |
| b69ab31 | | | 76 | expect(loadFromLastCommit).toBeInTheDocument(); |
| b69ab31 | | | 77 | fireEvent.click(loadFromLastCommit); |
| b69ab31 | | | 78 | await waitFor(() => { |
| b69ab31 | | | 79 | expect(getTitleEditor().value).toMatch('Head Commit'); |
| b69ab31 | | | 80 | expect(getDescriptionEditor().value).toMatch(/This is my commit message/); |
| b69ab31 | | | 81 | }); |
| b69ab31 | | | 82 | }); |
| b69ab31 | | | 83 | |
| b69ab31 | | | 84 | it('Load from commit template', async () => { |
| b69ab31 | | | 85 | expectMessageSentToServer({type: 'fetchCommitMessageTemplate'}); |
| b69ab31 | | | 86 | act(() => { |
| b69ab31 | | | 87 | simulateMessageFromServer({ |
| b69ab31 | | | 88 | type: 'fetchedCommitMessageTemplate', |
| b69ab31 | | | 89 | template: 'template title\nSummary: template summary', |
| b69ab31 | | | 90 | }); |
| b69ab31 | | | 91 | }); |
| b69ab31 | | | 92 | clickCommitMode(); |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | // the template is used automatically, so let's clear it out |
| b69ab31 | | | 95 | act(() => { |
| b69ab31 | | | 96 | userEvent.clear(getTitleEditor()); |
| b69ab31 | | | 97 | userEvent.clear(getDescriptionEditor()); |
| b69ab31 | | | 98 | }); |
| b69ab31 | | | 99 | |
| b69ab31 | | | 100 | expect(getTitleEditor()).toHaveValue(''); |
| b69ab31 | | | 101 | expect(getDescriptionEditor()).toHaveValue(''); |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | const loadFromLastCommit = withinCommitInfo().getByText('template file'); |
| b69ab31 | | | 104 | expect(loadFromLastCommit).toBeInTheDocument(); |
| b69ab31 | | | 105 | fireEvent.click(loadFromLastCommit); |
| b69ab31 | | | 106 | await waitFor(() => { |
| b69ab31 | | | 107 | expect(getTitleEditor().value).toMatch('template title'); |
| b69ab31 | | | 108 | expect(getDescriptionEditor().value).toMatch(/template summary/); |
| b69ab31 | | | 109 | }); |
| b69ab31 | | | 110 | }); |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | describe('conflicts in message to fill', () => { |
| b69ab31 | | | 113 | async function triggerConflict() { |
| b69ab31 | | | 114 | clickCommitMode(); |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | act(() => { |
| b69ab31 | | | 117 | userEvent.type(getTitleEditor(), 'existing title'); |
| b69ab31 | | | 118 | userEvent.type(getDescriptionEditor(), 'Summary: existing description'); |
| b69ab31 | | | 119 | }); |
| b69ab31 | | | 120 | |
| b69ab31 | | | 121 | const loadFromLastCommit = withinCommitInfo().getByText('last commit'); |
| b69ab31 | | | 122 | expect(loadFromLastCommit).toBeInTheDocument(); |
| b69ab31 | | | 123 | fireEvent.click(loadFromLastCommit); |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | await waitFor(() => { |
| b69ab31 | | | 126 | expect(screen.getByText('Commit Messages Conflict')).toBeInTheDocument(); |
| b69ab31 | | | 127 | }); |
| b69ab31 | | | 128 | } |
| b69ab31 | | | 129 | |
| b69ab31 | | | 130 | function withinConflictWarning() { |
| b69ab31 | | | 131 | return within(screen.getByTestId('fill-message-conflict-warning')); |
| b69ab31 | | | 132 | } |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | it('shows warning and differences', async () => { |
| b69ab31 | | | 135 | await triggerConflict(); |
| b69ab31 | | | 136 | |
| b69ab31 | | | 137 | expect(withinConflictWarning().getByText('Title')).toBeInTheDocument(); |
| b69ab31 | | | 138 | expect(withinConflictWarning().getByText('existing title')).toBeInTheDocument(); |
| b69ab31 | | | 139 | expect(withinConflictWarning().getByText('Head Commit')).toBeInTheDocument(); |
| b69ab31 | | | 140 | expect( |
| b69ab31 | | | 141 | withinConflictWarning().getByText('existing description', {exact: false}), |
| b69ab31 | | | 142 | ).toBeInTheDocument(); |
| b69ab31 | | | 143 | expect( |
| b69ab31 | | | 144 | withinConflictWarning().getByText('This is my commit message', {exact: false}), |
| b69ab31 | | | 145 | ).toBeInTheDocument(); |
| b69ab31 | | | 146 | }); |
| b69ab31 | | | 147 | |
| b69ab31 | | | 148 | it('allows merging', async () => { |
| b69ab31 | | | 149 | await triggerConflict(); |
| b69ab31 | | | 150 | const mergeButton = screen.getByText('Merge'); |
| b69ab31 | | | 151 | expect(mergeButton).toBeInTheDocument(); |
| b69ab31 | | | 152 | fireEvent.click(mergeButton); |
| b69ab31 | | | 153 | |
| b69ab31 | | | 154 | await waitFor(() => { |
| b69ab31 | | | 155 | expect(getTitleEditor().value).toMatch('existing title, Head Commit'); |
| b69ab31 | | | 156 | expect(getDescriptionEditor().value).toMatch(/existing description/); |
| b69ab31 | | | 157 | expect(getDescriptionEditor().value).toMatch(/This is my commit message/); |
| b69ab31 | | | 158 | }); |
| b69ab31 | | | 159 | }); |
| b69ab31 | | | 160 | |
| b69ab31 | | | 161 | it('allows merging non-empty', async () => { |
| b69ab31 | | | 162 | await triggerConflict(); |
| b69ab31 | | | 163 | const mergeButton = screen.getByText('Only Fill Empty'); |
| b69ab31 | | | 164 | expect(mergeButton).toBeInTheDocument(); |
| b69ab31 | | | 165 | fireEvent.click(mergeButton); |
| b69ab31 | | | 166 | |
| b69ab31 | | | 167 | await waitFor(() => { |
| b69ab31 | | | 168 | expect(getTitleEditor().value).toMatch('existing title'); |
| b69ab31 | | | 169 | expect(getDescriptionEditor().value).toMatch(/existing description/); |
| b69ab31 | | | 170 | expect(getDescriptionEditor().value).not.toMatch(/This is my commit message/); |
| b69ab31 | | | 171 | }); |
| b69ab31 | | | 172 | }); |
| b69ab31 | | | 173 | |
| b69ab31 | | | 174 | it('allows cancelling', async () => { |
| b69ab31 | | | 175 | await triggerConflict(); |
| b69ab31 | | | 176 | const cancelButton = screen.getByText('Cancel'); |
| b69ab31 | | | 177 | expect(cancelButton).toBeInTheDocument(); |
| b69ab31 | | | 178 | fireEvent.click(cancelButton); |
| b69ab31 | | | 179 | |
| b69ab31 | | | 180 | expect(screen.queryByText('Commit Messages Conflict')).not.toBeInTheDocument(); |
| b69ab31 | | | 181 | await waitFor(() => { |
| b69ab31 | | | 182 | expect(getTitleEditor().value).toMatch('existing title'); |
| b69ab31 | | | 183 | expect(getDescriptionEditor().value).toMatch(/existing description/); |
| b69ab31 | | | 184 | }); |
| b69ab31 | | | 185 | }); |
| b69ab31 | | | 186 | |
| b69ab31 | | | 187 | it('allows overwriting', async () => { |
| b69ab31 | | | 188 | await triggerConflict(); |
| b69ab31 | | | 189 | const overwrite = screen.getByText('Overwrite'); |
| b69ab31 | | | 190 | expect(overwrite).toBeInTheDocument(); |
| b69ab31 | | | 191 | fireEvent.click(overwrite); |
| b69ab31 | | | 192 | |
| b69ab31 | | | 193 | await waitFor(() => { |
| b69ab31 | | | 194 | expect(getTitleEditor().value).toMatch('Head Commit'); |
| b69ab31 | | | 195 | expect(getDescriptionEditor().value).toMatch(/This is my commit message/); |
| b69ab31 | | | 196 | }); |
| b69ab31 | | | 197 | }); |
| b69ab31 | | | 198 | }); |
| b69ab31 | | | 199 | |
| b69ab31 | | | 200 | it('Clears commit message', async () => { |
| b69ab31 | | | 201 | clickCommitMode(); |
| b69ab31 | | | 202 | |
| b69ab31 | | | 203 | expect(getTitleEditor()).toHaveValue(''); |
| b69ab31 | | | 204 | expect(getDescriptionEditor()).toHaveValue(''); |
| b69ab31 | | | 205 | |
| b69ab31 | | | 206 | const confirmSpy = jest |
| b69ab31 | | | 207 | .spyOn(platform, 'confirm') |
| b69ab31 | | | 208 | .mockImplementation(() => Promise.resolve(true)); |
| b69ab31 | | | 209 | |
| b69ab31 | | | 210 | fireEvent.click(screen.getByTestId('fill-commit-message-more-options')); |
| b69ab31 | | | 211 | fireEvent.click(screen.getByText('Clear commit message')); |
| b69ab31 | | | 212 | |
| b69ab31 | | | 213 | await waitFor(() => expect(confirmSpy).toHaveBeenCalled()); |
| b69ab31 | | | 214 | }); |
| b69ab31 | | | 215 | }); |