| 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, fireEvent, render, screen, within} from '@testing-library/react'; |
| 9 | import userEvent from '@testing-library/user-event'; |
| 10 | import App from '../../App'; |
| 11 | import {CommitInfoTestUtils, CommitTreeListTestUtils} from '../../testQueries'; |
| 12 | import { |
| 13 | closeCommitInfoSidebar, |
| 14 | COMMIT, |
| 15 | expectMessageSentToServer, |
| 16 | resetTestMessages, |
| 17 | simulateCommits, |
| 18 | TEST_COMMIT_HISTORY, |
| 19 | } from '../../testUtils'; |
| 20 | import {CommandRunner} from '../../types'; |
| 21 | |
| 22 | /*eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 23 | |
| 24 | describe('hide operation', () => { |
| 25 | beforeEach(() => { |
| 26 | resetTestMessages(); |
| 27 | render(<App />); |
| 28 | act(() => { |
| 29 | closeCommitInfoSidebar(); |
| 30 | expectMessageSentToServer({ |
| 31 | type: 'subscribe', |
| 32 | kind: 'smartlogCommits', |
| 33 | subscriptionID: expect.anything(), |
| 34 | }); |
| 35 | simulateCommits({ |
| 36 | value: TEST_COMMIT_HISTORY, |
| 37 | }); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | function rightClickAndChooseFromContextMenu(element: Element, choiceMatcher: string) { |
| 42 | act(() => { |
| 43 | fireEvent.contextMenu(element); |
| 44 | }); |
| 45 | const choice = within(screen.getByTestId('context-menu-container')).getByText(choiceMatcher); |
| 46 | expect(choice).not.toEqual(null); |
| 47 | act(() => { |
| 48 | fireEvent.click(choice); |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | it('previews hiding a stack of commits', () => { |
| 53 | rightClickAndChooseFromContextMenu(screen.getByText('Commit B'), 'Hide Commit and Descendants'); |
| 54 | |
| 55 | expect(document.querySelectorAll('.commit-preview-hidden-root')).toHaveLength(1); |
| 56 | expect(document.querySelectorAll('.commit-preview-hidden-descendant')).toHaveLength(3); |
| 57 | }); |
| 58 | |
| 59 | it('runs hide operation', () => { |
| 60 | rightClickAndChooseFromContextMenu(screen.getByText('Commit B'), 'Hide Commit and Descendants'); |
| 61 | |
| 62 | const runHideButton = screen.getByText('Hide'); |
| 63 | expect(runHideButton).toBeInTheDocument(); |
| 64 | fireEvent.click(runHideButton); |
| 65 | |
| 66 | expectMessageSentToServer({ |
| 67 | type: 'runOperation', |
| 68 | operation: { |
| 69 | args: ['hide', '--rev', {type: 'succeedable-revset', revset: 'b'}], |
| 70 | id: expect.anything(), |
| 71 | runner: CommandRunner.Sapling, |
| 72 | trackEventName: 'HideOperation', |
| 73 | }, |
| 74 | }); |
| 75 | }); |
| 76 | |
| 77 | it('uses exact revset if commit is obsolete', () => { |
| 78 | act(() => { |
| 79 | simulateCommits({ |
| 80 | value: [ |
| 81 | COMMIT('a', 'Commit A', '1', {successorInfo: {hash: 'a2', type: 'rebase'}}), |
| 82 | COMMIT('a2', 'Commit A2', '2'), |
| 83 | COMMIT('b', 'Commit B', '1'), |
| 84 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| 85 | COMMIT('2', 'some public base 2', '1', {phase: 'public'}), |
| 86 | ], |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | rightClickAndChooseFromContextMenu(screen.getByText('Commit A'), 'Hide Commit'); |
| 91 | |
| 92 | const runHideButton = screen.getByText('Hide'); |
| 93 | expect(runHideButton).toBeInTheDocument(); |
| 94 | fireEvent.click(runHideButton); |
| 95 | |
| 96 | expectMessageSentToServer({ |
| 97 | type: 'runOperation', |
| 98 | operation: { |
| 99 | args: ['hide', '--rev', {type: 'exact-revset', revset: 'a'}], |
| 100 | id: expect.anything(), |
| 101 | runner: CommandRunner.Sapling, |
| 102 | trackEventName: 'HideOperation', |
| 103 | }, |
| 104 | }); |
| 105 | }); |
| 106 | |
| 107 | it('shows optimistic preview of hide', () => { |
| 108 | rightClickAndChooseFromContextMenu(screen.getByText('Commit B'), 'Hide Commit and Descendants'); |
| 109 | |
| 110 | const runHideButton = screen.getByText('Hide'); |
| 111 | fireEvent.click(runHideButton); |
| 112 | |
| 113 | // original commit is hidden |
| 114 | expect(screen.queryByTestId('commit-b')).not.toBeInTheDocument(); |
| 115 | // same for descendants |
| 116 | expect(screen.queryByTestId('commit-c')).not.toBeInTheDocument(); |
| 117 | expect(screen.queryByTestId('commit-d')).not.toBeInTheDocument(); |
| 118 | expect(screen.queryByTestId('commit-e')).not.toBeInTheDocument(); |
| 119 | }); |
| 120 | |
| 121 | it('does not show uninteresting public base during optimistic hide', async () => { |
| 122 | // Go to another branch so head is not being hidden. |
| 123 | await CommitTreeListTestUtils.clickGoto('z'); |
| 124 | rightClickAndChooseFromContextMenu(screen.getByText('Commit A'), 'Hide Commit and Descendants'); |
| 125 | |
| 126 | const runHideButton = screen.getByText('Hide'); |
| 127 | fireEvent.click(runHideButton); |
| 128 | |
| 129 | // the whole subtree is hidden, so the parent commit is not even rendered |
| 130 | expect(screen.queryByTestId('commit-1')).not.toBeInTheDocument(); |
| 131 | }); |
| 132 | |
| 133 | it('shows public base when its the goto preview destination', () => { |
| 134 | rightClickAndChooseFromContextMenu(screen.getByText('Commit A'), 'Hide Commit and Descendants'); |
| 135 | |
| 136 | const runHideButton = screen.getByText('Hide'); |
| 137 | fireEvent.click(runHideButton); |
| 138 | |
| 139 | // the whole subtree and head is hidden, so the parent commit is shown as the goto destination |
| 140 | expect(screen.queryByTestId('commit-1')).toBeInTheDocument(); |
| 141 | }); |
| 142 | |
| 143 | it('does show interesting public base during optimistic hide', () => { |
| 144 | rightClickAndChooseFromContextMenu(screen.getByText('Commit X'), 'Hide Commit and Descendants'); |
| 145 | |
| 146 | const runHideButton = screen.getByText('Hide'); |
| 147 | fireEvent.click(runHideButton); |
| 148 | |
| 149 | // the whole subtree is hidden, but this commit has the remote/master bookmark so it's shown anyway. |
| 150 | expect(screen.queryByTestId('commit-2')).toBeInTheDocument(); |
| 151 | }); |
| 152 | |
| 153 | it('previews a hide by pressing delete with a selection', () => { |
| 154 | CommitInfoTestUtils.clickToSelectCommit('b'); |
| 155 | |
| 156 | act(() => { |
| 157 | userEvent.type(document.body, '{Backspace}'); |
| 158 | }); |
| 159 | |
| 160 | const runHideButton = screen.getByText('Hide'); |
| 161 | expect(runHideButton).toBeInTheDocument(); |
| 162 | expect(runHideButton).toHaveFocus(); |
| 163 | fireEvent.click(runHideButton); |
| 164 | |
| 165 | expectMessageSentToServer({ |
| 166 | type: 'runOperation', |
| 167 | operation: { |
| 168 | args: ['hide', '--rev', {type: 'succeedable-revset', revset: 'b'}], |
| 169 | id: expect.anything(), |
| 170 | runner: CommandRunner.Sapling, |
| 171 | trackEventName: 'HideOperation', |
| 172 | }, |
| 173 | }); |
| 174 | }); |
| 175 | }); |
| 176 | |