| 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 {ComparisonType} from 'shared/Comparison'; |
| b69ab31 | | | 11 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 12 | import App from '../App'; |
| b69ab31 | | | 13 | import {ignoreRTL} from '../testQueries'; |
| b69ab31 | | | 14 | import { |
| b69ab31 | | | 15 | closeCommitInfoSidebar, |
| b69ab31 | | | 16 | COMMIT, |
| b69ab31 | | | 17 | expectMessageSentToServer, |
| b69ab31 | | | 18 | resetTestMessages, |
| b69ab31 | | | 19 | simulateCommits, |
| b69ab31 | | | 20 | simulateMessageFromServer, |
| b69ab31 | | | 21 | simulateRepoConnected, |
| b69ab31 | | | 22 | simulateUncommittedChangedFiles, |
| b69ab31 | | | 23 | } from '../testUtils'; |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | describe('Shelve', () => { |
| b69ab31 | | | 26 | beforeEach(() => { |
| b69ab31 | | | 27 | resetTestMessages(); |
| b69ab31 | | | 28 | render(<App />); |
| b69ab31 | | | 29 | act(() => { |
| b69ab31 | | | 30 | simulateRepoConnected(); |
| b69ab31 | | | 31 | closeCommitInfoSidebar(); |
| b69ab31 | | | 32 | expectMessageSentToServer({ |
| b69ab31 | | | 33 | type: 'subscribe', |
| b69ab31 | | | 34 | kind: 'smartlogCommits', |
| b69ab31 | | | 35 | subscriptionID: expect.anything(), |
| b69ab31 | | | 36 | }); |
| b69ab31 | | | 37 | simulateCommits({ |
| b69ab31 | | | 38 | value: [ |
| b69ab31 | | | 39 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| b69ab31 | | | 40 | COMMIT('a', 'My Commit', '1'), |
| b69ab31 | | | 41 | COMMIT('b', 'Another Commit', 'a', {isDot: true}), |
| b69ab31 | | | 42 | ], |
| b69ab31 | | | 43 | }); |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | simulateUncommittedChangedFiles({ |
| b69ab31 | | | 46 | value: [ |
| b69ab31 | | | 47 | {path: 'src/file1.js', status: 'M'}, |
| b69ab31 | | | 48 | {path: 'src/file2.js', status: 'M'}, |
| b69ab31 | | | 49 | {path: 'src/file3.js', status: 'M'}, |
| b69ab31 | | | 50 | ], |
| b69ab31 | | | 51 | }); |
| b69ab31 | | | 52 | }); |
| b69ab31 | | | 53 | }); |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | describe('Shelve button', () => { |
| b69ab31 | | | 56 | it('Runs shelve when clicking button', async () => { |
| b69ab31 | | | 57 | const shelveButton = screen.getByText('Shelve'); |
| b69ab31 | | | 58 | expect(shelveButton).toBeInTheDocument(); |
| b69ab31 | | | 59 | |
| b69ab31 | | | 60 | fireEvent.click(shelveButton); |
| b69ab31 | | | 61 | await waitFor(() => |
| b69ab31 | | | 62 | expectMessageSentToServer({ |
| b69ab31 | | | 63 | type: 'runOperation', |
| b69ab31 | | | 64 | operation: expect.objectContaining({ |
| b69ab31 | | | 65 | args: ['shelve', '--unknown'], |
| b69ab31 | | | 66 | }), |
| b69ab31 | | | 67 | }), |
| b69ab31 | | | 68 | ); |
| b69ab31 | | | 69 | }); |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | it('Optimistic state hides all files when shelve running', async () => { |
| b69ab31 | | | 72 | expect(screen.queryAllByTestId(/changed-file-src\/file.\.js/)).toHaveLength(3); |
| b69ab31 | | | 73 | const shelveButton = screen.getByText('Shelve'); |
| b69ab31 | | | 74 | fireEvent.click(shelveButton); |
| b69ab31 | | | 75 | await waitFor(() => |
| b69ab31 | | | 76 | expect(screen.queryAllByTestId(/changed-file-src\/file.\.js/)).toHaveLength(0), |
| b69ab31 | | | 77 | ); |
| b69ab31 | | | 78 | }); |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | it('includes name for shelved change if typed', async () => { |
| b69ab31 | | | 81 | const shelveButton = screen.getByText('Shelve'); |
| b69ab31 | | | 82 | expect(shelveButton).toBeInTheDocument(); |
| b69ab31 | | | 83 | |
| b69ab31 | | | 84 | const quickInput = screen.getByTestId('quick-commit-title'); |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | act(() => { |
| b69ab31 | | | 87 | userEvent.type(quickInput, 'My Shelf'); |
| b69ab31 | | | 88 | }); |
| b69ab31 | | | 89 | |
| b69ab31 | | | 90 | fireEvent.click(shelveButton); |
| b69ab31 | | | 91 | await waitFor(() => |
| b69ab31 | | | 92 | expectMessageSentToServer({ |
| b69ab31 | | | 93 | type: 'runOperation', |
| b69ab31 | | | 94 | operation: expect.objectContaining({ |
| b69ab31 | | | 95 | args: ['shelve', '--unknown', '--name', 'My Shelf'], |
| b69ab31 | | | 96 | }), |
| b69ab31 | | | 97 | }), |
| b69ab31 | | | 98 | ); |
| b69ab31 | | | 99 | }); |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | it('only shelves selected files', async () => { |
| b69ab31 | | | 102 | const shelveButton = screen.getByText('Shelve'); |
| b69ab31 | | | 103 | expect(shelveButton).toBeInTheDocument(); |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | // uncheck one file |
| b69ab31 | | | 106 | fireEvent.click( |
| b69ab31 | | | 107 | nullthrows( |
| b69ab31 | | | 108 | screen.getByTestId('changed-file-src/file2.js').querySelector('input[type=checkbox]'), |
| b69ab31 | | | 109 | ), |
| b69ab31 | | | 110 | ); |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | fireEvent.click(shelveButton); |
| b69ab31 | | | 113 | |
| b69ab31 | | | 114 | await waitFor(() => |
| b69ab31 | | | 115 | expectMessageSentToServer({ |
| b69ab31 | | | 116 | type: 'runOperation', |
| b69ab31 | | | 117 | operation: expect.objectContaining({ |
| b69ab31 | | | 118 | args: [ |
| b69ab31 | | | 119 | 'shelve', |
| b69ab31 | | | 120 | '--unknown', |
| b69ab31 | | | 121 | {type: 'repo-relative-file', path: 'src/file1.js'}, |
| b69ab31 | | | 122 | {type: 'repo-relative-file', path: 'src/file3.js'}, |
| b69ab31 | | | 123 | ], |
| b69ab31 | | | 124 | }), |
| b69ab31 | | | 125 | }), |
| b69ab31 | | | 126 | ); |
| b69ab31 | | | 127 | }); |
| b69ab31 | | | 128 | |
| b69ab31 | | | 129 | it('Optimistic state hides selected files when shelve running', async () => { |
| b69ab31 | | | 130 | expect(screen.queryAllByTestId(/src\/file.\.js/)).toHaveLength(3); |
| b69ab31 | | | 131 | const shelveButton = screen.getByText('Shelve'); |
| b69ab31 | | | 132 | fireEvent.click(shelveButton); |
| b69ab31 | | | 133 | await waitFor(() => expect(screen.queryAllByTestId(/src\/file.\.js/)).toHaveLength(0)); |
| b69ab31 | | | 134 | }); |
| b69ab31 | | | 135 | |
| b69ab31 | | | 136 | it('shelve button disabled if no files selected', async () => { |
| b69ab31 | | | 137 | fireEvent.click(screen.getByText('Deselect All')); |
| b69ab31 | | | 138 | |
| b69ab31 | | | 139 | const shelveButton = screen.getByText('Shelve'); |
| b69ab31 | | | 140 | await waitFor(() => { |
| b69ab31 | | | 141 | expect(shelveButton).toBeInTheDocument(); |
| b69ab31 | | | 142 | expect(shelveButton).toBeDisabled(); |
| b69ab31 | | | 143 | }); |
| b69ab31 | | | 144 | }); |
| b69ab31 | | | 145 | }); |
| b69ab31 | | | 146 | |
| b69ab31 | | | 147 | describe('Shelved changes list', () => { |
| b69ab31 | | | 148 | const SHELVES = [ |
| b69ab31 | | | 149 | { |
| b69ab31 | | | 150 | hash: 'aaa', |
| b69ab31 | | | 151 | name: 'my shelve', |
| b69ab31 | | | 152 | date: new Date(), |
| b69ab31 | | | 153 | description: 'shelved on commit b', |
| b69ab31 | | | 154 | filesSample: [{path: 'src/shelvedfile.js', status: 'M' as const}], |
| b69ab31 | | | 155 | totalFileCount: 1, |
| b69ab31 | | | 156 | }, |
| b69ab31 | | | 157 | ]; |
| b69ab31 | | | 158 | |
| b69ab31 | | | 159 | it('shows shelved changes list tooltip', () => { |
| b69ab31 | | | 160 | fireEvent.click(screen.getByTestId('shelved-changes-button')); |
| b69ab31 | | | 161 | expect(screen.getByText('Shelved Changes')).toBeInTheDocument(); |
| b69ab31 | | | 162 | |
| b69ab31 | | | 163 | expectMessageSentToServer({ |
| b69ab31 | | | 164 | type: 'fetchShelvedChanges', |
| b69ab31 | | | 165 | }); |
| b69ab31 | | | 166 | }); |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | it('renders errors from fetching shelved changes', async () => { |
| b69ab31 | | | 169 | fireEvent.click(screen.getByTestId('shelved-changes-button')); |
| b69ab31 | | | 170 | act(() => { |
| b69ab31 | | | 171 | simulateMessageFromServer({ |
| b69ab31 | | | 172 | type: 'fetchedShelvedChanges', |
| b69ab31 | | | 173 | shelvedChanges: { |
| b69ab31 | | | 174 | error: new Error('failed to fetch shelved changes'), |
| b69ab31 | | | 175 | }, |
| b69ab31 | | | 176 | }); |
| b69ab31 | | | 177 | }); |
| b69ab31 | | | 178 | |
| b69ab31 | | | 179 | await waitFor(() => { |
| b69ab31 | | | 180 | expect( |
| b69ab31 | | | 181 | within(screen.getByTestId('shelved-changes-dropdown')).getByText( |
| b69ab31 | | | 182 | 'Could not fetch shelved changes', |
| b69ab31 | | | 183 | ), |
| b69ab31 | | | 184 | ).toBeInTheDocument(); |
| b69ab31 | | | 185 | }); |
| b69ab31 | | | 186 | }); |
| b69ab31 | | | 187 | |
| b69ab31 | | | 188 | it('renders empty state', async () => { |
| b69ab31 | | | 189 | fireEvent.click(screen.getByTestId('shelved-changes-button')); |
| b69ab31 | | | 190 | act(() => { |
| b69ab31 | | | 191 | simulateMessageFromServer({ |
| b69ab31 | | | 192 | type: 'fetchedShelvedChanges', |
| b69ab31 | | | 193 | shelvedChanges: { |
| b69ab31 | | | 194 | value: [], |
| b69ab31 | | | 195 | }, |
| b69ab31 | | | 196 | }); |
| b69ab31 | | | 197 | }); |
| b69ab31 | | | 198 | |
| b69ab31 | | | 199 | await waitFor(() => { |
| b69ab31 | | | 200 | expect( |
| b69ab31 | | | 201 | within(screen.getByTestId('shelved-changes-dropdown')).getByText('No shelved changes'), |
| b69ab31 | | | 202 | ).toBeInTheDocument(); |
| b69ab31 | | | 203 | }); |
| b69ab31 | | | 204 | }); |
| b69ab31 | | | 205 | |
| b69ab31 | | | 206 | describe('with shelved change rendered', () => { |
| b69ab31 | | | 207 | beforeEach(async () => { |
| b69ab31 | | | 208 | fireEvent.click(screen.getByTestId('shelved-changes-button')); |
| b69ab31 | | | 209 | act(() => { |
| b69ab31 | | | 210 | simulateMessageFromServer({ |
| b69ab31 | | | 211 | type: 'fetchedShelvedChanges', |
| b69ab31 | | | 212 | shelvedChanges: { |
| b69ab31 | | | 213 | value: SHELVES, |
| b69ab31 | | | 214 | }, |
| b69ab31 | | | 215 | }); |
| b69ab31 | | | 216 | }); |
| b69ab31 | | | 217 | |
| b69ab31 | | | 218 | await waitFor(() => { |
| b69ab31 | | | 219 | expect( |
| b69ab31 | | | 220 | within(screen.getByTestId('shelved-changes-dropdown')).getByText('my shelve'), |
| b69ab31 | | | 221 | ).toBeInTheDocument(); |
| b69ab31 | | | 222 | }); |
| b69ab31 | | | 223 | }); |
| b69ab31 | | | 224 | |
| b69ab31 | | | 225 | it('renders shelved changes', () => { |
| b69ab31 | | | 226 | expect( |
| b69ab31 | | | 227 | within(screen.getByTestId('shelved-changes-dropdown')).getByText( |
| b69ab31 | | | 228 | ignoreRTL('shelvedfile.js'), |
| b69ab31 | | | 229 | ), |
| b69ab31 | | | 230 | ).toBeInTheDocument(); |
| b69ab31 | | | 231 | }); |
| b69ab31 | | | 232 | |
| b69ab31 | | | 233 | it('runs unshelve', () => { |
| b69ab31 | | | 234 | fireEvent.click(screen.getByText('Unshelve')); |
| b69ab31 | | | 235 | |
| b69ab31 | | | 236 | expectMessageSentToServer({ |
| b69ab31 | | | 237 | type: 'runOperation', |
| b69ab31 | | | 238 | operation: expect.objectContaining({ |
| b69ab31 | | | 239 | args: ['unshelve', '--name', 'my shelve'], |
| b69ab31 | | | 240 | }), |
| b69ab31 | | | 241 | }); |
| b69ab31 | | | 242 | }); |
| b69ab31 | | | 243 | |
| b69ab31 | | | 244 | it('runs apply', () => { |
| b69ab31 | | | 245 | fireEvent.click(screen.getByText('Apply')); |
| b69ab31 | | | 246 | |
| b69ab31 | | | 247 | expectMessageSentToServer({ |
| b69ab31 | | | 248 | type: 'runOperation', |
| b69ab31 | | | 249 | operation: expect.objectContaining({ |
| b69ab31 | | | 250 | args: ['unshelve', '--keep', '--name', 'my shelve'], |
| b69ab31 | | | 251 | }), |
| b69ab31 | | | 252 | }); |
| b69ab31 | | | 253 | }); |
| b69ab31 | | | 254 | |
| b69ab31 | | | 255 | it('runs delete', () => { |
| b69ab31 | | | 256 | fireEvent.click(screen.getByTestId('delete-shelve-aaa')); |
| b69ab31 | | | 257 | |
| b69ab31 | | | 258 | expectMessageSentToServer({ |
| b69ab31 | | | 259 | type: 'runOperation', |
| b69ab31 | | | 260 | operation: expect.objectContaining({ |
| b69ab31 | | | 261 | args: ['shelve', '--delete', 'my shelve'], |
| b69ab31 | | | 262 | }), |
| b69ab31 | | | 263 | }); |
| b69ab31 | | | 264 | }); |
| b69ab31 | | | 265 | |
| b69ab31 | | | 266 | it('dismisses tooltip while running unshelve', () => { |
| b69ab31 | | | 267 | fireEvent.click(screen.getByText('Unshelve')); |
| b69ab31 | | | 268 | |
| b69ab31 | | | 269 | expect(screen.queryByTestId('shelved-changes-dropdown')).not.toBeInTheDocument(); |
| b69ab31 | | | 270 | }); |
| b69ab31 | | | 271 | |
| b69ab31 | | | 272 | it('can open comparison view for the shelve', async () => { |
| b69ab31 | | | 273 | fireEvent.click( |
| b69ab31 | | | 274 | within(screen.getByTestId('shelved-changes-dropdown')).getByText('View Changes'), |
| b69ab31 | | | 275 | ); |
| b69ab31 | | | 276 | |
| b69ab31 | | | 277 | await waitFor(() => { |
| b69ab31 | | | 278 | expectMessageSentToServer({ |
| b69ab31 | | | 279 | type: 'requestComparison', |
| b69ab31 | | | 280 | comparison: { |
| b69ab31 | | | 281 | type: ComparisonType.Committed, |
| b69ab31 | | | 282 | hash: 'aaa', |
| b69ab31 | | | 283 | }, |
| b69ab31 | | | 284 | }); |
| b69ab31 | | | 285 | }); |
| b69ab31 | | | 286 | }); |
| b69ab31 | | | 287 | |
| b69ab31 | | | 288 | it('refetches shelves when reopening dropdown', async () => { |
| b69ab31 | | | 289 | expect( |
| b69ab31 | | | 290 | within(screen.getByTestId('shelved-changes-dropdown')).getByText('my shelve'), |
| b69ab31 | | | 291 | ).toBeInTheDocument(); |
| b69ab31 | | | 292 | |
| b69ab31 | | | 293 | resetTestMessages(); |
| b69ab31 | | | 294 | |
| b69ab31 | | | 295 | act(() => { |
| b69ab31 | | | 296 | userEvent.type(document.body, '{Escape}'); |
| b69ab31 | | | 297 | }); |
| b69ab31 | | | 298 | |
| b69ab31 | | | 299 | // reopen the dropdown |
| b69ab31 | | | 300 | fireEvent.click(screen.getByTestId('shelved-changes-button')); |
| b69ab31 | | | 301 | expectMessageSentToServer({ |
| b69ab31 | | | 302 | type: 'fetchShelvedChanges', |
| b69ab31 | | | 303 | }); |
| b69ab31 | | | 304 | |
| b69ab31 | | | 305 | // send a new set of changes |
| b69ab31 | | | 306 | const NEW_SHELVES = [ |
| b69ab31 | | | 307 | { |
| b69ab31 | | | 308 | hash: 'a', |
| b69ab31 | | | 309 | name: 'my new shelve', |
| b69ab31 | | | 310 | date: new Date(), |
| b69ab31 | | | 311 | description: 'shelved on commit b', |
| b69ab31 | | | 312 | filesSample: [{path: 'src/shelvedfile.js', status: 'M' as const}], |
| b69ab31 | | | 313 | totalFileCount: 1, |
| b69ab31 | | | 314 | }, |
| b69ab31 | | | 315 | ]; |
| b69ab31 | | | 316 | act(() => { |
| b69ab31 | | | 317 | simulateMessageFromServer({ |
| b69ab31 | | | 318 | type: 'fetchedShelvedChanges', |
| b69ab31 | | | 319 | shelvedChanges: { |
| b69ab31 | | | 320 | value: NEW_SHELVES, |
| b69ab31 | | | 321 | }, |
| b69ab31 | | | 322 | }); |
| b69ab31 | | | 323 | }); |
| b69ab31 | | | 324 | |
| b69ab31 | | | 325 | // make sure we get the new name and not the old one |
| b69ab31 | | | 326 | expect( |
| b69ab31 | | | 327 | within(screen.getByTestId('shelved-changes-dropdown')).queryByText('my shelve'), |
| b69ab31 | | | 328 | ).not.toBeInTheDocument(); |
| b69ab31 | | | 329 | await waitFor(() => { |
| b69ab31 | | | 330 | expect( |
| b69ab31 | | | 331 | within(screen.getByTestId('shelved-changes-dropdown')).getByText('my new shelve'), |
| b69ab31 | | | 332 | ).toBeInTheDocument(); |
| b69ab31 | | | 333 | }); |
| b69ab31 | | | 334 | }); |
| b69ab31 | | | 335 | |
| b69ab31 | | | 336 | it('shows optimistic state for unshelve', () => { |
| b69ab31 | | | 337 | expect( |
| b69ab31 | | | 338 | within(screen.getByTestId('commit-tree-root')).queryByText(ignoreRTL('shelvedfile.js')), |
| b69ab31 | | | 339 | ).not.toBeInTheDocument(); |
| b69ab31 | | | 340 | |
| b69ab31 | | | 341 | fireEvent.click(screen.getByText('Unshelve')); |
| b69ab31 | | | 342 | |
| b69ab31 | | | 343 | expect( |
| b69ab31 | | | 344 | within(screen.getByTestId('commit-tree-root')).getByText(ignoreRTL('shelvedfile.js')), |
| b69ab31 | | | 345 | ).toBeInTheDocument(); |
| b69ab31 | | | 346 | }); |
| b69ab31 | | | 347 | }); |
| b69ab31 | | | 348 | }); |
| b69ab31 | | | 349 | }); |